Skip to content

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.

    Model properties

  • Typed configuration


    config() returns array shapes built from your own config files, and the typed accessors are checked against them.

    Configuration types

  • Laravel-specific rules


    Nineteen rules for mistakes the framework will happily let you make at runtime, each behind its own error identifier.

    Rules

  • Laravel-aware types


    view-string verifies a Blade view exists and model-property<Model> verifies a column exists, both applied throughout the core stubs.

    Custom types

Install

composer require --dev calebdw/phpstan-laravel

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