Nunomaduro/larastan is a powerful tool that can be used to identify errors in your Laravel code without actually running the code. Acting as a wrapper for PHPStan, larastan focuses on analyzing your code to uncover potential issues before you even write tests for it. By incorporating static typing into Laravel, the tool aids in improving your code's quality and the overall productivity of the developer. It is capable of catching entire classes of bugs, uncovering hidden errors in your codebase, and supports most of Laravel's magic.
In order to use nunomaduro/larastan, you should first install the package using Composer as a development dependency. Here's a simple example of how you can do it:
composer require nunomaduro/larastan:^2.0 --dev
After this, you need to create a phpstan.neon
or phpstan.neon.dist
file in your project's root directory with configurations as shown below:
includes:
- ./vendor/nunomaduro/larastan/extension.neon
parameters:
paths:
- app/
level: 5
You can then start analyzing your code using the phpstan console command:
./vendor/bin/phpstan analyse
If you encounter a memory error, you can specify a memory limit to fix the issue:
./vendor/bin/phpstan analyse --memory-limit=2G
In addition to this basic usage, larastan provides ways to ignore specific errors and set up a baseline file for large code bases where fixing all errors is time-consuming.
The comprehensive documentation for nunomaduro/larastan is available directly in the GitHub repository. This includes details on configurable rules, features, custom PHPDoc types, custom config parameters, and common errors to ignore. For more in-depth information, you can refer to the official PHPStan documentation.