Home  >  Article  >  Backend Development  >  Use PHPStan to enhance PHP code quality

Use PHPStan to enhance PHP code quality

Guanhui
Guanhuiforward
2020-06-08 09:14:204856browse

Use PHPStan to enhance PHP code quality

Here we introduce a PHP code static analysis tool: PHPStan, which does not require running the code. It can also perform strict syntax detection on the code and try to minimize the code running error rate.

PHPStan

Installation

Currently, PHPStanV0.10.2 requires a system environment PHP version is no less than 7.1. Install globally with Composer:

$ composer global require phpstan/phpstan

Use

The method of using PHPStan static analysis is very simple:

$ phpstan analyse [-c|--configuration CONFIGURATION] [-l|--level LEVEL] [--no-progress] [--debug] [-a|--autoload-file AUTOLOAD-FILE] [--errorFormat ERRORFORMAT] [--memory-limit MEMORY-LIMIT] [--] [<paths>]...
  • configuration: run Path to the configuration file;

  • level: strict level, 0-7, the larger, the more stringent;

  • no-progress: Do not display progress;

  • debug: debug mode;

  • autoload-file: the path to automatically load the file;

  • errorFormat: Error format;

  • memory-limit: Memory limit;

  • paths: File paths to be analyzed.

For example, analyze a PHP file:

$ phpstan analyse --level=7 --autoload-file=/PATH/TO/vendor/autoload.php /PATH/TO/someone.php

PHPStan in VSCode

Of course, the syntax analysis should be done by the editor After writing the code, it is too cumbersome to switch to the command terminal to execute phpstan. So here is a VSCode extension recommended: PHP Static Analysis.

PHP Static Analysis

Use PHPStan to enhance PHP code quality

First, use Composer to install PHPStan globally; then, search for PHP Static Analysis in the extension management of VSCode , install the first matching extension; after reloading the VSCode reload window, the extension will automatically analyze the PHP file opened under VSCode.

Running effect:

Use PHPStan to enhance PHP code quality

For example, a variable is declared but not called, an undeclared variable is called, an undefined method is called, etc. In this way, errors will be detected.

However, to put it loosely, the $this->array() method actually exists, but it is only implemented through the magic method __call().

PHPStan with Laravel

High strict level PHPStan detects that when an undeclared class method is called, it will report an error that the method in the class does not exist, even if the class is defined __call() or __callStatic().

Many application frameworks use a lot of magic methods for elegance, such as Laravel.

Using PHPStan to detect Laravel projects will naturally report many errors in calling undeclared class methods. For this problem, you can use laravel-ide-helper to reduce false positives.

Install laravel-ide-helper

$ cd /PATH/TO/LARAVEL_PROJECT
$ composer require barryvdh/laravel-ide-helper

Inject LaravelIdeHelper

Edit the registration in app/Providers/AppServiceProvider.php Method:

<?php
    ...
    public function register()
    {
        if ($this->app->environment() !== &#39;production&#39;) {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
        // ...
    }

Generate _ide_helper.php

$ cd /PATH/TO/LARAVEL_PROJECT
$ php artisan ide-helper:generate

At this time, the Facade class in the Laravel framework and the static methods originally obtained through __callStatic() are all declared in _ide_helper.php , introducing the _ide_helper.php file when PHPStan detects Laravel project code can reduce false positives.

PHPStan configuration

In the root directory of the Laravel project, create a new phpstan.neon file:

parameters:
    autoload_files:
        - %currentWorkingDirectory%/_ide_helper.php

In the root directory of the Laravel project, execute When running the phpstan command, the configuration phpstan.neon will be automatically used.

Recommended tutorial: "PHP"

The above is the detailed content of Use PHPStan to enhance PHP code quality. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete