phpstan-laravel¶
Static analysis that understands Laravel.
Laravel leans on magic: facades, container bindings, dynamic Eloquent properties, method forwarding, macros. A static analyser sees almost none of it on its own, so the parts of your application that carry the most behaviour are the parts it checks least.
This extension closes that gap. It boots your application during analysis and combines that with stubs, reflection extensions and schema scanning, so PHPStan can reason about your models, relations, collections, configuration and views:
$user = User::query()->firstOrFail(); // App\User
$user->email; // string
$user->emial; // Access to an undefined property
$user->accounts; // App\AccountCollection<int, App\Account>
User::query()->pluck('name'); // Collection<int, string>
$user->accounts()->pluck('name'); // Collection<int, string>
User::all()->groupBy('email'); // Collection<string, Collection<int, App\User>>
config('auth.defaults.guard'); // string|null
Config::string('auth.defaults.guard'); // string
User::create(['emial' => '...']); // Property 'emial' does not exist
Every type above is what the analyser actually reports, not an aspiration.
-
Eloquent that type checks
Columns come from your migrations and schema dumps, per connection. Casts, appends, dates and traits are read from a real model instance, so anything a trait contributes is visible.
-
Typed configuration
config()returns array shapes built from your own config files, and the typed accessors are checked against them. -
Laravel-specific rules
Nineteen rules for mistakes the framework will happily let you make at runtime, each behind its own error identifier.
-
Laravel-aware types
view-stringverifies a Blade view exists andmodel-property<Model>verifies a column exists, both applied throughout the core stubs.
Install¶
With the PHPStan extension installer that is the whole setup. See installation for the manual include and for projects that use squashed schema dumps.
Where to go next¶
-
New here
Installation then configuration. If you only turn on one thing, make it
checkModelProperties. -
Coming from Larastan
The migration guide covers the mechanics, and differences from Larastan covers why you might bother.
-
Chasing an error
Every error carries an identifier. Look it up in the identifier reference, or read troubleshooting for the known limits.
-
Analysing a package
A package has no application to boot. Analysing a package covers what changes.