PHP code review/checking methods during development
Asked Answered
O

4

0

I am looking for methods for performing sanity checks of PHP code during development

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
  • Using editors with error checking like NetBeans and Eclipse

Are there more ways to find problems in PHP code early?

Ovalle answered 19/1, 2010 at 21:0 Comment(1)
Tool requests, recommendation questions, and list questions (this one is all three in one) are off-topic on Stack Overflow.Alanalana
I
2

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.

Idioplasm answered 19/1, 2010 at 21:36 Comment(1)
A tool that just tokenizes the input might not object tokens that are individually OK, but aren't legal syntax, e.g., ") 5 (".Naaman
L
9

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

Labroid answered 19/1, 2010 at 21:2 Comment(2)
+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.Holle
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.Krasnodar
I
2

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.

Idioplasm answered 19/1, 2010 at 21:36 Comment(1)
A tool that just tokenizes the input might not object tokens that are individually OK, but aren't legal syntax, e.g., ") 5 (".Naaman
H
0

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

Hopple answered 19/1, 2010 at 21:45 Comment(0)
N
0

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.

Naaman answered 9/3, 2010 at 11:9 Comment(1)
SD PHP Formatter 404 wwwsemanticdesigns.com/Products/Formatters/PHPFormatter.htmlProlific

© 2022 - 2024 — McMap. All rights reserved.