Merge pull request #4604 from adangel/issue-4596-apex-excessivepubliccount
[apex] ExcessivePublicCount: consider properties
This commit is contained in:
@@ -43,6 +43,8 @@ The remaining section describes the complete release notes for 7.0.0.
|
||||
|
||||
* miscellaneous
|
||||
* [#4582](https://github.com/pmd/pmd/issues/4582): \[dist] Download link broken
|
||||
* apex-design
|
||||
* [#4596](https://github.com/pmd/pmd/issues/4596): \[apex] ExcessivePublicCount ignores properties
|
||||
* java
|
||||
* [#4401](https://github.com/pmd/pmd/issues/4401): \[java] PMD 7 fails to build under Java 19
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.apex.rule.design;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTFieldDeclarationStatements;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTProperty;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
|
||||
import net.sourceforge.pmd.lang.apex.rule.internal.AbstractCounterCheckRule;
|
||||
|
||||
@@ -36,20 +37,21 @@ public class ExcessivePublicCountRule extends AbstractCounterCheckRule<ASTUserCl
|
||||
|
||||
@Override
|
||||
protected int getMetric(ASTUserClass node) {
|
||||
// node streams would be useful here
|
||||
int publicMethods =
|
||||
(int) node.findChildrenOfType(ASTMethod.class)
|
||||
.stream()
|
||||
.filter(it -> it.getModifiers().isPublic() && !it.isSynthetic())
|
||||
.count();
|
||||
|
||||
node.children(ASTMethod.class)
|
||||
.filter(it -> it.getModifiers().isPublic() && !it.isSynthetic())
|
||||
.count();
|
||||
int publicFields =
|
||||
(int) node.findChildrenOfType(ASTFieldDeclarationStatements.class)
|
||||
.stream()
|
||||
.filter(it -> it.getModifiers().isPublic() && !it.getModifiers().isStatic())
|
||||
.count();
|
||||
node.children(ASTFieldDeclarationStatements.class)
|
||||
.filter(it -> it.getModifiers().isPublic() && !it.getModifiers().isStatic())
|
||||
.count();
|
||||
|
||||
return publicFields + publicMethods;
|
||||
int publicProperties =
|
||||
node.children(ASTProperty.class)
|
||||
.filter(it -> it.getModifiers().isPublic() && !it.getModifiers().isStatic())
|
||||
.count();
|
||||
|
||||
return publicFields + publicMethods + publicProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -281,12 +281,12 @@ public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) {
|
||||
<rule name="ExcessivePublicCount"
|
||||
language="apex"
|
||||
since="5.5.0"
|
||||
message="The class {0} has {1} public methods and attributes (limit: {2})"
|
||||
message="The class {0} has {1} public methods, attributes, and properties (limit: {2})"
|
||||
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessivePublicCountRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_design.html#excessivepubliccount">
|
||||
<description>
|
||||
Classes with large numbers of public methods and attributes require disproportionate testing efforts
|
||||
since combinational side effects grow rapidly and increase risk. Refactoring these classes into
|
||||
Classes with large numbers of public methods, attributes, and properties require disproportionate testing efforts
|
||||
since combinatorial side effects grow rapidly and increase risk. Refactoring these classes into
|
||||
smaller ones not only increases testability and reliability but also allows new variations to be
|
||||
developed easily.
|
||||
</description>
|
||||
@@ -303,6 +303,9 @@ public class Foo {
|
||||
public void doMoreWork() {}
|
||||
public void doWorkAgain() {}
|
||||
// [... more more public methods ...]
|
||||
|
||||
public String property1 { get; set; }
|
||||
// [... more more public properties ...]
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
|
||||
@@ -120,8 +120,8 @@ public class SomeClass {
|
||||
<expected-problems>2</expected-problems>
|
||||
<expected-linenumbers>1,5</expected-linenumbers>
|
||||
<expected-messages>
|
||||
<message>The class OuterClass has 2 public methods and attributes (limit: 1)</message>
|
||||
<message>The class InnerClass has 4 public methods and attributes (limit: 1)</message>
|
||||
<message>The class OuterClass has 2 public methods, attributes, and properties (limit: 1)</message>
|
||||
<message>The class InnerClass has 4 public methods, attributes, and properties (limit: 1)</message>
|
||||
</expected-messages>
|
||||
<code><![CDATA[
|
||||
public class OuterClass {
|
||||
@@ -136,6 +136,32 @@ public class OuterClass {
|
||||
public void method2() { }
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[apex] ExcessivePublicCount ignores properties #4596</description>
|
||||
<rule-property name="minimum">4</rule-property>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>1</expected-linenumbers>
|
||||
<expected-messages>
|
||||
<message>The class Example has 4 public methods, attributes, and properties (limit: 4)</message>
|
||||
</expected-messages>
|
||||
<code><![CDATA[
|
||||
public class Example {
|
||||
public void publicMethod1() { } // count: 1
|
||||
public Integer publicField; // count: 2
|
||||
public String publicProperty1 {get; set; } // count: 3
|
||||
public String publicProperty2 { // count: 4
|
||||
get {
|
||||
return publicProperty2;
|
||||
}
|
||||
set {
|
||||
publicProperty2 = value;
|
||||
}
|
||||
}
|
||||
private String privateProperty1 {get; set;}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
||||
Reference in New Issue
Block a user