checking syntax of php-code w/o php-cli, possible? - php

Is it possible to check syntax of php-code (without running),
similar to the
php-cli -l
when running php the "ordinary" way (as a module)?

There are also some PECL extensions which parse PHP code for various reasons. First there is BCompiler, which can compile a PHP into byte code. Since this step requires parsing the PHP code, I would expect errors if it isn't lint. Parsekit allwos you to compile PHP code to OP codes, which is basically what you desire. However, the extension did not have a release since late 2009, so it might be outdated. Parse_Tree is sadly unmaintained since 2007, but its purpose is to parse a PHP file into an AST. Maybe you can get to something with this one, after some polishing.
PHP_Parser is a PEAR package, which does not rely on special PHP extensions and attempts to parse PHP code from within PHP. Its marked alpha and unmaintained, but it might give you a basis to experiment with.

You can try to run a tool like PHP Depend on the sources, which attempts to parse the given PHP files into an abstract syntax tree. While this might not catch all PHP parser errors, it will already catch quite a lot of them.
You get nice software metrics as an additional goodie, if the code is valid. :)

Is it possible to check syntax of php-code (without running), similar to the php-cli -l
when running php the "ordinary" way (as a module)?
I think the question everyone missed is that there is no difference in PHP syntax whether you run it as a module, or simply execute the binary from a shell: the PHP syntax is the same on both occasions. So, you might as well just use php -l filename.php, as that has the exact same result as using the tools listed above.

For getting the same result as php-cli -l, use the function: php_check_syntax

Related

Does PHP have a built-in means of determining the oldest PHP version compatible with a given PHP script? Or will it ever?

I wish to be able to do something like:
php --determine-oldest-supported-php-version test.php
And get this output:
7.2
That is, the php binary checks test.php for all function calls or syntax used, and then spits out the oldest version of PHP which would be able to run the script.
The purpose would be for me to create a script which goes through all the .php files in my library/framework and figures out which version of PHP I should consider to be the "oldest supported version".
Currently, I'm doing this manually. Whenever I knowingly use a new feature, function or syntax, I bump up a variable to "7.4" or whatever, and then compare the currently used PHP version against that on runtime. This is not ideal, as you can imagine, and I could very well forget to bump up this value or bump it up too much, or too little, etc.
It would be much nicer to be able to determine this automatically, with such a feature in PHP itself.
I have of course looked through the list of options and PHP has no such feature as far as I can tell. Since I basically answered my own question right away, are there any plans on such a feature in the future?
The only guarantee given is that PHP will remain compatible within the same major version.
You may be interested in looking at the article
Why You Should Be Using Supported PHP Versions.
The tool you are looking for is the GitHub project
PHP_CodeSniffer,
further described in the article
PHPCompatibility update.
You may run it using a command such as:
phpcs --standard=PHPCompatibility --runtime-set testVersion 5.4 <path-of-your-php-files>

Parsing php code conditionally to avoid parse errors on older php versions

I have scripts that must be run with PHP 5.1.0 or higher (because using PDO class, for instance).
If not, they bring parse errors.
I'm talking of scripts that anonymous visitors are invited to download, install on their server (on which I can't have any information) and run to demonstrate how easy php scripts can communicate with a system of ours.
Of course, running any of these scripts and getting a "parse error at line xxx" won't really give a good image of the solution...
I tried echoing a neat "you need PHP 5.1.0 or higher to run this script" error message, using if (version_compare(PHP_VERSION, '5.1.0') < 0) { die(...); } but, of course, this doesn't work, as PHP first parses the whole script before starting executing it and having a chance to die on this test.
So my question is:
Is there a way in PHP to do some tests on php version BEFORE actually parsing?
I mean something like C/C++ #if or #ifdef PRE-processor directives.
Or any other type of workaround?
One constraint beeing: the solution to the issue should not make the whole scripts look unreadable or complex to understand (like massive calling of eval() which breaks syntax coloration) nor uncomprehensibly contrived (like having each script conditionaly included in a "container script"), because the global purpose is to demonstrate how SIMPLE it is to communicate with our system...
All solutions found googling around were about maintaining compatibility, using arrays of alternative functions to call depending on php version, comined with eval everywhere where something could go wrong for php parser... I would need something really simpler like "if php version not ok, don't parse"...

php compiler or syntax checker

