PHP - Analyse statique de code

Comment analyser statiquement son code PHP et améliorer sa qualité et cohérence en réduisant les erreurs "évitables".

Outil phpstan

Prérequis :

  • interpréteur PHP
  • composer

Prérequis PHP

Installation via brew sous MacOS

1brew install php

Prérequis composer

Installation via brew sous MacOS

1brew install composer

Installation manuelle

1wget https://getcomposer.org/installer
2php installer
3mv composer.phar /usr/local/bin/composer
4rm installer

phpstan

Installation

À installer par projet via composer

1cd /codebase/php-project
2composer require --dev phpstan/phpstan

Usage

 1$ cd /codebase/php-project
 2$ ./vendor/bin/phpstan analyse --memory-limit 512M
 3> ./vendor/bin/phpstan analyse --memory-limit 512M
 4Note: Using configuration file /Users/gseznec/workspace/radio-captation/phpstan.neon.
 5 162/162 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
 6
 7 ------ --------------------------------------------------------------
 8  Line   src/Ina/Captation/AppAdm.php
 9 ------ --------------------------------------------------------------
10  480    PHPDoc tag @param references unknown parameter: $componentId
11 ------ --------------------------------------------------------------
12
13 ------ ----------------------------------------------------------------
14  Line   src/Ina/Modules/ModuleSubtitles.php
15 ------ ----------------------------------------------------------------
16  198    Method Ina\Modules\ModuleSubtitles::parseDABwrite() is unused.
17 ------ ----------------------------------------------------------------
18
19 ------ ------------------------------------
20  Line   src/Ina/Utils/Mediainfo.php
21 ------ ------------------------------------
22  234    Variable property access on mixed.
23 ------ ------------------------------------
24
25 ------ -------------------------------------------------------
26  Line   tools/ackwamann-capt-manu.php
27 ------ -------------------------------------------------------
28  24     Unreachable statement - code above always terminates.
29 ------ -------------------------------------------------------
30
31 ------ -------------------------------------------------------
32  Line   tools/ackwamann-sent-ack-manu.php
33 ------ -------------------------------------------------------
34  12     Unreachable statement - code above always terminates.
35 ------ -------------------------------------------------------
36
37
38 [ERROR] Found 5 errors
39
40
41Script ./vendor/bin/phpstan analyse --memory-limit 512M handling the stan event returned with error code 1

Configuration

Dans la racine du projet créer un fichier phpstan.neon

 1includes:
 2  - vendor/phpstan/phpstan-strict-rules/rules.neon
 3parameters:
 4  level: 6
 5  bootstrapFiles:
 6    - bootstrap.php
 7  paths:
 8    - .
 9  excludePaths:
10    - adm/templates_c
11    - node_modules
12    - vendor

Dans la racine du projet on peut créer un fichier composer.json

1{
2    "require-dev": {
3        "phpstan/phpstan": "1.10.38",
4    },
5    "scripts": {
6        "stan": "./vendor/bin/phpstan analyse --memory-limit 512M",
7    }
8}

Et appeler phpstan via la commande

1composer run stan

Ressources

comments powered by Disqus