How to set up PHPStan with Larastan in Laravel 13

Install Larastan in Laravel 13, write phpstan.neon.dist, pick a PHPStan level, choose a baseline or start green, and run it in composer and CI.

Updated

On this page

To install Larastan in Laravel 13, run composer require --dev larastan/larastan, create a phpstan.neon.dist that includes vendor/larastan/larastan/extension.neon, list your paths, set a level, and run vendor/bin/phpstan analyse. Larastan v3 is the PHPStan 2 extension for Laravel. It understands facades, Eloquent builders, relations, the container and model properties, so PHPStan reports real bugs instead of noise about Laravel magic.

This guide covers the config, choosing a level, baseline versus starting green, IDE Helper, composer scripts, CI and the errors you will hit first. Commands assume Sail.

Install Larastan

./vendor/bin/sail composer require --dev larastan/larastan

This pulls in phpstan/phpstan as a dependency. You do not require PHPStan separately. Larastan 3 needs PHP 8.2+ and Laravel 11.16+, so any Laravel 13 app qualifies.

If laravel new or your starter kit left a phpstan.neon in the project root, delete it or merge it into the file below. PHPStan loads phpstan.neon before phpstan.neon.dist, so a leftover phpstan.neon silently replaces your config.

Write phpstan.neon.dist

includes:
    - vendor/larastan/larastan/extension.neon
    - vendor/nesbot/carbon/extension.neon

parameters:
    level: 7

    # Laravel documents some runtime values more narrowly than they are.
    # $request->user() is typed to your own User model, so a defensive check
    # reads as impossible. Treat PHPDoc as a hint, not proof.
    treatPhpDocTypesAsCertain: false

    paths:
        - app
        - bootstrap/app.php
        - config
        - database
        - routes

What each part does:

Key Purpose
extension.neon Loads Larastan's rules and type extensions
Carbon extension.neon Teaches PHPStan Carbon's macros and magic methods
level How strict the analysis is, 0 to 10
treatPhpDocTypesAsCertain Stops "always true" errors on defensive checks against PHPDoc types
paths What to analyse

Use the .dist name and commit it. A developer can then create a local, gitignored phpstan.neon that includes the .dist file and overrides a setting, without touching the shared config.

On paths: list app, config, database, routes and bootstrap/app.php. Leave out bootstrap/cache, storage and vendor. Add a custom source directory such as src only when it exists. A path that does not exist on disk is a config error, and under Docker it can show up as an exit code with no message at all. See PHPStan exits with no output.

Choose a level

PHPStan has levels 0 to 10. Each level includes everything below it.

Level Adds, roughly
0–2 Unknown classes, functions and methods; wrong argument counts; undefined variables
3–4 Return types, property types, dead branches
5 Argument types passed to methods and functions
6 Missing typehints
7 Partially wrong union types
8 Calling methods on nullable types
9 Strict about explicit mixed
10 Strict about implicit mixed too

How to pick:

  • New Laravel 13 app: start at 7 or 8. The skeleton is small and passes with little or no work. Raising the level later on a large codebase is far harder than starting high.
  • Existing app: run level 5 first, see the error count, then decide between fixing and a baseline.
  • Levels 9 and 10: expect to fight mixed from config(), env(), request input and decoded JSON. Worth it for libraries and domain code. For a typical app, 7 or 8 is the practical ceiling.

Try a level without editing the file:

./vendor/bin/sail bin phpstan analyse --level=8

Baseline or start green

A baseline records every current error in a file, and PHPStan ignores exactly those errors from then on. New code is checked at full strictness.

./vendor/bin/sail bin phpstan analyse --generate-baseline

This writes phpstan-baseline.neon. Include it:

includes:
    - vendor/larastan/larastan/extension.neon
    - vendor/nesbot/carbon/extension.neon
    - phpstan-baseline.neon

When to use which:

Situation Approach
Fresh app, a handful of errors Fix them. Start green with no baseline
Existing app, hundreds of errors Baseline, then shrink it over time
Errors in published vendor stubs Fix the stub; it is your code now

