phpphpmd

How to avoid superglobals using PHPMD


I really love phpmd to ensure a minimum level of code quality in our PHP projects. Now I like to add a rule to dectect the use of superglobals. But I do not want to have the whole controversial ruleset included. I tried the following line (phpmd installed with composer):

vendor/bin/phpmd src/ text codesize,design,naming,unusedcode,Superglobals

But the output is the following:

Cannot find specified rule-set "Superglobals".

Does anyone knows how to add a single rule in phpmd?


Solution

  • I've created a file phpmd.rules.xml with the following content:

    <?xml version="1.0"?>
    <ruleset name="No controversial but superglobals"
             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> Rulesets without controversial rules but including the check for superglobals </description> 
        <rule ref="rulesets/codesize.xml" />
        <rule ref="rulesets/design.xml" />
        <rule ref="rulesets/naming.xml" />
        <rule ref="rulesets/unusedcode.xml" />
        <rule ref="rulesets/controversial.xml/Superglobals" />
    </ruleset>
    

    To use these rules, you can use the following command (phpmd installed locally in your project with composer):

     vendor/bin/phpmd src/ text phpmd.xml 
    

    More details can be found on http://phpmd.org/documentation/creating-a-ruleset.html