To be frank, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions really start to shine when used to keep track of which lines are executed by a PHP application based on a certain condition. To exemplify this specific situation, I’m going to use the already familiar “User” class that you learned in the prior section, along with a simple script which will change its execution flow according to a semi-random value generated by the built-in PHP “rand()” function. Of course, in this case, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions will be employed to keep track of which code block is executed by the script in question. The code sample that shows how to use these functions with a basic conditional statement is as follows: class User{ private $firstName; private $lastName; private $email; public function __construct($firstName,$lastName,$email){ if(!$firstName){ throw new Exception('Invalid parameter First Name.'); } if(!$lastName){ throw new Exception('Invalid parameter Last Name.'); } if(!$email){ throw new Exception('Invalid parameter Email.'); } $this->firstName=$firstName; $this->lastName=$lastName; $this->email=$email; } // get first name public function getFirstName(){ return $this->firstName; } // get last name public function getLastName(){ return $this->lastName; } // get email public function getEmail(){ return $this->email; } } xdebug_start_code_coverage(); $user=new User('John','Doe','john@domain.com'); if(rand(1,10)<5){ echo 'First Name :'.$user->getFirstName().'<br />'; echo 'Last Name :'.$user->getLastName().'<br />'; } else{ echo 'Email :'.$user->getEmail().'<br />'; } var_dump(xdebug_get_code_coverage()); /* displays the following when 'rand()' function returns a value < 5 First Name :John Last Name :Doe array '/path/to/example/program_flow_debug.php' => array 8 => int 1 11 => int 1 14 => int 1 17 => int 1 18 => int 1 19 => int 1 20 => int 1 23 => int 1 36 => int 1 37 => int 1 38 => int 1 39 => int 1 40 => int 1 44 => int 1 */ Definitely, the above example shows in a nutshell how useful the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be when it comes to debugging conditional statements within a PHP script. In this case, the “rand()” PHP native function is utilized in conjunction with an “if” conditional statement to create a simple situation where there are two blocks of code that can be invoked. Logically, this example shows the sequence of lines executed when the “rand()” function returns a value less than 5, in this way demonstrating the functionality of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions. So far, so good, right? You hopefully understand how simple it is to debug conditional statements with the two functions shown before. The previous script would be rather incomplete, however, if I don’t show you the output it produces when the value returned by the “rand()” function is greater than 5. Therefore, in the last section of this article I will represent this condition through a functional script, in this manner finishing this quick overview on using the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions included with the X-debug extension. Click on the link below and read the next few lines, please.
blog comments powered by Disqus |
|
|
|
|
|
|
|