PHP Mess Detector

Language / Framework:PHP
Checks: 40
Categories: Clarity, Style, Bug Risk, Compatibility
PHPMD Version: 2.5.0

PHPMD looks for several potential problems within PHP source code, like possible bugs, suboptimal code, overcomplicated expressions, and unused parameters, methods, or properties.

Enable the Engine

To enable PHPMD analysis, add the following to your .codeclimate.yml configuration file:

engines:
  phpmd:
    enabled: true
ratings:
  paths:
  - "**.php"

You can also enable the engine via the CLI with codeclimate engines:enable phpmd. This will create a default configuration file for you if you don’t already have one.

More information about the CLI is available in the README here: https://github.com/codeclimate/codeclimate

Configure the Engine

Extensions

You can specify which file extensions you'd like our PHPMD engine to analyze by adding a file_extensions key nested under an engine config key in your .codeclimate.yml:

engines:
  phpmd:
    enabled: true
    config:
      file_extensions: 
        - php
        - inc

Rulesets

PHP Mess Detector offers a variety of checks for your PHP code, including:

You can configure our PHPMD engine to use the rulesets most valuable to your team, and also provide paths to custom rulesets:

engines:
  phpmd:
    enabled: true
    config:
      file_extensions: "php"
      rulesets: "unusedcode,codesize,naming,optional_relative_path_to_custom_ruleset.xml"

If no rulesets are specified in your .codeclimate.yml, our PHPMD engine by default includes checks from all six of the above rulesets.

Configuring Thresholds

Standard PHPMD checks come set up with certain thresholds by default. To adjust these, add a custom ruleset that includes the PHPMD ruleset of interest, and excludes the rules for which you'd like to set custom values. Add these rules in the same .xml ruleset file separately.

For instance, to set the codesizeruleset's TooManyPublicMethods to 25, add a custom ruleset.xml that includes the codesize ruleset excluding that rule, then lists the TooManyPublicMethods rule separately with the desired value.

<?xml version="1.0"?>
<ruleset name="PHPMD rule set for my project" xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>Custom rules for checking my project</description>
    
    <rule ref="rulesets/codesize.xml">
        <exclude name="TooManyPublicMethods"/>
    </rule>

    <rule ref="rulesets/codesize.xml/TooManyPublicMethods">
        <properties>
            <property name="maxmethods" value="25" />
        </properties>
    </rule>
</ruleset>

In your .codeclimate.yml config under the phpmd engine rulesets, include the path to this custom ruleset file only, and omit the codesize ruleset. This step is important because PHPMD rulesets are additive.

engines:
  phpmd:
    enabled: true
    config:
      file_extensions: 
        - php
        - inc
      rulesets: "unusedcode,naming,myRuleset.xml"

Understand the Engine

For more information about all the checks available and their configuration options, please check out the documentation here: https://phpmd.org

PHP Mess Detector