No one’s perfect (even if we wish we were), and that counts for coders too. Even with a relatively simple and commonly used language like PHP, mistakes can be (and are) made. It’s terrible when those mistakes don’t get caught in the web of PHP error reporting you go through before publishing your project. Somewhere down the line, someone tries to do something and the system breaks. Enter PHP code analyzers.
Instead of reading through lines of code to try and figure out where you went wrong, a code analyzer will do most of the work for you, by providing an error log that highlights any issues you may have missed. There are two types of analyzers. The first are called ‘linters’ and check for syntax and style errors. The second are called ‘static analyzers’ and check for logical issues.
Here are five of our favorite code analyzers and tools for use with PHP. All of which are designed for error reporting and ensuring your code is up to scratch.
Avoid Common Mistakes with PHP Code Checker
“Silly mistakes” is what we call them, but they’re not funny. Neither is sitting in front of a computer for hours staring at lines of code because you forget one little symbol… somewhere. PHP Code Checker is a linter that aims to catch and fix these “silly mistakes”.
It works through either copy and paste into an online form or an API, which then matches your code against a list of common mistakes previously identified by Mario Lurig. The tool is free and can catch mistakes even an integrated development environment (IDE) misses.
PHP Code Checker Compatibility
PHP Code Checker is a web-based form for checking your code, so don’t expect too much out of it. Currently, PHP Code Checker (php -l) uses PHP version 5.6.10.
PHP Code Checker is a great starter tool to check your PHP code for errors or inconsistencies. However, it won’t take you much further than a basic, aesthetic check. If you’re looking to get deeper into PHP error reporting, keep reading.
Maintain Good Syntax with PHP Code Syntax Check
One of the issues several code checkers and PHP tools have is that they are limited by the version of PHP they support. PHP Code Syntax Check helps you to get around this by providing support for legacy versions of PHP as far back as 4.4.9.
PHP Code Syntax Check Compatibility
You can expect a high level of compatibility with PHP code syntax check and your PHP scripts; with support for legacy versions of PHP, including 4.4.9.
If you’re still working with legacy versions of PHP, then PHP code syntax check is great for checking your code for syntax errors. Otherwise, move on.
Minimize Logic Errors with Phan
Started in July 2015, Phan is a brilliant static analyzer for PHP that prefers to minimize false-positives. In other words, instead of checking if something is correct, it checks if it’s incorrect.
Isn’t that the same thing? In a word: no.
By checking whether something is incorrect, Phan manages to find and report errors that other PHP checkers miss; errors which can break your code without you realizing until release day (or even later).
Phan works by making use of PHP 7’s AST extension to decipher the inputted PHP code. It then scans the code (using two passes) to build out several hashes of features and highlight areas which need to be reviewed.
Phan is a great tool for making sure your PHP code doesn’t fall victim to logic errors. While it may be harder to implement than simple web-based error checkers, the results are far superior and worth the effort.
Minimize Syntax and Style Errors with Code Sniffer
In the world of coding, controversy surrounds the idea of coding standards. Some believe that they are a good thing, others believe that coders should be able to do what they want (including comment omission). That’s fine for small agencies or lone wolf developers, but as you become part of bigger and larger organizations, coding standards become almost impossible to live without.
Code Sniffer checks PHP code and reports on style errors by checking for inconsistencies with a defined coding standard. Instead of having several different styles competing for dominance in the office place, a single standard can be defined and adhered to.
Code Sniffer does this by tokenizing your code. Tokenizing can be considered to be a sub-task of parsing. It is a process in which code is demarcated and character string sections are classified into tokens. These ‘tokens’ are then passed to another form of processing.
Code Sniffer also includes another tool known as PHPCBF, which allows you to automatically check and fix several style and formatting issues.
Code Sniffer Compatibility
Analyze in Secure Environments with Psalm
Psalm is a static analyzer created by Vimeo. While not as well known as Phan, Psalm comes with a slight advantage, in that it does not require the PHP ast module like most other static analyzers do.
This is a huge advantage for those trying to analyze code in secure environments where you do not have the ability to install PHP modules. For instance, web hosting accounts, managed servers, continuous integration VMs, and more.
While Psalm’s documentation states that it works well with PHP 5.6+ and PHP 7, it is only actively being tested on 7.0 and higher.
If you’re a web dev with limitations on what you’re able to do, then Psalm is a great way to get around those limitations.
Everyone codes differently. We all have our own tricks for making sure that one line works, but we often don’t account for other coders on our team when we use them.
Code analyzers are the best way of maintaining a coding standard among large development teams, or when trying to create a script you want to release to the public.
So, whether you’re a PHP master, or just a beginner looking for a way to improve your coding skills, try the PHP tools above and see if they make a difference.