-
Notifications
You must be signed in to change notification settings - Fork 17
/
ruleset.xml
256 lines (220 loc) · 11.3 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
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
<?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>
</rule>
<rule ref="category/apex/bestpractices.xml">
<priority>2</priority>
<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>3</priority>
<properties>
<property name="additionalAssertMethodPattern" value="((System\.Assert|Assert|Asserts)\.\w+|verify\w+)"/>
</properties>
</rule>
<rule ref="category/apex/design.xml">
<priority>2</priority>
<!-- Note: Cognitive Complexity covers all of that better -->
<exclude name="AvoidDeeplyNestedIfStmts" />
<exclude name="StdCyclomaticComplexity" />
<exclude name="CyclomaticComplexity" />
<exclude name="ExcessiveClassLength" />
<exclude name="ExcessiveParameterList" />
<exclude name="ExcessivePublicCount" />
<exclude name="TooManyFields" />
<exclude name="NcssConstructorCount" />
<exclude name="NcssMethodCount" />
<exclude name="NcssTypeCount" />
</rule>
<rule ref="category/apex/performance.xml">
<priority>2</priority>
<!-- Improved by AvoidProductionDebugLogs below -->
<exclude name="AvoidDebugStatements" />
</rule>
<rule name="AvoidDebugStatements" language="apex" message="Avoid leaving System.debug() statments in code as they negativly influence performance." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties>
<property name="xpath"><value><![CDATA[
//MethodCallExpression[lower-case(@FullMethodName)='system.debug'][count(*)=2]
]]></value></property>
</properties>
</rule>
<rule ref="category/apex/codestyle.xml">
<priority>2</priority>
<!-- Improve by disallowing _ in Classnames -->
<exclude name="ClassNamingConventions" />
</rule>
<rule ref="category/apex/codestyle.xml/ClassNamingConventions">
<priority>2</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>
<!-- Note: Stupid category but I think everything in there is valid and a 2 -->
</rule>
<!-- CUSTOM RULES -->
<!-- Apex Rules -->
<rule name="UseTypeForNameOrDeferredFor" language="apex" message="" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>2</priority>
<properties>
<property name="xpath"><value><![CDATA[
//MethodCallExpression[lower-case(@MethodName)='get']/ReferenceExpression
/MethodCallExpression[lower-case(@MethodName)='getglobaldescribe']/ReferenceExpression[lower-case(@Image)='schema']
]]></value></property>
</properties>
</rule>
<rule name="CommentsOftenExcuseForBadCodeAndTests" language="apex" message="Comments are often an excuse for bad code and tests" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>3</priority>
<properties>
<property name="xpath"><value><![CDATA[
//FormalComment
]]></value></property>
</properties>
</rule>
<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>
<!-- Most people find this to controversial ;-)
<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>
<!-- 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>3</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)[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="ModifyOrViewAllOnSysAdminProfile" language="xml" message="Only Admins can view and modify all data, if any other profile get these permissions, they could manipulate records that shouldn't" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>1</priority>
<properties>
<property name="xpath"><value><![CDATA[
//Profile/userPermissions[ name/text[@Image='ModifyAllData' or @Image='ViewAllData'] and enabled/text[@Image='true'] and pmd:fileName() != 'System Administrator' ]
]]> </value>
</property>
</properties>
</rule>
<rule name="ManageUsersByNonSysAdmins" language="xml" message="Managing users need to be limited to System Administrator Profile only." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<priority>1</priority>
<properties>
<property name="xpath"><value><![CDATA[
//Profile/userPermissions[ pmd:fileName() != 'System Administrator' and name/text[@Image='ManageUsers'] ]
]]> </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>