Recently, I found that the running results of THE PHP code I wrote were always different from what I expected. Most of them were found to be syntax errors, which had been planted before running. Maybe you were careless, or maybe phP-L detection was too easy, but some syntax errors are buried too deep (PHP is a dynamic language, after all), so is there a way to catch them all before the code actually runs?

Here is a PHP code static analysis tool: PHPStan, do not need to run the code, also can be strict syntax detection of the code, as far as possible to minimize the error rate of the code run.

PHPStan

The installation

Currently, PHPStanV0.10.2 requires the system environment to be at least version 7.1 of PHP. Install globally with Composer:

$ composer global require phpstan/phpstan
Copy the code

use

The use of PHPStan static analysis is simple:

$ phpstan analyse [-c|--configuration CONFIGURATION]
[-l|--level LEVEL] [--no-progress] [--debug]
[-a|--autoload-file AUTOLOAD-FILE] [--errorFormat ERRORFORMAT]
[--memory-limit MEMORY-LIMIT] [--] [<paths>]...
Copy the code
  • Configuration: Path to run the configuration file.
  • Level: indicates the severity level. The value ranges from 0 to 7.
  • No-progress: no progress is displayed.
  • Debug: Indicates the debug mode.
  • Autoload-file: indicates the path for automatically loading files.
  • ErrorFormat: indicates an errorFormat.
  • Memory-limit: indicates the memory limit.
  • Paths: File path to be analyzed.

For example, parsing a PHP file:

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

PHPStan in VSCode

Of course, parsing should be done by the editor, and switching to the command terminal to execute PHPstan would be too tedious. PHP Static Analysis is recommended as a VSCode extension.

First, install PHPStan globally with Composer; Then, search PHP Static Analysis in VSCode’s extension management and install the first matching extension; After reloading the window, the extension will automatically analyze PHP files opened under VSCode.

Operation effect:

For example, declaring a variable without calling it, calling an undeclared variable, calling an undefined method, and so on all errors are detected.

$this->array(); $this->array();

PHPStan with Laravel

High levels of rigor in PHPStan detect calls to undeclared class methods and report errors where methods in the class do not exist, even if the class defines __call() or __callStatic().

Many application frameworks make extensive use of magic methods for the sake of elegance, such as Laravel. Checking Laravel projects with PHPStan will naturally report many errors calling undeclared class methods. For this problem, laravel-ID-Helper can be used to reduce false positives.

Install laravel – ide – helper

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

Injection LaravelIdeHelper

In the editing app/will/AppServiceProvider. PHP registration method:


      .public function register(a)
    {
        if ($this->app->environment() ! = ='production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
        // ...
    }
Copy the code

Generate _ide_helper. PHP

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

At this point, the Facade class in the Laravel framework, which originally gets static methods from __callStatic(), is all declared in _ide_helper.php. The _ide_helper.php file is introduced when PHPStan checks the Laravel project code to 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
Copy the code

The phpstan.neon configuration is automatically used when the phpstan command is executed in the root directory of the Laravel project.

The last

Syntax errors in code should be detected at the time of writing to minimize formal runtime errors.