PHPStan Static Code Analysis

Table of contents

Loading...

Introduction

PHPStan is a static code analysis tool that helps find bugs in the code without running it.

Among other things, PHPStan looks for:

Installation

PHPStan can be installed as a development dependency using Composer:

composer require --dev phpstan/phpstan

Configuration

The configuration of PHPStan is done in the phpstan.neon file in the root directory of the project. This file contains the paths to the directories that should be analyzed, special rules and the level of strictness that should be applied.

File .phpstan.neon

parameters:
    level: 8
    paths:
        - src
        - tests
    ignoreErrors:
        - identifier: missingType.iterableValue

Please refer to the PHPStan documentation to learn how to ignore errors and suppress specific warnings.

Composer command

To run PHPStan with a short command, add the following command to the scripts section of the composer.json file:

"scripts": {
  "stan": "phpstan analyse -c phpstan.neon --no-progress --ansi",
}

Usage

To run PHPStan, execute the following command in the terminal:

composer stan
^