19/06/2018 - PHING, PHP, SYMFONY
We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.
The aim of PHPStan is to scan application code to find errors without actually running it. Since PHP is not a compiled language it often tends to crash in production only when a code with errors gets used. PHPStan is used to find errors before code being deployed to production.
$ composer require --dev phpstan/phpstan to install the library.
By default you can start using it with
$ vendor/bin/phpstan analyse src --level max command. You can reduce "strictness" level as per your needs.
Sometimes you might need to create your own configuration file
phpstan.neon with certain rules. This is optional but let's see a few basics when it comes to working with one. You can check
vendor/phpstan/phpstan/conf folder for the actual rule files to see what they look like. Create "phpstan.neon" file in your project root with content below and run
$ vendor/bin/phpstan analyse src -c phpstan.neon command.
# Needed only when you use "rules" block below
# Sometimes needed
# Prevents scanning given folders/files
# You specific list of rules
If you are not manually requiring
phpunit/phpunit component in your composer.json file and relying on Symfony's PHPUnit Bridge instead, you will probably face similar errors as shown below.
Class PHPUnit\Framework\TestCase not found and could not be autoloaded.
Class [a test class of yours] was not found while trying to analyse it - autoloading is probably not configured properly.
Do the following to solve the first issue.
$ find -iname 'autoload.php'command to identify the path to the PHPUnit's autoloader file.
autoload_filessection of the PHPStan's configuration file. e.g.
Add code below to the
parameters section of the PHPStan's configuration file to solve the second issue.