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

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 83 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 40 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:
  Lists commands

Usage:
  list [options] [--] [<namespace>]

Arguments:
  namespace            The namespace name

Options:
      --raw            To output raw command list
      --format=FORMAT  The output format (txt, xml, json, or md) [default: "txt"]

Help:
  The list command lists all commands:

    php /root/.composer/vendor/bin/phpstan list

  You can also display the commands for a specific namespace:

    php /root/.composer/vendor/bin/phpstan list test

  You can also output the information in other formats by using the --format option:

    php /root/.composer/vendor/bin/phpstan list --format=xml

  It's also possible to get raw list of commands (useful for embedding command runner):

    php /root/.composer/vendor/bin/phpstan list --raw

Installation on mega-linter Docker image

  • Dockerfile commands :
# Parent descriptor install
RUN wget --tries=5 -O phive.phar https://phar.io/releases/phive.phar \
    && wget --tries=5 -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