A baseline on a brand-new app is a concession on day one. Every entry in it is a known bug nobody will look at again. On a fresh skeleton the few errors that appear usually come from published stubs, such as a starter kit's validation trait with a @return type that is too narrow. Fix those files directly.

When you fix a baselined error, PHPStan reports the stale baseline entry as unmatched. Regenerate the baseline so it only shrinks.

Add IDE Helper

./vendor/bin/sail composer require --dev barryvdh/laravel-ide-helper

Larastan already understands most Laravel magic on its own. IDE Helper is mostly for your editor and AI agents: autocompletion for facades, container bindings and model attributes. Its model PHPDoc also gives PHPStan precise types for columns.

Generate the files and gitignore them:

./vendor/bin/sail artisan ide-helper:generate
./vendor/bin/sail artisan ide-helper:meta
./vendor/bin/sail artisan ide-helper:models --nowrite
_ide_helper.php
_ide_helper_models.php
.phpstorm.meta.php

--nowrite writes model docs to _ide_helper_models.php instead of editing your model classes. ide-helper:models reads the live schema, so the database must be migrated and running.

Composer scripts

{
    "scripts": {
        "phpstan": "vendor/bin/phpstan analyse",
        "phpstan-clear": "vendor/bin/phpstan clear-result-cache",
        "ide-helper": [
            "@php artisan ide-helper:generate",
            "@php artisan ide-helper:meta",
            "@php artisan ide-helper:models --nowrite"
        ],
        "quality": [
            "@pint:dry",
            "@phpstan",
            "@rector:dry"
        ]
    }
}
./vendor/bin/sail composer phpstan
./vendor/bin/sail composer quality

PHPStan caches results between runs. If it reports errors that are already fixed, or misses new ones after a config or dependency change, run composer phpstan-clear.

Run it in CI

name: PHPStan

on:
  push:
  pull_request:

jobs:
  phpstan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.5'
          coverage: none

      - run: composer install --no-interaction --prefer-dist

      - run: vendor/bin/phpstan analyse --no-progress --error-format=github

--error-format=github turns errors into inline annotations on the pull request. Use the same PHP version you pinned in composer.json under config.platform.php. If you run the whole composer quality in CI instead, the --error-format flag is not needed.

Common errors

Allowed memory size exhausted

PHPStan's default memory limit is too low for larger apps.

./vendor/bin/sail bin phpstan analyse --memory-limit=2G

Put the flag in the composer script so everyone gets it.

Access to an undefined property on a model

Larastan reads model properties from migrations and casts. If a property comes from somewhere it cannot see, add a @property PHPDoc on the model or generate _ide_helper_models.php.

Method not found on a relation or builder

Usually a missing generic. Type relations explicitly:

/**
 * @return HasMany<Post, $this>
 */
public function posts(): HasMany
{
    return $this->hasMany(Post::class);
}

Return type has no value type specified in iterable type array

Level 6 and up want array shapes. Describe them in PHPDoc:

/**
 * @return array<string, mixed>
 */
public function toArray(): array

Exit code 1 with no output

Check that every path in phpstan.neon.dist exists. See PHPStan exits with no output.

Your config seems ignored

Look for a phpstan.neon next to phpstan.neon.dist. The non-dist file wins.

Next steps

PHPStan finds problems. Rector fixes many of them mechanically and Pint formats the result. Set those up next: Rector for Laravel and Laravel Pint config. For the full list of fresh-project steps, see the Laravel 13 setup checklist.

Skip the setup

kalimera installs Larastan and IDE Helper on every new app, publishes the phpstan.neon.dist above at level 7, removes a skeleton phpstan.neon that would shadow it, gitignores the IDE Helper files, adds the phpstan, phpstan-clear, ide-helper and quality scripts, and repairs the published stubs that would fail the analysis, so the first commit is green without a baseline.

All of it, in one unattended run.

kalimera makes these choices once, upfront, then scaffolds a fresh Laravel app with every piece wired and committed.

$ composer global require welldigit/kalimera

Read the docs