-
Notifications
You must be signed in to change notification settings - Fork 16
/
ruleset.xml
229 lines (165 loc) · 9.55 KB
/
ruleset.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?xml version="1.0"?>
<ruleset name="OpinionatedSalesforce" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Opinionated PMD Ruleset</description>
<!-- IGNORE EXTERNAL LIBS -->
<include-pattern>.*/force-app/main/default/.*</include-pattern>
<!-- STANDARD RULES -->
<rule ref="category/apex/security.xml">
<priority>1</priority>
<!-- Salesforce Graph Engine does this better -->
<exclude name="ApexCRUDViolation" />
</rule>
<rule ref="category/apex/bestpractices.xml">
<priority>2</priority>
<exclude name="DebugsShouldUseLoggingLevel" /> <!-- Is buggy -->
<exclude name="ApexUnitTestClassShouldHaveRunAs" /> <!-- No smart person does that -->
<exclude name="ApexAssertionsShouldIncludeMessage" /> <!-- Better have a focused well named method -->
<exclude name="ApexUnitTestClassShouldHaveAsserts" /> <!-- Configured below -->
</rule>
<rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveAsserts" message="Apex unit test classes should have at least one System.assert() or assertEquals() or AssertNotEquals() call">
<priority>2</priority>
<properties>
<property name="additionalAssertMethodPattern" value="((System\.Assert|Assert|Asserts)\.\w+|verify\w+)"/>
</properties>
</rule>
<rule ref="category/apex/design.xml">
<priority>2</priority>
<!-- Salesforce Graph Engine does this better -->
<exclude name="UnusedMethod" />
<!-- Cognitive Complexity covers all of that better -->
<exclude name="AvoidDeeplyNestedIfStmts" />
<exclude name="StdCyclomaticComplexity" />
<exclude name="CyclomaticComplexity" />
<exclude name="ExcessiveClassLength" />
<exclude name="ExcessivePublicCount" />
<exclude name="TooManyFields" />
<exclude name="NcssTypeCount" />
</rule>
<rule ref="category/apex/performance.xml">
<priority>2</priority>
<exclude name="AvoidNonRestrictiveQueries" /> <!-- Stupid rule, sorry but no -->
<exclude name="AvoidDebugStatements" /> <!-- Improved by XPath below -->
</rule>
<rule name="DebugsNeedALoggingLevel" language="apex" message="Debug statements should use an explicit Debug Level" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties><property name="xpath"><value><![CDATA[
//MethodCallExpression[lower-case(@FullMethodName)='system.debug'][count(//VariableExpression)>2]
]]></value></property></properties>
</rule>
<rule ref="category/apex/codestyle.xml">
<priority>3</priority>
<!-- Improve by disallowing _ in Classnames -->
<exclude name="ClassNamingConventions" />
</rule>
<rule ref="category/apex/codestyle.xml/ClassNamingConventions">
<priority>3</priority>
<properties>
<property name="testClassPattern" value="[A-Z][a-zA-Z0-9_]*" />
<property name="abstractClassPattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="classPattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="interfacePattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="enumPattern" value="[A-Z][a-zA-Z0-9]*" />
</properties>
</rule>
<rule ref="category/apex/errorprone.xml">
<priority>2</priority>
</rule>
<!-- CUSTOM RULES -->
<!-- Apex Rules -->
<rule name="UnneededUseOfThisReducesReadability" language="apex" message="Unneeded use of this reduces readability" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//ThisVariableExpression[not(
ancestor::ReturnStatement
or
ancestor::MethodCallExpression[ReferenceExpression/@Image = VariableExpression/@Image]
or
ancestor::AssignmentExpression[VariableExpression[1]/@Image = VariableExpression[2]/@Image]
)]
]]></value></property></properties>
</rule>
<rule name="OnlyOneReturnPerMethod" language="apex" message="Method with multiple returns are confusing" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//Method[count(.//ReturnStatement) > 1]
]]></value></property></properties>
</rule>
<rule name="TestsShouldNotStartWithTest" language="apex" message="Test results are harder to grasp when tests start with the word test" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//Method[
starts-with(lower-case(@Image), "test")
and
descendant::ModifierNode/Annotation[@Image="IsTest"]
]
]]></value></property></properties>
</rule>
<rule name="DeclareWhatYouReturnFirstAndCallItResult" language="apex" message="What you return call it result and declare it in the first line" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//ReturnStatement[
lower-case(@Image) != 'result'
and
ancestor::Method[
@Synthetic = false()
and
lower-case(@ReturnType)!='void'
]
and
preceding-sibling::VariableDeclarationStatements/VariableDeclaration[1][@Image != 'result'
]]
]]></value></property></properties>
</rule>
<rule name="CheckIfProperFalsePositive" language="apex" message="Only False Positives can be supressed and need a proper comment." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>5</priority>
<properties><property name="xpath"><value><![CDATA[
//Annotation[@Image='SuppressWarnings']/AnnotationParameter[starts-with(@Value, 'PMD.')]
]]></value></property></properties>
</rule>
<rule name="CommentsOftenExcuseForBadCodeAndTests" language="apex" message="Formal class or method comments are often an excuse for bad code and tests" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties><property name="xpath"><value><![CDATA[
//FormalComment
]]></value></property></properties>
</rule>
<rule name="PreferRealObjectsOverStaticHelpers" language="apex" message="Use real objects with data and state instead of static messy helpers." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//UserClass[not(.//Method[@Constructor=true()])]//Method/ModifierNode[@Static=true()]
]]></value></property></properties>
</rule>
<!-- Metadata XML Rules -->
<rule name="BumpApiVersion" language="xml" message="Metadata should use the latest API version." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties><property name="xpath"><value><![CDATA[
//apiVersion/text[number(@Image) < 50]
]]></value></property></properties>
</rule>
<!-- Flow Rules -->
<rule name="DMLStatementInFlowLoop" language="xml" message="DML Operations shouldn't be done inside of Flow loops" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties><property name="xpath"><value><![CDATA[
//Flow/loops//targetReference[not(ancestor::noMoreValuesConnector)]/text[@Image=//Flow/(recordCreates|recordDeletes|recordUpdates)/name/text/@Image]
]]></value></property></properties>
</rule>
<!-- Custom Objects / Fields Rules -->
<rule name="MetadataRequiresDescription" language="xml" message="Add a description to explain custom metadata" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties><property name="xpath"><value><![CDATA[
//(CustomObject | CustomField | PermissionSet)[not(description)]
]]></value></property></properties>
</rule>
<!-- Profile and Permission Set Rules -->
<rule name="ModifyOrViewAllOnPermSet" language="xml" message="Allowing this user permission can give access and ability to modify sensitive data." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>1</priority>
<properties><property name="xpath"><value><![CDATA[
//PermissionSet/userPermissions[ name/text[@Image='ModifyAllData' or @Image='ViewAllData'] and enabled/text[@Image='true'] ]
]]></value></property></properties>
</rule>
<rule name="ViewSetupByNonSysAdmins" language="xml" message="Exposing the setup menu to non-authorized users." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>1</priority>
<properties><property name="xpath"><value><![CDATA[
//Profile/userPermissions[ pmd:fileName() != 'System Administrator' and enabled/text[@Image='true'] and name/text[@Image='ViewSetup'] ]
]]></value></property></properties>
</rule>
</ruleset>