PHPStan is a static analyzer for PHP focused on finding bugs in your code without actually running it. PHPStan 1.0 finally has been released, so let’s see what’s new.
Introducing static code analysis into your process should improve the quality of your code and make the QA process more efficient due to finding errors earlier on.
PHPStan is an open source tool with 10.4K GitHub stars and 737 GitHub forks. It is probably the most popular static analysis system for PHP projects, which finds bugs in your codebase by inspecting the source files. In other words, you don’t need to run your code or manually write tests to discover issues.
Related: How to Configure Nginx to Work with PHP via PHP-FPM
PHPStan will read the PHP code from the passed files. If it encounters unknown classes, then it will try to autoload them and understand their interface by reflection. On top of that you can also configure a path to the Bootstrap file via which you can configure autoload, as well as
include() some additional files in order to simplify the PHPStan analysis.
Furthermore, PHPStan doesn’t only perform autoload in the case of unknown classes, but it also does so for all classes.
PHPStan 1.0 was released earlier this month as the first stable release for this leading open-source PHP static analysis tool. Let’s take a brief look at what’s new.
PHPStan 1.0 Highlights
The flagship feature of PHPStan 1.0 is a brand-new level 9. It includes all the rules from level 8 and lower, as well as one extra check: strict
mixed type comparisons.
If you’re not familiar, the fundamental way of how PHPStan handles and accommodates analysis of codebases of varying quality is the concept of rule levels.
If you want to use PHPStan but your codebase isn’t up to speed with strong typing and PHPStan’s strict checks, you can currently choose from 10 levels, where 0 is the loosest and 9 is the strictest.
Moreover PHPStan 1.0 now remembers when you call a function for a second time and the function is supposed to return the same value. On top of that you can also allows extending PHPStan’s comprehension of your code where you might declare seemingly unused properties and constants for good reasons.
Last but not least, the new version brings a lot of stability improvements and performance optimizations.
You can Learn more about the PHPStan 1.0 PHP static analyzer release on the project’s website.
Installation and Usage
To run PHPStan 1.0, you’ll need to have PHP 7.1 or newer. This requirement only applies to the version of PHP used to execute PHPStan itself. The tool is capable of analyzing source files targeting older versions of PHP.
To start performing analysis on your code, require PHPStan in Composer, which is dependency manager for PHP.
composer require --dev phpstan/phpstan
Composer will install PHPStan’s executable in its
bin-dir which defaults to
The PHPStan binary will be added to your project at
vendor/bin/phpstan. You can now use it to analyze your codebase:
vendor/bin/phpstan analyse filename.php