Can Vural and Nuno Maduro introduce Larastan that adds static analysis to Laravel and improves developer productivity and code quality. Larastan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code.
First, You have to use Composer to install Larastan as a development dependency into your Laravel project. Run this command:
composer require nunomaduro/larastan --with-dependencies --dev
If you want to use Larastan for analyzing Laravel packages. You may need to install
After the installation, you’ve to create a
phpstan.neon.dist file in the root of your application. It might look like this:
includes: - ./vendor/nunomaduro/larastan/extension.neon parameters: paths: - app # The level 8 is the highest level level: 5 ignoreErrors: - '#Unsafe usage of new static#' excludePaths: - ./*/*/FileToBeExcluded.php checkMissingIterableValueType: false
If you want to know about all available options, then please visit
Finally, you can start analyzing your code using the phpstan console command:
If you are getting the error Allowed memory size exhausted, then you can utilize the
–memory-limit option to fix the problem:
./vendor/bin/phpstan analyse --memory-limit=2G
Ignoring a specific error can be done either with a
php comment or in the configuration file:
// @phpstan-ignore-next-line $test->badMethod(); $test->badMethod(); // @phpstan-ignore-line
When ignoring errors in PHPStan’s configuration file, they are ignored by writing a regex-based on error messages:
parameters: ignoreErrors: - '#Call to an undefined method .*badMethod\(\)#'
In older codebases, it might be hard to employ the time fixing all the code to pass a high
To get around this a baseline file can be generated. The baseline file will create a configuration file with all of the current errors, so new code can be written following a higher standard than the old code. ( PHPStan Docs )
./vendor/bin/phpstan analyse --generate-baseline
Some parts of Laravel are currently too mysterious for
Larastan/PHPStan to understand. We listed common errors to ignore, add them as needed.
A list of configurable rules specific to Laravel can be found here.
For more details, Visit its documentation on Github;
Published at : 03-11-2021
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.