PHPStan is an open-source PHP Static Analysis tool. PHPStan is a must-have tool for any PHP project.
What is PHP Static Analysis
PHP Static Analysis tools will read your code and try to find errors before you execute the code.
Do not confuse Static Analysis with “syntax errors checking”.
PHP Static Analysis Common checks:
- undefined variable
- unknown class
- wrong method calling
- non-matching types (string, int, etc)
- invalid PHPDoc
- and many more
PHPStan in VSCode in common PHP projects
Just install PHPStan as a dev dependency of your project
composer require --dev phpstan/phpstan
Then install a VSCode extension for PHPStan. Just search in the extensions menu. I use this one, but you can use any other you like.
It’s so simple!
PHPStan in VSCode in WordPress
The “problem” with WordPress is that PHPStan does not recognize by default the WordPress functions.
Fortunately, you can use WordPress stubs to “teach” PHPStan to recognize the WordPress functions.
Even better, you can use the project WordPress Extensions for PHPStan which depends on both PHPStan and WordPress stubs. Just do:
composer require --dev szepeviktor/phpstan-wordpress
To force PHPStan to find this extension automatically, use:
composer require --dev phpstan/extension-installer
(or manually include it in your
phpstan.neon file in the root of your project as mentioned here).
includes: - vendor/szepeviktor/phpstan-wordpress/extension.neon
I prefer the first solution (
And of course, install a VSCode extension for PHPStan as mentioned in the previous paragraph.
- WordPress stubs on Github and Packagist
- WordPress extensions for PHPStan on Github and Packagist
Alternatives to PHPStan
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.