nunomaduro/larastan
Larastan is a PHPStan extension for Laravel that adds strong type inference and “code analysis” by booting the app container. It understands Laravel’s magic, finds bugs early, and improves code quality and developer productivity.
Installation
composer require --dev larastan/larastan:^3.0
Ensure your project uses PHP 8.2+ and Laravel 11.15+.
Configure phpstan.neon
Create a minimal config file in your project root:
includes:
- vendor/larastan/larastan/extension.neon
- vendor/nesbot/carbon/extension.neon
parameters:
paths:
- app/
level: 5 # Start with level 5 (moderate strictness)
First Run Execute analysis:
./vendor/bin/phpstan analyse
For memory-intensive projects, increase memory:
./vendor/bin/phpstan analyse --memory-limit=2G
Larastan excels at detecting Laravel-specific issues like:
// ❌ Error: Call to undefined method `User::findByEmail()`
$user = User::findByEmail($email);
// ✅ Fixed: Use `where()` instead
$user = User::where('email', $email)->first();
Add Larastan to your GitHub Actions workflow:
- name: Run Larastan
run: ./vendor/bin/phpstan analyse --level=7
Tip: Use --error-format=github for PR-friendly output.
level: 5 (moderate) to avoid overwhelming the team../vendor/bin/phpstan analyse --generate-baseline
Commit phpstan.baseline.neon to track progress.Leverage Larastan’s Laravel-specific PHPDoc types:
/**
* @return \Illuminate\Support\Collection<int, \App\Models\User>
*/
public function getActiveUsers(): Collection
{
return User::active()->get();
}
enableMigrationCache in phpstan.neon to speed up analysis:
parameters:
larastan:
enableMigrationCache: true
NoMissingTranslationsRule to catch missing Blade translations:
parameters:
larastan:
rules:
- NoMissingTranslationsRule
Memory Limits
--memory-limit=4G or split analysis by directory:
./vendor/bin/phpstan analyse app/Http app/Models
False Positives in Magic Methods
Model::findOrFail()).phpstan.neon:
parameters:
ignoreErrors:
- '#Call to undefined method .*findOrFail\(\)#'
Migration Parsing Errors
phpmyadmin/sql-parser (GPL-licensed) for better support:
composer require --dev phpmyadmin/sql-parser
Template Types in Models
Model<T>) may cause errors.@template-extends in PHPDoc:
/**
* @template T of \Illuminate\Database\Eloquent\Model
* @property T $relation
*/
--verbose to debug rule application:
./vendor/bin/phpstan analyse --verbose
--focus to target specific files/classes:
./vendor/bin/phpstan analyse --focus=app/Http/Controllers/UserController.php
Custom Rules
Extend Larastan by creating a PHPStan rule and include it in extension.neon:
includes:
- vendor/larastan/larastan/extension.neon
- custom-rules.neon
Override Default Config
Use parameters.larastan to tweak behavior:
parameters:
larastan:
parseModelCastsMethod: true # Improves cast inference
enableMigrationCache: false # Disable if migrations are unstable
Stub Files
Add custom stubs for unsupported classes (e.g., third-party packages) in stubs/ and reference them in extension.neon:
services:
- MyCustomStub()
pestphp/pest: Use Larastan’s rules to guide test writing:
// Larastan catches this before you write tests:
// ❌ $user->role->permissions->where('id', 1); // role() may return null
collection-of Type
Reduce boilerplate for collections:
/**
* @return collection-of<\App\Models\User>
*/
public function search(string $query): Collection
./vendor/bin/phpstan analyse --count-errors
How can I help you explore Laravel packages today?