Skip to content

phpstan

phpstan documentation

phpstan - GitHub

Configuration in Mega-Linter

Variable Description Default value
PHP_PHPSTAN_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
PHP_PHPSTAN_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src|lib)
Include every file
PHP_PHPSTAN_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test|examples)
Exclude no file
PHP_PHPSTAN_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".php"]
PHP_PHPSTAN_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
PHP_PHPSTAN_CONFIG_FILE phpstan configuration file name
Use LINTER_DEFAULT to let the linter find it
phpstan.neon
PHP_PHPSTAN_RULES_PATH Path where to find linter configuration file Workspace folder, then Mega-Linter default rules
PHP_PHPSTAN_DISABLE_ERRORS Run linter but consider errors as warnings false
PHP_PHPSTAN_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use phpstan in your favorite IDE to catch errors before Mega-Linter !

IDE Extension Name Install
IDEA PHPStan / Psalm / Generics
Visual Studio Code vscode-phpstan Install in VsCode

Mega-Linter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default Mega-Linter Flavor 84 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 41 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .php

Example calls

phpstan analyse --no-progress --no-ansi myfile.php
phpstan analyse --no-progress --no-ansi -c phpstan.neon myfile.php

Help content

Description:
  Analyses source code

Usage:
  analyse [options] [--] [<paths>...]
  analyze

Arguments:
  paths                                        Paths with source code to run analysis on

Options:
      --paths-file=PATHS-FILE                  Path to a file with a list of paths to run analysis on
  -c, --configuration=CONFIGURATION            Path to project configuration file
  -l, --level=LEVEL                            Level of rule options - the higher the stricter
      --no-progress                            Do not show progress bar, only results
      --debug                                  Show debug information - which file is analysed, do not catch internal errors
  -a, --autoload-file=AUTOLOAD-FILE            Project's additional autoload file path
      --error-format=ERROR-FORMAT              Format in which to print the result of the analysis
      --generate-baseline[=GENERATE-BASELINE]  Path to a file where the baseline should be saved [default: false]
      --memory-limit=MEMORY-LIMIT              Memory limit for analysis
      --xdebug                                 Allow running with XDebug for debugging purposes
      --fix                                    Launch PHPStan Pro
      --watch                                  Launch PHPStan Pro
      --pro                                    Launch PHPStan Pro
  -h, --help                                   Display this help message
  -q, --quiet                                  Do not output any message
  -V, --version                                Display this application version
      --ansi                                   Force ANSI output
      --no-ansi                                Disable ANSI output
  -n, --no-interaction                         Do not ask any interactive question
  -v|vv|vvv, --verbose                         Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Installation on mega-linter Docker image

  • Dockerfile commands :
# Parent descriptor install
RUN wget --tries=5 -q -O phive.phar https://phar.io/releases/phive.phar \
    && wget --tries=5 -q -O phive.phar.asc https://phar.io/releases/phive.phar.asc \
    && PHAR_KEY_ID="0x9D8A98B29B2D5D79" \
    && ( gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$PHAR_KEY_ID" \
        || gpg --keyserver pgp.mit.edu --recv-keys "$PHAR_KEY_ID" \
        || gpg --keyserver keyserver.pgp.com --recv-keys "$PHAR_KEY_ID" ) \
    && gpg --verify phive.phar.asc phive.phar \
    && chmod +x phive.phar \
    && mv phive.phar /usr/local/bin/phive \
    && rm phive.phar.asc

# Linter install
RUN composer global require phpstan/phpstan \
    && composer global config bin-dir --absolute

ENV PATH="/root/.composer/vendor/bin:$PATH"

Example success log

Results of phpstan linter (version 0.12.58)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/php_phpstan/
-----------------------------------------------

[SUCCESS] .automation/test/php/php_good_1.php

     [OK] No errors

[SUCCESS] .automation/test/php/php_good_2.php

     [OK] No errors

Example error log

Results of phpstan linter (version 0.12.58)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/php_phpstan/
-----------------------------------------------

[ERROR] .automation/test/php/php_bad_1.php
     ------ ---------------------------------------------- 
      Line   php_bad_1.php                                 
     ------ ---------------------------------------------- 
      3      Invalid numeric literal on line 3             
      3      Invalid numeric literal on line 3             
      3      Syntax error, unexpected '=' on line 3        
      3      Syntax error, unexpected T_LNUMBER on line 3  
      3      Syntax error, unexpected T_STRING on line 3   
      3      Syntax error, unexpected T_STRING on line 3   
     ------ ---------------------------------------------- 

     [ERROR] Found 6 errors                                                         

    ::error file=mp/lint/.automation/test/php/php_bad_1.php,line=3,col=0::Invalid numeric literal on line 3
    ::error file=mp/lint/.automation/test/php/php_bad_1.php,line=3,col=0::Invalid numeric literal on line 3
    ::error file=mp/lint/.automation/test/php/php_bad_1.php,line=3,col=0::Syntax error, unexpected '=' on line 3
    ::error file=mp/lint/.automation/test/php/php_bad_1.php,line=3,col=0::Syntax error, unexpected T_LNUMBER on line 3
    ::error file=mp/lint/.automation/test/php/php_bad_1.php,line=3,col=0::Syntax error, unexpected T_STRING on line 3
    ::error file=mp/lint/.automation/test/php/php_bad_1.php,line=3,col=0::Syntax error, unexpected T_STRING on line 3

[ERROR] .automation/test/php/php_bad_2.php
     ------ ----------------------------------------- 
      Line   php_bad_2.php                            
     ------ ----------------------------------------- 
      15     Syntax error, unexpected '}' on line 15  
     ------ ----------------------------------------- 

     [ERROR] Found 1 error                                                          

    ::error file=mp/lint/.automation/test/php/php_bad_2.php,line=15,col=0::Syntax error, unexpected '}' on line 15