views:

313

answers:

4
+1  Q: 

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?

+8  A: 

How bout unit testing? =) http://www.phpunit.de/

robertbasic
+1 yep. (insert 15 char filler)
David Oneill
+1 indeed. However PHPUnit can be quite heavy and daunting especially if youre new to testing and dont need all the great features PHPUnit provides. In this case i would recommend Lime instead.
prodigitalson
Good unit tests are helpful in so many ways. They'll catch dumb syntax and programming mistakes, verify that your code behaves how you expect, and help you find what parts of your program break when you change something or add new features. However, there is an art to it and writing good tests (and testable code) has a learning curve of its own. Well worth the effort, though.
Bryan M.
+1  A: 

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.

CJD
Just what I was going to suggest.
symcbean
A tool that just tokenizes the input might not object tokens that are individually OK, but aren't legal syntax, e.g., ") 5 (".
Ira Baxter
A: 

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 :)

foxed
A: 

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.

Ira Baxter
SD PHP Formatter 404 http://wwwsemanticdesigns.com/Products/Formatters/PHPFormatter.html
Kirzilla
Sorry. Link fixed.
Ira Baxter