I'm looking for a php syntax checker, preferably as an eclipse plugin, preferably be able to sort of compile it(at least find undefined variables in addition to syntax checking. Does such thing exist?
Every better IDE (PhpStorm, Eclipse/PDT, Eclipse/PHPEclipse, Netbeans with PHP-Plugin, and so on) comes with automatic syntax check built-in. At least PhpStorm is able to find undefined variables. The last time I used PDT it didn't support it. PHPEclipse seems to be not maintained anymore, so I assume, that it cannot find undefined variables too, and netbeans ... don't know.
If you just want to check the syntax the quick&dirty way, you can use the php-interpreter itself
php -l filename.php
Of course, look on this:
http://www.eclipse.org/pdt/
This IDE using php parser engine to syntax, and is for free :-).
But, better do not use the plugin version from update site, but All-In-One Package. At least previous versions from update site did not work too well...
Install phpEclipse for syntax-checking:
http://www.phpeclipse.com/
In PHP there is no such thing as an undefined variable. Variables are automatically initialized with null.
Zend Studio (custom Eclipse) also has a syntax checker, FWIW.
Our PHP Formatter parses PHP code to an AST, and then prettyprints the results. This can be used as a command-line script. If the source file isn't parsable, the tool exits with with an error (and doesn't prettyprint). So, if you ignore the prettyprint feature, this is precisely a command-line level syntax checker. Easy to launch from Eclipse.

PHP Sanity Check

I am looking for applications or methods for performing sanity checks of php code.
I hope to to avoid finding out about the coding mistakes the hard way, but instead find them before publishing the website.
display_errors = on and similar run-time methods find the problems too late.
So far I have found the following ways, which I think are not thorough enough:
php_check_syntax() from within php
php -l from the command line
ioncube php encoder
netbeans and eclipse as editors
What better way is there to find problems in PHP code early?
How bout unit testing? =) http://www.phpunit.de/
PHP Code Sniffer can help ensure you're writing code to a set standard.
http://pear.php.net/package/PHP_CodeSniffer/
PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.
Incidentally, if you want to get really into code checking, you can integrate Code Sniffer, PHPUnit and a repo together with something like phpUnderControl for automating such a process.
You could of course strip back a little and get a friend, colleague ... or dare I say it a Coding Buddy - nothing better than getting a real human being to check your code when you check it in :)
The DMS Software Reengineering Toolkit has a full PHP parser which does syntax checks. That's a big system if all you want is syntax checking.
One way to get "just" the syntax checking part of DMS is the SD PHP Formatter. This tool formats PHP code nicely. To do so, it parses it first (there's the syntax check) and then prettyprints it according to the structures implied by the PHP language rules. Of course, you could just ignore the formatted result and simply look for parsing errors.
If you like the test coverage idea, you should consider the SD PHP Test Coverage tool. This packages DMS to parse your source code, fill it with instrumentation to determine what gets executed when you run. It obviously has the syntax check still built in, as well as providing the test coverage ability.

Debugging PHP

I've been using xdebug to debug and understand code in php projects for a while now, and have sometimes come into situations where it's been unclear what's going on inside of PHP.
Is it possible to set xdebug or gdb up so that i can trace into actual php builtin functions?
If you are using a macosx, solaris or recent freebsd system you can throw a little dtrace at it. It can come in handy for those all too numerous "WTF is PHP doing?" moments.
I doubt it, xdebug is intended for tracing your PHP code, not the internals. The internals are assumed to be bug-free (which obviously they aren't sometimes, but that's beyond the scope of xdebug).
You can always look at the PHP source if you want to know what the built-in functions do, but that's sometimes pretty hairy. The PHP manual docs have always served me well enough when I want to know what they'll do.
You can use gdb to trace in to the C-level code, provided you have php compiled with debug symbols. Have a look here for a start:
http://derickrethans.nl/phps_segmentation_faults_gdbfu.php
One way to test the output of the Zend engine, to peek inside at the opcodes, you can use Derick Rethan's VLD (Vulcan Logic Dissasembler), which also appears to be on PECL. Note: only works on *nix systems (see site for requirements).
Some examples of debugging these opcodes can be found on Sara Golemon's blog, in articles such as Understanding Opcodes and How long is a piece of string?.
There is also great Google Chrome extension PHP Console with php library that allows to:
See errors & exception in Chrome JavaScript console & in notification popups.
Dump any type variable.
Execute PHP code remotely.
Protect access by password.
Group console logs by request.
Jump to error file:line in your text editor.
Copy error/debug data to clipboard (for testers).
Recommend to everyone!

Categories