Free 20-minute strategy call with a senior engineer: AI, payments, eLearning or custom software
Engineering

How to Get Your Laravel App from 0 to 9 with Larastan

How to Get Your Laravel App from 0 to 9 with Larastan

In short

Install and configure Larastan for Laravel: phpstan.neon setup, rule levels, baselines, common errors and fixes, and a step-by-step plan to reach level 9.

Code quality becomes harder to maintain as a Laravel application grows. Larastan helps by bringing static analysis to Laravel: it finds bugs, type errors and dead code before they reach production, without running your code. This guide shows how to install and configure Larastan, understand its rule levels, fix the most common errors and move your Laravel app step by step from level 0 to the strictest levels.

Quick answer: install Larastan with composer require --dev larastan/larastan, create a phpstan.neon file that includes vendor/larastan/larastan/extension.neon, start at a low level (such as 0 to 5), generate a baseline for existing errors, fix issues gradually, and raise the level over time. Larastan 3 runs on PHPStan 2, which has levels 0 to 10.

1. What is Larastan?

Larastan is a PHPStan extension for Laravel. PHPStan analyses PHP code without executing it; Larastan teaches it how Laravel works, including facades, the service container, Eloquent models and relationships, collections and magic methods. Without Larastan, PHPStan reports many false positives in Laravel code.

Larastan was originally published as nunomaduro/larastan; the current package is larastan/larastan.

2. Why use Larastan in your Laravel application?

  • Catch bugs early: undefined methods and properties, wrong argument types and impossible conditions.
  • Improve type safety: encourages accurate return types, parameter types and PHPDoc generics.
  • Safer refactoring and upgrades: shows exactly what breaks when you change code or upgrade Laravel or PHP.
  • Consistent code reviews: automated checks in CI so reviewers can focus on logic.
  • Better IDE support: the same type information helps your editor’s autocompletion.

3. Install Larastan

Larastan installs PHPStan as a dependency, so one command is enough:

composer require --dev larastan/larastan

Check the Larastan README for the minimum PHP and Laravel versions supported by the current release.

4. Configure Larastan

Create a phpstan.neon (or phpstan.neon.dist) file in your project root:

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

parameters:
    paths:
        - app/
    level: 5
  • includes: loads Larastan’s Laravel-specific extension.
  • paths: the directories to analyse. Add config/, database/, routes/ or modules as needed.
  • level: how strict the analysis is.

If you are upgrading from an older setup, replace ./vendor/nunomaduro/larastan/extension.neon with the new path and update the Composer package name.

Free resource

Upgrade and patching checklist

Want this applied to your project? Our engineers will send it with notes for your stack.

Get it free

5. Understanding Larastan levels

PHPStan rule levels go from 0 (least strict) to 9, and to 10 in PHPStan 2. Each level includes all the checks from the levels below it.

LevelWhat it adds (simplified)
0Basic checks: unknown classes, functions and methods, wrong argument counts, undefined variables
1Possibly undefined variables, unknown magic methods and properties
2Unknown methods on all expressions, PHPDoc validation
3Return types and property types
4Dead code and always-true or always-false conditions
5Argument types passed to methods and functions
6Missing type declarations
7Partially wrong union types
8Calling methods or accessing properties on nullable types
9Strict handling of mixed
10Even stricter handling of implicit mixed (PHPStan 2)

6. Running Larastan

vendor/bin/phpstan analyse

Override the level for a one-off run:

vendor/bin/phpstan analyse --level=6

If PHPStan runs out of memory on large projects, add --memory-limit=1G.

7. Getting from level 0 to 9: a practical plan

  1. Start low. Run at level 0 and fix everything; these are usually real bugs.
  2. Generate a baseline so existing errors at your target level do not block new work:
vendor/bin/phpstan analyse --generate-baseline

Then include phpstan-baseline.neon in your configuration. New code must pass; old errors are fixed gradually.

  1. Raise the level one step at a time, fixing new errors and regenerating the baseline.
  2. Add types as you go: return types, parameter types, typed properties and generics for collections and relationships.
  3. Run Larastan in CI on every pull request so the codebase never slides back.
  4. Shrink the baseline over time until it is empty.

8. Common Larastan errors and fixes

  • “Call to an undefined method”: often a missing return type or a custom query builder method. Add return types, use @method annotations or Larastan’s custom builder support.
  • “Access to an undefined property” on models: Larastan reads your migrations to understand model columns. Make sure migrations are in the analysed paths or document properties with @property PHPDoc.
  • Relationship and collection types: add generic PHPDoc such as @return HasMany<Post, $this> or @return Collection<int, User>.
  • Nullable values at level 8: handle null explicitly, for example after find() or first(), or use findOrFail().
/** @var \App\Models\User|null $user */
$user = User::find($id);

if ($user === null) {
    abort(404);
}

9. Advanced configuration

Ignoring specific errors

Ignore an error only when you are sure it is a false positive:

parameters:
    ignoreErrors:
        -
            identifier: method.notFound
            path: app/Legacy/*

Ignoring by error identifier and path is more precise than regular expressions.

Custom rules and extensions

You can add other PHPStan extensions (for example strict rules or deprecation rules) or write your own rule classes to enforce team conventions.

10. Best practices for using Larastan with Laravel

  • Commit phpstan.neon and the baseline so every developer runs the same checks.
  • Run Larastan locally before pushing, and always in CI.
  • Pair it with Laravel Pint for code style and Pest or PHPUnit for tests.
  • Upgrade Larastan alongside Laravel so the analysis understands new framework features.
  • Treat the baseline as technical debt to pay down, not a permanent ignore list.

Frequently asked questions

What is the difference between PHPStan and Larastan? PHPStan is the static analyser; Larastan is an extension that makes PHPStan understand Laravel’s facades, Eloquent and other framework features.

Which Larastan level should I use? Start at the highest level your code passes, then increase gradually. Many teams aim for level 6 to 8; level 9 or 10 is achievable for well-typed codebases.

What is the new Larastan package name? larastan/larastan. The older nunomaduro/larastan package has been replaced.

Will Larastan slow down development? Not once it runs in CI with a baseline. It catches bugs earlier, which saves time in testing and production support.

Level up your Laravel app with Vibidsoft Pvt Ltd

Vibidsoft Pvt Ltd improves Laravel code quality with static analysis, automated tests, refactoring and upgrades to current Laravel and PHP versions. See our Laravel development services or read about the best Laravel development tools in 2026.

Vibidsoft Engineering Team

Senior engineers who have shipped 450+ projects since 2014 across AI, payments, eCommerce, eLearning and custom software. Every guide is written and reviewed by the people who do the work.

Follow us on LinkedIn · About Vibidsoft

Next step

Talk to an engineer about Modernize & Rescue

Fixed-price upgrades for ageing PHP, Drupal, Magento or hybrid apps, with security patching and a care plan. from $5,000.

  • A 20-minute call with a senior engineer, not a salesperson
  • A written next-step plan within one business day
  • NDA on request
Book a call WhatsApp