Before I start explaining how to use the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to debug conditional statements within a PHP application, I’m going to reintroduce the last example that I created in the preceding tutorial. It was aimed at illustrating how to utilize the same functions for keeping track of which lines were executed by a simple function and a sample class. The code samples corresponding to these practical examples originally looked like this: (example on debugging program flow with a PHP function) xdebug_start_code_coverage(); function displayIntegers(){ for($i=1;$i<=10;$i++){ echo $i; } } displayIntegers(); var_dump(xdebug_get_code_coverage()); /* displays the following 12345678910 array 'pathtoexampleflow_debugging_example.php' => array 4 => int 1 5 => int 1 6 => int 1 7 => int 1 8 => int 1 9 => int 1 10 => int 1 */ (example on debugging program flow with a PHP class) 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; } } try{ xdebug_start_code_coverage(); $user=new User('John','Doe','john@domain.com'); echo 'First Name :'.$user->getFirstName().'<br />'; echo 'Last Name :'.$user->getLastName().'<br />'; echo 'Email :'.$user->getEmail().'<br />'; var_dump(xdebug_get_code_coverage());
/* displays the following First Name :John Last Name :Doe Email :john@domain.com array 'pathtoexampleflow_debugging_example.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 27 => int 1 31 => int 1 36 => int 1 37 => int 1 38 => int 1 39 => int 1 41 => int 1 */ } catch(Exception $e){ echo $e->getMessage(); exit(); } As illustrated above, the combination of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions is very convenient for determining the program flow of a PHP script. In the first example, these functions are used in conjunction to determine the sequence of lines that are executed by a simple PHP function, while in the second case, this same process is applied to a primitive class. At this point, I’m pretty sure that you have already grasped the logic that drives the two previous code samples, since they’re very easy to follow. With that idea in mind, it’s time to continue exploring the functionality provided by the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions. Thus, as I expressed in the introduction, it would be quite useful to demonstrate how these functions can be used with conditional statements. In the upcoming section I’ll be coding a brand new example, which hopefully will demonstrate how to utilize these functions with a simple conditional instruction. As usual, to see how this example will be developed, please click on the link shown below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|