Blog Detail

03

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

arrow_back 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

Requirements

  • PHP 7.2+
  • Laravel 6.0+

Installation

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.

Usage

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:

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

parameters:

    paths:
        - app

    # The level 8 is the highest level
    level: 5

    ignoreErrors:
        - '#Unsafe usage of new static#'

    excludePaths:
        - ./*/*/FileToBeExcluded.php

    checkMissingIterableValueType: false

If you want to know about all available options, then please visit PHPStan documentation:
https://phpstan.org/config-reference

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();

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

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

parameters:
    ignoreErrors:
        - '#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.

Rules

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
AUTHOR
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 your project

Launch project