Idea for a PHP Validator
How about creating a PHP code validator? Got this idea by looking at tokenizer and reflection extensions. I doubt I will find the time to work on this myself but then someone else might be interested to pick it up.
From user’s point of view there could be a form made of a large textarea box and a single file upload input. One could paste code snippet on that textarea or upload a ZIP'ed source code archive (or a single .php file) for validation.
What’s the result?
Validation page could show an output like:
PHP version required: 5.1.4+
Required extensions:
- gd
- pdo
- pdo_pgsql
- etc..
E_STRICT compatible: No
Lines of code: 11,453 (36% comments)
(and anything else I can't think of right now)Optional/additional information could be provided – like, for example, what’s the cause (think function name and line number) of that 5.1.4+ requirement.
What about implementation, what’s the cache?
As already suggested – implementation should rely on tokenizer/reflection extensions and not use preg_match() or any kind of custom parser. As for determining required PHP version I can think of two things:
- Use php.net documentation to get function/method/feature requirements (can be automated to some extent)
- use tokenizer extension to analyze uploaded code and apply version requirements (for example – a T_ABSTRACT token implies a 5.0.0+ version)
Getting required extensions should be quite simple, if not for code like:
if (extension_loaded('apc')) { /* stuff */ } if (function_exists('apc_store')) { /* stuff */ } if (method_exists('ZipArchive', 'open')) { /* stuff */ } // or the worst case possible $func = 'apc_store'; $func(...); // or use of get_defined_functions(), get_loaded_extensions()
This of course does not mean it’s not possible to implement – it’s just that you will have to track these things in your validator code.
Who could make use of this tool?
- Someone writing a library/module
- Administrator deploying a PHP application/website
- Forum/StackOverflow/etc user pasting code snippet in that textarea to quickly check requirement
- And anyone else not too sure about their code
So what do you think? Is it worth the time? Are there any other technical issues I am not aware right now that could hinder implementation?

Comments 4 Comments
http://pear.php.net/package/PHP_CompatInfo/
By the way, the package is looking for a new maintainer if you feel so.
By the way, the "Lines of code" part could rely on phploc :
http://github.com/sebastianbergmann/phploc
For the "E_STRICT compatible", I do not know any tool that can provide this data.
Sebastian Bergmann has a wealth of knowledge and is the maintainer or contributor on a bunch of these projects I believe. Saw him give a talk at OSCON this year.
http://phpqatools.org
phpcs - php code sniffer, just like the PEAR module, based on tokenizer
phpmd - php mess detector - reports violations of rules
pdepend - PHP_Depend
phpcpd - copy/paste detector
then also:
php lint, things of that nature