Back in 2009 and 2010 I wrote about a PHPUnit patch I wrote to automatically verify parameter types in function calls. The feature never made it into PHPUnit and honestly it didn’t really fit into the feature set either. Although I still plan on releasing it as a PHPUnit extension that you can easily load, I’ve since been using it outside of PHPUnit, not just on tests, but on any PHP code.
Introducing PHPConsistent
PHPConsistent will verify your code using both dynamic and static analysis.
The goal is to improve code quality of your code and the libraries you use by :
- Verifying your code is making calls using the right parameters and parameter types
- Verifying if the in-line documentation (docblock) of the called functions/methods is accurate
It will compare :
- Parameter types specified in the docblock <-> types of parameters passed upon calling the function/method
- Number of parameters specified in the docblock <-> number of parameters actually present in the function/method definition
- Names of parameters specified in the docblock <-> names of parameters actually present in the function/method definition
Sample output
Invalid type calling SomeClass->GiveMeAnArray : parameter 3 ($somearray) should be of type array but got boolean instead : library/App.php (line 5) Parameter names in function definition and docblock don't match when calling JustAnotherFunction : parameter 2 ($inputFilename) should be called $inputFile according to docblock : application/Bootstrap.php (line 214) Parameter count in function definition and docblock don't match when calling OneMoreFunction : function has 6 but should be 5 according to docblock : application/Bootstrap.php (line 215)
Performance
Keep in mind that PHPConsistent relies on Xdebug’s trace functionality, making it quite slow. It also needs to analyze the output of that trace, making it even slower. So it’s definitely not something you want to run on a production environment !
Want to know more ?
Check out the PHPConsistent Github page
Hey.
Seems interesting. Wanted to try it out, but a quick look into GitHub revealed that I need to include this file into my codebase.
Would you consider making a .phar / executable version of it, so it would go to vendor/bin/phpconsistent and I could run it via CLI, as part of an automated inspection suite, run by PHING?
I’m working on the PHPUnit plugin (actually a PHPUnit listener), which will allow you to use PHPConsistent while running your unit tests.
Making it a phar won’t allow you to execute your normal code, however using the prepend functionality (described at https://github.com/Cube-Solutions/PHPConsistent) would work for this purpose. Note though that you’ll need to ignore all PHPUnit classes, which the PHPUnit plugin will take care of for you. I’m also trying to get that plugin to do multithreading, so it analyzes everything a lot faster. I’m just dealing with some very odd reflection issues right now. Once that’s sorted, the plugin should be available.