phpstan
phpstan documentation
- Version in Mega-Linter: 1.0.0
- Visit Official Web Site
- See How to configure phpstan rules
- If custom
phpstan.neon.dist
config file is not found, phpstan.neon.dist will be used
- If custom
- See How to disable phpstan rules in files
Configuration in Mega-Linter
- Enable phpstan by adding
PHP_PHPSTAN
in ENABLE_LINTERS variable - Disable phpstan by adding
PHP_PHPSTAN
in DISABLE_LINTERS variable
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_CLI_LINT_MODE | Override default CLI lint mode - file : Calls the linter for each file- list_of_files : Call the linter with the list of files as argument- project : Call the linter from the root of the project |
{linter.cli_lint_mode} |
PHP_PHPSTAN_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".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_PRE_COMMANDS | List of bash commands to run before the linter | None |
PHP_PHPSTAN_POST_COMMANDS | List of bash commands to run after the linter | None |
PHP_PHPSTAN_CONFIG_FILE | phpstan configuration file nameUse LINTER_DEFAULT to let the linter find it |
phpstan.neon.dist |
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 |
Mega-Linter Flavours
This linter is available in the following flavours
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
all | Default Mega-Linter Flavor | 94 | ||
php | Optimized for PHP based projects | 44 |
Behind the scenes
How are identified applicable files
- File extensions:
.php
How the linting is performed
- phpstan is called once with the list of files as arguments
Example calls
phpstan analyse --no-progress --no-ansi myfile.php
phpstan analyse --no-progress --no-ansi -c phpstan.neon myfile.php
phpstan analyse --no-progress --no-ansi myfile1.php myfile2.php
phpstan analyse --no-progress --no-ansi mydir1/ mydir2/ myfile.php
Help content
Description:
Analyses source code
Usage:
analyse [options] [--] [<paths>...]
analyze
Arguments:
paths Paths with source code to run analysis on
Options:
-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 keyserver.pgp.com --recv-keys "$PHAR_KEY_ID" \
|| gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$PHAR_KEY_ID" \
|| gpg --keyserver pgp.mit.edu --recv-keys "$PHAR_KEY_ID" \
|| gpg --keyserver hkps://keyserver.ubuntu.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 phive --no-progress install phpstan -g --trust-gpg-keys CF1A108D0E7AE720
Example success log
Results of phpstan linter (version 0.12.58)
See documentation on https://megalinter.github.io/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://megalinter.github.io/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