Introduction to the PHP Parser library
PHP Parser is a library that takes a source code written in PHP, passes it through a lexical analyzer, and creates its respective syntax tree. This is very useful for static code analysis, where we want to check our own code not only for syntactic errors but also for satisfying certain quality criteria.
In this chapter, we'll write an application that takes a directory, iterates all its files and subdirectories recursively, and runs each PHP file through the PHP Parser. We will check only for one specific pattern; that is enough for this demo.
We want to be able to find any statement where we use the assignment inside a condition. This could be any of the following examples (this time we're also including line numbers for clarity):
// _test_source_code.php 1. <?php 2. $a = 5; 3. if ($a = 1) { 4. var_dump($a); 5. } elseif ($b = 2) {} 6. while ($c = 3) {} 7. for (; $d = 4;) {}
All this is of course a...