Blog Detail


Discover Bugs in Your Code Before Running It with Larastan cover image

Discover Bugs in Your Code Before Running It with Larastan

Can Vural and Nuno Maduro introduce Larastan that adds static analysis to Laravel and improves developer productivity and code quality. Larastan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code.

  • Adds static typing to Laravel to improve developer productivity and code quality
  • Supports most of Laravel's beautiful magic
  • Discovers bugs in your code without running it


  • PHP 7.2+
  • Laravel 6.0+


First, You have to use Composer to install Larastan as a development dependency into your Laravel project. Run this command:

composer require nunomaduro/larastan --with-dependencies --dev

If you want to use Larastan for analyzing Laravel packages. You may need to install orchestra/testbench.


After the installation, you've to create a phpstan.neon or phpstan.neon.dist file in the root of your application. It might look like this:

    - ./vendor/nunomaduro/larastan/extension.neon


        - app

    # The level 8 is the highest level
    level: 5

        - '#Unsafe usage of new static#'

        - ./*/*/FileToBeExcluded.php

    checkMissingIterableValueType: false

If you want to know about all available options, then please visit PHPStan documentation:

Finally, you can start analyzing your code using the phpstan console command:

./vendor/bin/phpstan analyse

If you are getting the error Allowed memory size exhausted, then you can utilize the --memory-limit option to fix the problem:

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

Ignoring errors

Ignoring a specific error can be done either with a php comment or in the configuration file:

// @phpstan-ignore-next-line

$test->badMethod(); // @phpstan-ignore-line

When ignoring errors in PHPStan's configuration file, they are ignored by writing a regex-based on error messages:

        - '#Call to an undefined method .*badMethod\(\)#'

Baseline file

In older codebases, it might be hard to employ the time fixing all the code to pass a high PHPStan Level.

To get around this a baseline file can be generated. The baseline file will create a configuration file with all of the current errors, so new code can be written following a higher standard than the old code. ( PHPStan Docs )

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

Errors-To Ignore

Some parts of Laravel are currently too mysterious for Larastan/PHPStan to understand. We listed common errors to ignore, add them as needed.


A list of configurable rules specific to Laravel can be found here.

For more details, Visit its documentation on Github;

Published at : 03-11-2021

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch project