PHPStan
focuses on finding errors in your code. It catches whole classes of bugs even before you write tests for the code.
- Adds static typing to Laravel to improve developer productivity and code quality
- Supports most of Laravel's beautiful magic
- Discovers bugs in your code
Larastan is a phpstan/phpstan
wrapper for Laravel. Discover bugs in your code without running it.
Installation
composer require nunomaduro/larastan:^2.0 --dev
Configure
touch phpstan.neon
includes:
- ./vendor/nunomaduro/larastan/extension.neon
parameters:
paths:
- app
# The level 9 is the highest level
level: 5
ignoreErrors:
excludePaths:
- ./*/*/FileToBeExcluded.php
checkMissingIterableValueType: false
Add format
script
Update composer.json
, add a format
script
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
}
More
Ignoring errors
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:
file: phpstan.neon
parameters:
ignoreErrors:
- '#Call to an undefined method .*badMethod\(\)#'
Run analyse
composer analyse
Further (Optional)
Try levelling up the stan level from 5 to 6 and fix the code
# from
level: 5
# to
level: 6
Run analyse
composer analyse
Happy Coding!
Top comments (0)