phpmd
Analyze PHP code for potential problems
TLDR
Display a list of available rulesets and formats
Scan a file or directory for problems using comma-separated rulesets
Specify the minimum priority threshold for rules
Include only the specified extensions in analysis
Exclude the specified comma-separated directories
Output the results to a file instead of stdout
Ignore the use of warning-suppressive PHPDoc comments
SYNOPSIS
phpmd source format ruleset [options]
Where:
source: A comma-separated list of files and/or directories to analyze. Example: `src/,tests/unit/MyClass.php`
format: The format of the report. Common formats include `xml`, `text`, `html`, `json`, `gitlab`, `github`, `checkstyle`.
ruleset: A comma-separated list of built-in ruleset names or paths to custom XML ruleset files. Example: `cleancode,codesize,unusedcode` or `/path/to/my_rules.xml`
PARAMETERS
source
A comma-separated list of files and/or directories to analyze (required).
format
The output format of the report (e.g., `xml`, `text`, `html`, `json`) (required).
ruleset
A comma-separated list of built-in ruleset names or paths to custom XML ruleset files (required).
--minimumpriority
Reports problems with a priority at or higher than this value (1-5, 1 being highest).
--reportfile
Writes the generated report to the specified file instead of standard output.
--suffixes
Comma-separated list of valid source code filename extensions (default: `php`).
--exclude
Comma-separated list of directories to exclude from analysis.
--strict
Do not ignore exceptions occurring during analysis.
--ignore-errors
Ignore analysis errors and continue with the analysis.
--ignore-violations-on-exit
Do not exit with a non-zero code if violations are found.
--generate-baseline
Generates a baseline file with current violations to ignore in future runs.
--baseline
Uses the specified baseline file to ignore existing violations.
-h, --help
Displays the help message and exits.
-v, --verbose
Increases verbosity of output messages.
-V, --version
Displays program version and exits.
DESCRIPTION
PHP Mess Detector (PHPMD) is a static analysis tool designed to inspect PHP source code for common programming errors, suboptimal code, complex code, and unused parameters, properties, and methods.
It helps developers identify and eliminate potential code smells, improving the overall quality, maintainability, and readability of their applications. PHPMD works by applying a set of configurable rulesets to the codebase and reporting any violations found, making it an invaluable tool for enforcing coding standards and best practices, especially within continuous integration/continuous delivery (CI/CD) pipelines.
CAVEATS
PHPMD might occasionally produce false positives, flagging valid code as problematic. It also requires a PHP runtime environment to execute.
For very large codebases, the analysis can be time-consuming, and careful configuration of rulesets and exclusion patterns is necessary to yield relevant results and optimize performance.
RULESETS
PHPMD ships with several built-in rulesets, each targeting a specific type of code smell:
- cleancode: Rules for common clean code violations.
- codesize: Rules for excessively long/complex code.
- controversial: Rules that might be subject to debate but often indicate issues.
- design: Rules for potential design flaws.
- naming: Rules for problematic naming conventions.
- unusedcode: Rules for detecting unused parameters, properties, and methods.
Users can also create custom rulesets in XML format to tailor analysis to their specific needs.
OUTPUT FORMATS
PHPMD supports various output formats to integrate with different tools and workflows, including `xml`, `text`, `html`, `json`, `gitlab`, `github`, and `checkstyle`, allowing for flexible reporting and integration into IDEs or CI systems.
HISTORY
PHPMD was created by Manuel Pichler as a companion tool to PHP_CodeSniffer, focusing on detecting "mess" rather than just coding standard violations.
It fills a crucial gap in the PHP quality assurance ecosystem, enabling developers to identify and refactor problematic code patterns early in the development cycle. Its usage has grown steadily, particularly in automated build and continuous integration environments, becoming a standard component of many PHP development workflows.