How to use the command 'phpstan' (with examples)
PHPStan is a powerful static analysis tool for PHP which helps developers discover and fix errors in their code. It analyzes the codebase and provides feedback on potential bugs, type errors, and other issues. This article will illustrate several use cases of the phpstan
command to help you get started with this valuable tool.
Use case 1: Display available options for analysis
Code:
phpstan analyse --help
Motivation:
Understanding the available options and arguments for the phpstan
command is crucial for efficient code analysis. By using the --help
option, you can quickly get an overview of all the available options and their functionalities.
Explanation:
phpstan
: The command itself.analyse
: The action to perform, which in this case is analysis.--help
: The option to display the available options and their functionalities.
Example output:
Usage:
phpstan analyse [options] [--] [<directories>...]
Options:
-c, --configuration <file> Path to the configuration file.
-l, --level <level> Rule level (0-7, higher is stricter).
-a, --autoload-file <file> Autoload file to load.
-m, --memory-limit <limit> Memory limit, can be in bytes or using shorthand notation. (default: "1G")
-l, --path-mode <mode> Specify path mode - how paths passed to a current command should be treated. (only_paths: "only paths are analyzed", only_files: "only files are analyzed") (default: "only_paths")
-f, --error-format <format> Format of the error output. (default: "pretty") [default: pretty]
-h, --help Display help for the given command. When no command is given display help for the analyse command.
-q, --quiet Do not output any message.
-V, --version Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
-n, --no-progress Do not advance progress bar.
-t, --error-separator <sep> Separator string between the project root path and the filepath of an error.
-x, --xsl <file> Path to a stylesheet in XSL. Optionally, you can specify - to output the result in raw XML. [default: phpstan.xsl]
Use case 2: Analyze the specified space-separated directories
Code:
phpstan analyse path/to/directory
Motivation: By analyzing specific directories, you can focus the static analysis on specific parts of your codebase, ensuring that the analysis is performed on relevant code files only.
Explanation:
phpstan
: The command itself.analyse
: The action to perform, which in this case is analysis.path/to/directory
: The directories to be analyzed. Multiple directories can be specified, separated by spaces.
Example output:
Analyzing path/to/directory
No errors found!
Use case 3: Analyze a directory using a configuration file
Code:
phpstan analyse path/to/directory --configuration path/to/config
Motivation: By utilizing a configuration file, you can provide custom rulesets, ignore specific directories, and define other analysis settings tailored to your project’s needs.
Explanation:
phpstan
: The command itself.analyse
: The action to perform, which in this case is analysis.path/to/directory
: The directory to be analyzed.--configuration
: The option to specify the path to the configuration file.
Example output:
Analyzing path/to/directory with configuration file path/to/config
No errors found!
Use case 4: Analyze using a specific rule level
Code:
phpstan analyse path/to/directory --level level
Motivation: Different projects might have different requirements for strictness. By specifying a rule level, you can adjust the strictness of the analysis according to your project’s needs.
Explanation:
phpstan
: The command itself.analyse
: The action to perform, which in this case is analysis.path/to/directory
: The directory to be analyzed.--level
: The option to specify the rule level. The allowed values range from 0 to 7, where higher levels are stricter.
Example output:
Analyzing path/to/directory with rule level 5
No errors found!
Use case 5: Specify an autoload file to load before analyzing
Code:
phpstan analyse path/to/directory --autoload-file path/to/autoload_file
Motivation: Some projects require specific autoload files to properly analyze dependencies. By specifying an autoload file, you ensure that the necessary dependencies are loaded before performing the analysis.
Explanation:
phpstan
: The command itself.analyse
: The action to perform, which in this case is analysis.path/to/directory
: The directory to be analyzed.--autoload-file
: The option to specify the path to the autoload file.
Example output:
Analyzing path/to/directory with autoload file path/to/autoload_file
No errors found!
Use case 6: Specify a memory limit during analysis
Code:
phpstan analyse path/to/directory --memory-limit memory_limit
Motivation: For large codebases or resource-intensive systems, it might be necessary to allocate more memory for the analysis process. By specifying a memory limit, you can control the amount of memory allocated to the analysis.
Explanation:
phpstan
: The command itself.analyse
: The action to perform, which in this case is analysis.path/to/directory
: The directory to be analyzed.--memory-limit
: The option to specify the memory limit. The limit can be specified in bytes or using shorthand notation.
Example output:
Analyzing path/to/directory with memory limit of 2G
No errors found!
Conclusion:
The phpstan
command is a versatile and powerful tool for analyzing PHP code. By exploring its various options and arguments, you can customize the analysis process to suit your project’s requirements. Whether you want to check specific directories, adjust the strictness of the analysis, or load custom autoload files, phpstan
provides the flexibility to enhance the quality and reliability of your codebase.