-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpmd-ruleset.xml
More file actions
315 lines (233 loc) · 13.2 KB
/
pmd-ruleset.xml
File metadata and controls
315 lines (233 loc) · 13.2 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?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 Salesforce Developer</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" /> <!-- Stupid rule, a well-named test method is the assertion -->
</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]*(_t)?" />
<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[
ancestor::VariableExpression
and
not(
ancestor::ReturnStatement
or
ancestor::MethodCallExpression[ReferenceExpression/@Image = VariableExpression/@Image]
or
ancestor::VariableExpression/@Image = ancestor::Method//Parameter/@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="NullValueCheckBeforeEmptyCheck" language="apex" message="Apex code must check != null before .isEmpty()" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties><property name="xpath"><value><![CDATA[
//BooleanExpression[@Op="&&"][
child::*[2][
self::BooleanExpression[@Op="!="][
child::*[1][self::VariableExpression] and
child::*[2][self::LiteralExpression[@LiteralType="NULL"]]
]
]
]
]]></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[
count(.//Method) = count(.//Method/ModifierNode[@Static=true()])
and count(.//Method) > 0
and not(descendant::ModifierNode/Annotation[@Image='IsTest'])
and not(.//Method/ModifierNode/Annotation[@Image='InvocableMethod' or @Image='AuraEnabled'])
]
]]></value></property></properties>
</rule>
<rule name="UseModernAssertClass" language="apex" message="Use the modern Assert class instead of legacy System.assert methods" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//MethodCallExpression[starts-with(lower-case(@FullMethodName), 'system.assert')]
]]></value></property></properties>
</rule>
<rule name="AssertionsShouldNotHaveMessages" language="apex" message="Assertions don't need messages - let your test method name do the explaining" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties><property name="xpath"><value><![CDATA[
//MethodCallExpression[
starts-with(lower-case(@FullMethodName), 'assert.')
and count(./ReferenceExpression/following-sibling::*) > 2
]
]]></value></property></properties>
</rule>
<rule name="TestClassesMustEndWithUnderscoreTest" language="apex" message="Test classes must end with _Test suffix" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//UserClass[
ModifierNode/Annotation[@Image='IsTest']
and not(ends-with(@Image, '_Test'))
]
]]></value></property></properties>
</rule>
<rule name="TestClassesShouldBePrivate" language="apex" message="Test classes should be private - they're not part of your API" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//UserClass[
ModifierNode/Annotation[@Image='IsTest']
and ModifierNode[@Public = true() or @Global = true()]
]
]]></value></property></properties>
</rule>
<rule name="GetPrefixIsJustNoise" language="apex" message="Skip the 'get' prefix - account.name() reads better than account.getName()" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>4</priority>
<properties><property name="xpath"><value><![CDATA[
//Method[
starts-with(@Image, 'get')
and @Synthetic = false()
and not(ancestor::UserClass/descendant::ModifierNode/Annotation[@Image='IsTest'])
]
]]></value></property></properties>
</rule>
<rule name="ClassNamesBecomeSelfFulfillingProphecies" language="apex" message="Service, Handler, Manager, Helper, Util, Wrapper - these names attract messy code like magnets" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties><property name="xpath"><value><![CDATA[
//UserClass[
matches(@Image, '.*(Service|Handler|Manager|Helper|Util|Wrapper)$')
and not(descendant::ModifierNode/Annotation[@Image='IsTest'])
]
]]></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>