How to ignore specific rule for specific directory in PHPMD
Asked Answered
D

2

6

command

phpmd app,config,routes text phpmd.xml

I use phpmd on Laravel project, there is a rule named CouplingBetweenObjects under design.xml, I want to ignore all files in app/Http/Controllers directory to this rule, phpmd only provide --exclude option which will make the directory pass ALL rules, is it possible to ignore a rule to specific directory either by XML file or command line

phpmd.xml

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Netask rule set"
    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>Netask code style rule set
</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveParameterList"/>
<rule ref="rulesets/codesize.xml/ExcessivePublicCount">
    <properties>
        <property name="minimum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyFields">
    <properties>
        <property name="maxfields" value="20"/>
    </properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
    <properties>
        <property name="maximum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/controversial.xml"/>
<rule ref="rulesets/design.xml"/>
<rule ref="rulesets/naming.xml/ShortVariable"/>
<rule ref="rulesets/naming.xml/LongVariable">
    <properties>
        <property name="maximum" value="30"/>
    </properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">
    <properties>
        <property name="minimum" value="2"/>
    </properties>
</rule>
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass"/>
<rule ref="rulesets/naming.xml/ConstantNamingConventions"/>
<rule ref="rulesets/naming.xml/BooleanGetMethodName"/>
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" />

<exclude-pattern>app/Console/Kernel.php</exclude-pattern>
<exclude-pattern>app/Services/Service.php</exclude-pattern>
<exclude-pattern>tests/TestCase.php</exclude-pattern>
</ruleset>
Dodgson answered 9/7, 2020 at 3:8 Comment(0)
C
5

I suggest looking at it in a slightly different way i.e. you want to ignore a specific PHPMD rule in specific files, which is supported by PHPMD by annotations in comments.

e.g.

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */

https://phpmd.org/documentation/suppress-warnings.html

I think that's the idiomatic way to use PHPMD. If that's not possible, you could alias a new command that runs phpmd twice (with || between the commands) and excludes the unwanted rule in a second rule set xml.

Coupling answered 9/10, 2020 at 14:30 Comment(1)
but you have to add this to each file. Does not look convenient if there are many files in directory.Antineutrino
P
1

It seems like the only way is to write your own rule, copy the suppression you'd like, and exclude folders. :( Looking into this as well might share my solution at some point.

My use case is "excessive param list" in readonly VO's.

Perreault answered 15/6, 2023 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.