17:54:55 15/2/2025 - 0 views
Setting Up PHPStan and Larastan in Your Laravel Project: A Comprehensive Guide
Static analysis tools are essential for maintaining robust and high-quality PHP code. In this post, we’ll walk you through setting up PHPStan and Larastan in your project, troubleshooting potential version compatibility issues, and configuring your analysis with a custom phpstan.neon file.
Installation
Installing PHPStan
Begin by installing PHPStan as a development dependency:composer require --dev phpstan/phpstan
Installing Larastan for Laravel
If you’re working with a Laravel project, you’ll also want to install Larastan to enhance PHPStan’s capabilities for Laravel-specific code:
composer require --dev nunomaduro/larastanHandling Version Compatibility Issues
In my case, while trying to add nunomaduro/larastan, I encountered compatibility issues. The root of the problem was a PHP version mismatch. As of February 15, 2025, PHP 8.2 is the latest version, and Larastan’s latest version is fully compatible with PHP 8.2. However, my project was running PHP 8.1, which resulted in errors.
After some research, I discovered the correct installation commands for PHP 8.1:
- Install PHPStan with dependencies and ignore the ext-exif platform requirement: composer require --dev phpstan/phpstan:^1.12 --with-all-dependencies --ignore-platform-req=ext-exif
- Install Larastan similarly: composer require --dev nunomaduro/larastan:^2.9 --ignore-platform-req=ext-exif --with-all-dependencies
Using these commands ensures that your project’s PHP version is respected and that all dependencies are compatible.
Configuring PHPStan
After installation, create a configuration file named phpstan.neon in your project’s root directory. For example, you might start with a simple configuration like this:parameters: level: max paths: - app/Console/Commands/
Including Larastan in the Configuration
If you are using Laravel and have installed Larastan, you can include its extension in your phpstan.neon file as follows:includes: - ./vendor/nunomaduro/larastan/extension.neon parameters: level: max paths: - app/Console/Commands/
This inclusion integrates Larastan’s Laravel-specific rules into PHPStan, providing more tailored analysis for your Laravel application.
Example and Error Message
Consider the following example class:
class ChangeFreq {
static const DAILY = 'daily';
static const MONTHLY = 'monthly';
static const WEEKLY = 'weekly';
}When you run PHPStan using the command:
./vendor/bin/phpstanYou might encounter an error similar to the following:
------ ------------------------------------------------------
Line GenerateSiteMap.php
------ ------------------------------------------------------
217 Cannot use 'static' as constant modifier on line 217
218 Cannot use 'static' as constant modifier on line 218
219 Cannot use 'static' as constant modifier on line 219
------ ------------------------------------------------------This error indicates that PHPStan has detected an incorrect use of the static keyword when defining class constants. It’s important to review your code and adjust the constant definitions to comply with PHP syntax.
Conclusion
Setting up PHPStan and Larastan can significantly enhance your code quality by catching errors early through static analysis. While installation is straightforward, be mindful of PHP version compatibility, especially when using Laravel and its associated tools. With the proper configuration in phpstan.neon and attention to error messages, you can ensure your PHP code remains clean, maintainable, and robust.
Happy coding!