Parsing a chunk of code and display a CSV results

Overview

This example show how to parse a single string (chunk of code) and display results in CSV format with the corresponding renderer rather than the default one (Array).

Dependencies

This example requires mandatory resources :

Source Code

The very basic script to parse the chunk of code (lines 4 to 19)

  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $str1 = '<?php
  5. class Request6056
  6. {
  7.    function testMaxVersion()
  8.    {
  9.        // PHP 5 <= 5.0.4
  10.        $res = php_check_syntax("bug6581.php");
  11.  
  12.        $array1 = array("blue"  => 1, "red"  => 2, "green"  => 3);
  13.        $array2 = array("green" => 5, "blue" => 6, "yellow" => 7);
  14.  
  15.        // PHP 5 >= 5.1.0RC1
  16.        $diff = array_diff_key($array1, $array2);
  17.    }
  18. }
  19. ?>';
  20.  
  21. $dataSource = array($str1);
  22. // CAUTION: if you forget this option, you will have no output except a FALSE result
  23. $options = array('is_string' => true);
  24.  
  25. $info = new PHP_CompatInfo('csv');
  26. $info->parseArray($dataSource, $options);
  27. // you may also use unified method: $info->parseData($dataSource, $options);
  28. ?>

Output

"Source code";"Version";"C";"Extensions";"Constants/Tokens"
"<?php ... ?>";"5.1.0,5.0.4";"0";"";""

Explains step by step

At line 25, we created an instance of PHP_CompatInfo with the renderer type to use (csv) and its default options:

At line 26, we used the parseArray() method because we have pre-set input for it (see line 21). But we also may used the parseString() method with $dataSource = $str1.

In all cases, don't forget to specify the parser option is_string (= true).

On CSV results, we can find an header line, follow by results itself. Of course each column available depend of output-level configuration option (default = 31 : all details).

<?php ... ?> identify a chunk of code analysed, and "C" = 0 (so accuray is at 100%).

Finally, we can tell that the chunk of code (in $str1) need PHP 5.1.0 (minimum) and PHP 5.0.4 (maximum) to run : it's an impossible case (conflict). This script is for the purpose of demonstration of version/max_version only.