PHPStan is a free tool to statically analyze PHP projects and programs. Here it inspects PHP files inside the specified directory based on the level of checking. It can detect syntax errors, type system issues, unknown classes, unknown methods, undefined values, dead code, and many others. You just add the PHPStan dependency via composer to your project and then use it to find errors in the entire code base. The best part is that it checks for errors without actually running the code and that it why it is called a static analysis tool for PHP.
After doing the entire project in PHP, it is a good idea to look for undesirable errors. Long projects often have a lot of errors that need a resolution and to do that, you can use the tool I have mentioned here. Use PHPStan to do static analysis of the entire project and then generate a comprehensive report. You can do all the checking from the command line and PHPStan will generate a beautiful report with all the details as you can see in the screenshot below.
Free tool to Statically Analyze PHP Projects for Errors: PHPStan
If you have a PHP project already on your PC then you can simply add PHPStan as a dependency to it via composer. Assuming that you have the composer installed, you just have to run the following command to install it.
composer require --dev phpstan/phpstan
Now, after it has been added to the project successfully, you can start using it. To do that, you just run it like as shown below. The binary is available in the “vendor” folder. You just specify a folder or file like this and then it will quickly generate a simple report as you can see in the screenshot below.
vendor/bin/phpstan analyse src
Above you can see how it generated a very simple report with no errors. This is because by default, it only does basic checks and to check the code thoroughly, you will have to add a “level” parameter. There are 9 different type of levels that you specify from 0-9 to analyze the code for errors and potential inconsistencies. You can generate a detailed report with PHPStan by adding a level parameter like this.
vendor/bin/phpstan analyse src --level 8
In this way, you can use this simple and powerful tool to analyze your PHP projects. Use this tool in this way and then you can find and locate some silly mistakes and some errors that you can debug to ship a bug-free code. The tool is very simple and very easy to use and you only have to run a few commands.
Closing thoughts:
If you are a PHP programmer or developer then you will surely find PHPStan a very useful tool and a dependency for all your projects, use it to catch errors before encountering them in the dev build or dry run. Just go through this post here and see the commands and the syntax that you have to use to locate errors and then you can fix your code accordingly.