1

I have set this rule in the phpmd.xml file:

<rule ref="rulesets/codesize.xml/ExcessiveClassLength">
    <properties>
        <property name="minimum" value="1500"/>
    </properties>
</rule>

But this rule is being ignored, I still get this error:

/var/www/html/tests/Model/AdvertTest.php:18 The class AdvertTest has 1026 lines of code. Current threshold is 1000. Avoid really long classes.

Any idea why phpmd does't read this rule. Actually to fix it the only way for me is adding the suppression error on the top the class.

Some more info: I am running phpmd on MAC, with Docker with this command: docker-compose exec php sh -c "./vendor/bin/phpmd ./tests text phpmd.test.xml"

Full XML: PHP Mess Detector rulesets

<rule ref="rulesets/codesize.xml">
    <exclude name="CyclomaticComplexity"/>
    <exclude name="ExcessiveMethodLength"/>
    <exclude name="NPathComplexity"/>
    <exclude name="TooManyMethods"/>
    <exclude name="ExcessiveClassComplexity"/>
    <exclude name="ExcessivePublicCount"/>
    <exclude name="TooManyPublicMethods"/>
    <exclude name="TooManyFields"/>
</rule>
<rule ref="rulesets/codesize.xml/TooManyFields">
    <properties>
        <property name="maxfields" value="21"/>
    </properties>
</rule>
<rule ref="rulesets/cleancode.xml">
    <exclude name="StaticAccess"/>
    <exclude name="ElseExpression"/>
</rule>
<rule ref="rulesets/controversial.xml"/>
<rule ref="rulesets/design.xml">
    <exclude name="CouplingBetweenObjects" />
    <exclude name="NumberOfChildren" />
</rule>
<rule ref="rulesets/design.xml/NumberOfChildren">
    <properties>
        <property name="minimum" value="20"/>
    </properties>
</rule>
<rule ref="rulesets/naming.xml">
    <exclude name="ShortVariable"/>
    <exclude name="LongVariable"/>
</rule>
<rule ref="rulesets/naming.xml/LongVariable">
    <properties>
        <property name="maximum" value="25"/>
    </properties>
</rule>
<rule ref="rulesets/unusedcode.xml">
    <exclude name="UnusedFormalParameter"/>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength">
    <properties>
        <property name="minimum" value="1500"/>
    </properties>
</rule>

albanx
  • 6,193
  • 9
  • 67
  • 97
  • Are you sure it's this xml file which is read? What if you change other random rules? Does it work? – rap-2-h Dec 16 '16 at 11:00
  • "minimum"? That doesn't seem logical ... shouldn't it be "maximum"? – Narf Dec 16 '16 at 11:22
  • @Narf according to the documentation the property is called `minimum` https://phpmd.org/rules/codesize.html – albanx Dec 16 '16 at 11:26
  • Hmm, you are correct. In that case I wonder if it has something to do with a conflict between that particular `ref` and `ref="rulesets/codesize.xml"`. But either way, it seems to me that you have way too many customizations here, going out of your way to use the tool, but at the same time not wanting to comply with it - kind of misses the point IMO. – Narf Dec 16 '16 at 11:39

1 Answers1

0

I was too late.

By default, you load all rules (including ExcessiveClassLength) in this line:

<rule ref="rulesets/codesize.xml">

This rule is applied twice:

  1. By loading them all:
<rule ref="rulesets/codesize.xml">
  1. Having your own definition:
<rule ref="rulesets/codesize.xml/ExcessiveClassLength">

Then, the most restrictive is triggered.

When you load them all, you have to ignore the rule you are trying to replace by adding:

<rule ref="rulesets/codesize.xml">
    <exclude name="ExcessiveClassLength"> <--- Here
    <exclude name="CyclomaticComplexity"/>
    <exclude name="ExcessiveMethodLength"/>
    <exclude name="NPathComplexity"/>
    <exclude name="TooManyMethods"/>
    <exclude name="ExcessiveClassComplexity"/>
    <exclude name="ExcessivePublicCount"/>
    <exclude name="TooManyPublicMethods"/>
    <exclude name="TooManyFields"/>
</rule>

Hope it works.

vadov
  • 21
  • 1
  • 3
  • I honestly do not even recall why I asked this question. Said that I have switched editor/configs long ago :D. Thanks though – albanx Nov 23 '22 at 16:04