PHP Mess Dectector (PHPMD) with PhpStorm Integration
24/09/2018 by keopx


PHPMD - PHP Mess Detector

PHP Mess Detector is one of those tools that helps keep the code base manageable and clean. We are introducing you to PHPMD, as used in the PHP project. It is a piece of PMD software by PHP, which bears a similarity to Java PMD. It clearly indicates the areas where bugs occur, any unsuitable coding, complex expressions, unused parameters/methods and properties.

Here you have the description directly from the site:

It is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.

What PHPMD does is: It takes a given PHP source code base and look for several potential problems within that source. These problems can be things like:

  • Possible bugs
  • Suboptimal code
  • Overcomplicated expressions
  • Unused parameters, methods, properties


Global installation:

$ composer global require phpmd/phpmd

You can also install it only for your project, but if you are using it with more than one project, I recommend you to install it globally instead. 

PhpStorm integration



Now, phpmd is running and monitoring your code all the time. Here example:

 PHPMD PhpStorm error exampel

How to use it.

We will explain its use through the console

$ phpmd --help
Mandatory arguments:
1) A php source code filename or directory. Can be a comma-separated string
2) A report format
3) A ruleset filename or a comma-separated string of rulesetfilenames

Available formats: xml, text, html.
Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.

Optional arguments that may be put after the mandatory arguments:
--minimumpriority: rule priority threshold; rules with lower priority than this will not be used
--reportfile: send report output to a file; default to STDOUT
--suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml
--exclude: comma-separated string of patterns that are used to ignore directories
--strict: also report those nodes with a @SuppressWarnings annotation
--ignore-violations-on-exit: will exit with a zero code, even if any violations are found

There are three arguments.

  1. Specify a source code or directory. Put a comma between each one, if specifying more than one.
  2. Format the report. You can specify between xml, text or HTML.
  3. Rule set. There are options of: cleancode, codesize, controversial, design, naming, and unusedcode.

It may take some time to complete, depending on the number of files. The following results will be displayed if you specified “text”.

Regarding rule sets.

The rules applicable to PHPMD are as follows:

  • cleancode rule: cleancode
    • This points out any messy coding issues.
  • codesize rule: codesize
    • This will split any classes that are too big, or methods that are too frequent, to reduce the code size.
  • discussion rule: controversial
    • This points out any frequently problems with camel case and snake case when developing via PHP, so that camel case can be used.
  • design rule: design
    • Highlights any problem related to the software design.
  • naming rule: naming
    • Points out too long or too short names.
  • unusedcode rule: unusedcode
    • Searches and Highlights any coding that's not used.

Specify the desired rules to check your coding (multiple options are possible).

Example running shell command

$ phpmd web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php text cleancode,codesize,controversial,design,naming,unusedcode
/Proyectos/Test/Web/data/www/keopx/web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php:25       The parameter $field_definition is not named in camelCase.
/Proyectos/Test/Web/data/www/keopx/web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php:61       Avoid unused local variables such as '$a'.
/Proyectos/Test/Web/data/www/keopx/web/modules/contrib/metatag/src/Plugin/Field/FieldType/MetatagFieldItem.php:71       The method preSave uses an else expression. Else is never necessary and you can simplify the code to work without else.

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.