-
Notifications
You must be signed in to change notification settings - Fork 2
/
pmd-ruleset.xml
1981 lines (1909 loc) · 68.5 KB
/
pmd-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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" ?>
<ruleset
name="PMD Apex"
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>PMD Apex - 23 Apr 2021</description>
<!--
* Note on priorities:
* 1: hihgest level
* 5: lowest level
*
* 1: must fix (CI will stop on any of those)
* 2: should fix (CI might refuse the PR). Not fixing those will make the code difficult to understand, test or debug.
* 3: default value
* 4: generic code style, naming conventions, etc..
* 5: for information no immediate action required
-->
<!--
* Best Practices - 28 Jun 2020
* Rules which enforce generally accepted best practices.
* https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/resources/category/apex/bestpractices.xml
-->
<rule
name="ApexAssertionsShouldIncludeMessage"
language="apex"
since="6.13.0"
message="Apex test assert statement should make use of the message parameter."
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexAssertionsShouldIncludeMessageRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#apexassertionsshouldincludemessage"
>
<description>
The second parameter of System.assert/third parameter of System.assertEquals/System.assertNotEquals is a message.
Having a second/third parameter provides more information and makes it easier to debug the test failure and
improves the readability of test output.
</description>
<priority>3</priority>
<example>
<![CDATA[
@isTest
public class Foo {
@isTest
static void methodATest() {
System.assertNotEquals('123', o.StageName); // not good
System.assertEquals('123', o.StageName, 'Opportunity stageName is wrong.'); // good
System.assert(o.isClosed); // not good
System.assert(o.isClosed, 'Opportunity is not closed.'); // good
}
}
]]>
</example>
</rule>
<rule
name="ApexUnitTestClassShouldHaveAsserts"
language="apex"
since="5.5.1"
message="Apex unit tests should System.assert() or assertEquals() or assertNotEquals()"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestClassShouldHaveAssertsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#apexunittestclassshouldhaveasserts"
>
<description>
Apex unit tests should include at least one assertion. This makes the tests more robust, and using assert
with messages provide the developer a clearer idea of what the test does.
</description>
<priority>3</priority>
<example>
<![CDATA[
@isTest
public class Foo {
public static testMethod void testSomething() {
Account a = null;
// This is better than having a NullPointerException
// System.assertNotEquals(a, null, 'account not found');
a.toString();
}
}
]]>
</example>
</rule>
<rule
name="ApexUnitTestMethodShouldHaveIsTestAnnotation"
language="apex"
since="6.13.0"
message="Apex test methods should have @isTest annotation."
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestMethodShouldHaveIsTestAnnotationRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#apexunittestmethodshouldhaveistestannotation"
>
<description>
Apex test methods should have @isTest annotation.
As testMethod keyword is deprecated, Salesforce advices to use @isTest annotation for test class/methods.
</description>
<priority>3</priority>
<example>
<![CDATA[
@isTest
private class ATest {
@isTest
static void methodATest() {
}
static void methodBTest() {
}
@isTest static void methodCTest() {
System.assert(1==2);
}
@isTest static void methodCTest() {
System.debug('I am a debug statement');
}
private void fetchData() {
}
}
]]>
</example>
</rule>
<rule
name="ApexUnitTestShouldNotUseSeeAllDataTrue"
language="apex"
since="5.5.1"
message="Apex unit tests should not use @isTest(seeAllData = true)"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestShouldNotUseSeeAllDataTrueRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#apexunittestshouldnotuseseealldatatrue"
>
<description>
Apex unit tests should not use @isTest(seeAllData=true) because it opens up the existing database data for unexpected modification by tests.
</description>
<priority>3</priority>
<example>
<![CDATA[
@isTest(seeAllData = true)
public class Foo {
public static testMethod void testSomething() {
Account a = null;
// This is better than having a NullPointerException
// System.assertNotEquals(a, null, 'account not found');
a.toString();
}
}
]]>
</example>
</rule>
<rule
name="AvoidGlobalModifier"
language="apex"
since="5.5.0"
message="Avoid using global modifier"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidGlobalModifierRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#avoidglobalmodifier"
>
<description>
Global classes should be avoided (especially in managed packages) as they can never be deleted or changed in signature. Always check twice if something needs to be global.
Many interfaces (e.g. Batch) required global modifiers in the past but don't require this anymore. Don't lock yourself in.
</description>
<priority>3</priority>
<example>
<![CDATA[
global class Unchangeable {
global UndeletableType unchangable(UndeletableType param) {
// ...
}
}
]]>
</example>
</rule>
<rule
name="AvoidLogicInTrigger"
language="apex"
since="5.5.0"
message="Avoid logic in triggers"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidLogicInTriggerRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#avoidlogicintrigger"
>
<description>
As triggers do not allow methods like regular classes they are less flexible and suited to apply good encapsulation style.
Therefore delegate the triggers work to a regular class (often called Trigger handler class).
See more here: <https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices>
</description>
<priority>3</priority>
<example>
<![CDATA[
trigger Accounts on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
for(Account acc : Trigger.new) {
if(Trigger.isInsert) {
// ...
}
// ...
if(Trigger.isDelete) {
// ...
}
}
}
]]>
</example>
</rule>
<rule
name="DebugsShouldUseLoggingLevel"
since="6.18.0"
language="apex"
message="Calls to System.debug should specify a logging level."
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#debugsshoulduselogginglevel"
>
<description>
The first parameter of System.debug, when using the signature with two parameters, is a LoggingLevel enum.
Having the Logging Level specified provides a cleaner log, and improves readability of it.
</description>
<priority>3</priority>
<properties>
<property
name="strictMode"
type="Boolean"
value="false"
description="If true, mark statements that use the DEBUG enum of LoggingLevel."
/>
<property name="version" value="2.0" />
<property name="xpath">
<value>
<![CDATA[
//MethodCallExpression[lower-case(@FullMethodName)='system.debug'][count(*)=2
or ($strictMode=true() and count(*)=3 and lower-case(VariableExpression/@Image)='debug')]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
@isTest
public class Foo {
@isTest
static void bar() {
System.debug('Hey this code executed.'); // not good
System.debug(LoggingLevel.WARN, 'Hey, something might be wrong.'); // good
System.debug(LoggingLevel.DEBUG, 'Hey, something happened.'); // not good when on strict mode
}
}
]]>
</example>
</rule>
<rule
name="UnusedLocalVariable"
since="6.23.0"
language="apex"
message="Variable ''{0}'' defined but not used"
class="net.sourceforge.pmd.lang.apex.rule.bestpractices.UnusedLocalVariableRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_bestpractices.html#unusedlocalvariable"
>
<description>
Detects when a local variable is declared and/or assigned but not used.
</description>
<example>
<![CDATA[
public Boolean bar(String z) {
String x = 'some string'; // not used
String y = 'some other string'; // used in the next line
return z.equals(y);
}
]]>
</example>
</rule>
<!--
* Code Style - 28 Jun 2020
* Rules which enforce a specific coding style.
* https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/resources/category/apex/codestyle.xml
-->
<rule
name="ClassNamingConventions"
language="apex"
since="5.5.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.ClassNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#classnamingconventions"
>
<description>
Configurable naming conventions for type declarations. This rule reports
type declarations which do not match the regex that applies to their
specific kind (e.g. enum or interface). Each regex can be configured through
properties.
By default this rule uses the standard Apex naming convention (Pascal case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class FooClass { } // This is in pascal case, so it's ok
public class fooClass { } // This will be reported unless you change the regex
]]>
</example>
</rule>
<rule
name="IfElseStmtsMustUseBraces"
language="apex"
since="5.6.0"
message="Avoid using 'if...else' statements without curly braces"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#ifelsestmtsmustusebraces"
>
<description>
Avoid using if..else statements without using surrounding braces. If the code formatting
or indentation is lost then it becomes difficult to separate the code being controlled
from the rest.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0" />
<property name="xpath">
<value>
<![CDATA[
//IfBlockStatement/BlockStatement[@CurlyBrace= false()][count(child::*) > 0]
|
//IfElseBlockStatement/BlockStatement[@CurlyBrace= false()][count(child::*) > 0]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
// this is OK
if (foo) x++;
// but this is not
if (foo)
x = x+1;
else
x = x-1;
]]>
</example>
</rule>
<rule
name="IfStmtsMustUseBraces"
language="apex"
since="5.6.0"
message="Avoid using if statements without curly braces"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#ifstmtsmustusebraces"
>
<description>
Avoid using if statements without using braces to surround the code block. If the code
formatting or indentation is lost then it becomes difficult to separate the code being
controlled from the rest.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0" />
<property name="xpath">
<value>
<![CDATA[
//IfBlockStatement/BlockStatement[@CurlyBrace= false()]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
if (foo) // not recommended
x++;
if (foo) { // preferred approach
x++;
}
]]>
</example>
</rule>
<rule
name="FieldDeclarationsShouldBeAtStart"
language="apex"
since="6.23.0"
message="Field declaration for ''{0}'' should be before method declarations in its class"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.FieldDeclarationsShouldBeAtStartRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#fielddeclarationsshouldbeatstart"
>
<description>
Field declarations should appear before method declarations within a class.
</description>
<priority>3</priority>
<example>
<![CDATA[
class Foo {
public Integer someField; // good
public void someMethod() {
}
public Integer anotherField; // bad
}
]]>
</example>
</rule>
<rule
name="FieldNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.FieldNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#fieldnamingconventions"
>
<description>
Configurable naming conventions for field declarations. This rule reports variable declarations
which do not match the regex that applies to their specific kind ---e.g. constants (static final),
static field, final field. Each regex can be configured through properties.
By default this rule uses the standard Apex naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
Integer instanceField; // This is in camel case, so it's ok
Integer INSTANCE_FIELD; // This will be reported unless you change the regex
}
]]>
</example>
</rule>
<rule
name="ForLoopsMustUseBraces"
language="apex"
since="5.6.0"
message="Avoid using 'for' statements without curly braces"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#forloopsmustusebraces"
>
<description>
Avoid using 'for' statements without using surrounding braces. If the code formatting or
indentation is lost then it becomes difficult to separate the code being controlled
from the rest.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0" />
<property name="xpath">
<value>
<![CDATA[
//ForLoopStatement/BlockStatement[@CurlyBrace= false()]
|
//ForEachStatement/BlockStatement[@CurlyBrace= false()]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
for (int i = 0; i < 42; i++) // not recommended
foo();
for (int i = 0; i < 42; i++) { // preferred approach
foo();
}
]]>
</example>
</rule>
<rule
name="FormalParameterNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.FormalParameterNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#formalparameternamingconventions"
>
<description>
Configurable naming conventions for formal parameters of methods.
This rule reports formal parameters which do not match the regex that applies to their
specific kind (e.g. method parameter, or final method parameter). Each regex can be
configured through properties.
By default this rule uses the standard Apex naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
public bar(Integer methodParameter) { } // This is in camel case, so it's ok
public baz(Integer METHOD_PARAMETER) { } // This will be reported unless you change the regex
}
]]>
</example>
</rule>
<rule
name="LocalVariableNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.LocalVariableNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#localvariablenamingconventions"
>
<description>
Configurable naming conventions for local variable declarations.
This rule reports variable declarations which do not match the regex that applies to their
specific kind (e.g. local variable, or final local variable). Each regex can be configured through
properties.
By default this rule uses the standard Apex naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
public Foo() {
Integer localVariable; // This is in camel case, so it's ok
Integer LOCAL_VARIABLE; // This will be reported unless you change the regex
}
}
]]>
</example>
</rule>
<rule
name="MethodNamingConventions"
language="apex"
since="5.5.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.MethodNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#methodnamingconventions"
>
<description>
Configurable naming conventions for method declarations. This rule reports
method declarations which do not match the regex that applies to their
specific kind (e.g. static method, or test method). Each regex can be
configured through properties.
By default this rule uses the standard Apex naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
public void instanceMethod() { } // This is in camel case, so it's ok
public void INSTANCE_METHOD() { } // This will be reported unless you change the regex
}
]]>
</example>
</rule>
<rule
name="OneDeclarationPerLine"
language="apex"
since="6.7.0"
message="Use one statement for each line, it enhances code readability."
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#onedeclarationperline"
>
<description>
Apex allows the use of several variables declaration of the same type on one line. However, it
can lead to quite messy code. This rule looks for several declarations on the same line.
</description>
<priority>1</priority>
<properties>
<property name="version" value="2.0" />
<property name="xpath">
<value>
<![CDATA[
//VariableDeclarationStatements
[count(VariableDeclaration) > 1]
[$strictMode or count(distinct-values(VariableDeclaration/@BeginLine)) != count(VariableDeclaration)]
|
//FieldDeclarationStatements
[count(FieldDeclaration) > 1]
[$strictMode or count(distinct-values(FieldDeclaration/VariableExpression/@BeginLine)) != count(FieldDeclaration/VariableExpression)]
]]>
</value>
</property>
<property name="version" value="2.0" />
<property
name="strictMode"
type="Boolean"
value="false"
description="If true, mark combined declaration even if the declarations are on separate lines."
/>
</properties>
<example>
<![CDATA[
Integer a, b; // not recommended
Integer a,
b; // ok by default, can be flagged setting the strictMode property
Integer a; // preferred approach
Integer b;
]]>
</example>
</rule>
<rule
name="PropertyNamingConventions"
language="apex"
since="6.15.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.PropertyNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#propertynamingconventions"
>
<description>
Configurable naming conventions for property declarations. This rule reports
property declarations which do not match the regex that applies to their
specific kind (e.g. static property, or instance property). Each regex can be
configured through properties.
By default this rule uses the standard Apex naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
public Integer instanceProperty { get; set; } // This is in camel case, so it's ok
public Integer INSTANCE_PROPERTY { get; set; } // This will be reported unless you change the regex
}
]]>
</example>
</rule>
<rule
name="VariableNamingConventions"
language="apex"
since="5.5.0"
deprecated="true"
message="{0} variable {1} should begin with {2}"
class="net.sourceforge.pmd.lang.apex.rule.codestyle.VariableNamingConventionsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#variablenamingconventions"
>
<description>
A variable naming conventions rule - customize this to your liking. Currently, it
checks for final variables that should be fully capitalized and non-final variables
that should not include underscores.
This rule is deprecated and will be removed with PMD 7.0.0. The rule is replaced
by the more general rules {% rule "apex/codestyle/FieldNamingConventions" %},
{% rule "apex/codestyle/FormalParameterNamingConventions" %},
{% rule "apex/codestyle/LocalVariableNamingConventions" %}, and
{% rule "apex/codestyle/PropertyNamingConventions" %}.
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
public static final Integer MY_NUM = 0;
public String myTest = '';
DataModule dmTest = new DataModule();
}
]]>
</example>
</rule>
<rule
name="WhileLoopsMustUseBraces"
language="apex"
since="5.6.0"
message="Avoid using 'while' statements without curly braces"
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_codestyle.html#whileloopsmustusebraces"
>
<description>
Avoid using 'while' statements without using braces to surround the code block. If the code
formatting or indentation is lost then it becomes difficult to separate the code being
controlled from the rest.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0" />
<property name="xpath">
<value>
<![CDATA[
//WhileLoopStatement/BlockStatement[@CurlyBrace= false()]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
while (true) // not recommended
x++;
while (true) { // preferred approach
x++;
}
]]>
</example>
</rule>
<!--
* Design - 26 Aug 2020
* Rules that help you discover design issues.
* https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/resources/category/apex/design.xml
-->
<rule
name="AvoidDeeplyNestedIfStmts"
language="apex"
since="5.5.0"
message="Deeply nested if..then statements are hard to read"
class="net.sourceforge.pmd.lang.apex.rule.design.AvoidDeeplyNestedIfStmtsRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#avoiddeeplynestedifstmts"
>
<description>
Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar(Integer x, Integer y, Integer z) {
if (x>y) {
if (y>z) {
if (z==x) {
// !! too deep
}
}
}
}
}
]]>
</example>
</rule>
<rule
name="CyclomaticComplexity"
language="apex"
message="The {0} ''{1}'' has a{2} cyclomatic complexity of {3}."
since="6.0.0"
class="net.sourceforge.pmd.lang.apex.rule.design.CyclomaticComplexityRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#cyclomaticcomplexity"
>
<description>
The complexity of methods directly affects maintenance costs and readability. Concentrating too much decisional logic
in a single method makes its behaviour hard to read and change.
Cyclomatic complexity assesses the complexity of a method by counting the number of decision points in a method,
plus one for the method entry. Decision points are places where the control flow jumps to another place in the
program. As such, they include all control flow statements, such as 'if', 'while', 'for', and 'case'.
Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote moderate complexity, 8-10 denote
high complexity, and 11+ is very high complexity. By default, this rule reports methods with a complexity >= 10.
Additionally, classes with many methods of moderate complexity get reported as well once the total of their
methods' complexities reaches 40, even if none of the methods was directly reported.
Reported methods should be broken down into several smaller methods. Reported classes should probably be broken down
into subcomponents.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Complicated {
public void example() { // This method has a cyclomatic complexity of 12
int x = 0, y = 1, z = 2, t = 2;
boolean a = false, b = true, c = false, d = true;
if (a && b || b && d) {
if (y == z) {
x = 2;
} else if (y == t && !d) {
x = 2;
} else {
x = 2;
}
} else if (c && d) {
while (z < y) {
x = 2;
}
} else {
for (int n = 0; n < t; n++) {
x = 2;
}
}
}
}
]]>
</example>
</rule>
<rule
name="CognitiveComplexity"
language="apex"
message="The {0} ''{1}'' has a{2} cognitive complexity of {3}."
since="6.22.0"
class="net.sourceforge.pmd.lang.apex.rule.design.CognitiveComplexityRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#cognitivecomplexity"
>
<description>
Methods that are highly complex are difficult to read and more costly to maintain. If you include too much decisional
logic within a single method, you make its behavior hard to understand and more difficult to modify.
Cognitive complexity is a measure of how difficult it is for humans to read and understand a method. Code that contains
a break in the control flow is more complex, whereas the use of language shorthands doesn't increase the level of
complexity. Nested control flows can make a method more difficult to understand, with each additional nesting of the
control flow leading to an increase in cognitive complexity.
Information about Cognitive complexity can be found in the original paper here:
https://www.sonarsource.com/docs/CognitiveComplexity.pdf
By default, this rule reports methods with a complexity of 15 or more. Reported methods should be broken down into less
complex components.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
// Has a cognitive complexity of 0
public void createAccount() {
Account account = new Account(Name = 'PMD');
insert account;
}
// Has a cognitive complexity of 1
public Boolean setPhoneNumberIfNotExisting(Account a, String phone) {
if (a.Phone == null) { // +1
a.Phone = phone;
update a;
return true;
}
return false;
}
// Has a cognitive complexity of 5
public void updateContacts(List<Contact> contacts) {
List<Contact> contactsToUpdate = new List<Contact>();
for (Contact contact : contacts) { // +1
if (contact.Department == 'Finance') { // +2
contact.Title = 'Finance Specialist';
contactsToUpdate.add(contact);
} else if (contact.Department == 'Sales') { // +2
contact.Title = 'Sales Specialist';
contactsToUpdate.add(contact);
}
}
update contactsToUpdate;
}
}
]]>
</example>
</rule>
<rule
name="ExcessiveClassLength"
language="apex"
since="5.5.0"
message="Avoid really long classes."
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessiveClassLengthRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveclasslength"
>
<description>
Excessive class file lengths are usually indications that the class may be burdened with excessive
responsibilities that could be provided by external classes or functions. In breaking these methods
apart the code becomes more managable and ripe for reuse.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar1() {
// 1000 lines of code
}
public void bar2() {
// 1000 lines of code
}
public void bar3() {
// 1000 lines of code
}
public void barN() {
// 1000 lines of code
}
}
]]>
</example>
</rule>
<rule
name="ExcessiveParameterList"
language="apex"
since="5.5.0"
message="Avoid long parameter lists."
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessiveParameterListRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist"
>
<description>
Methods with numerous parameters are a challenge to maintain, especially if most of them share the
same datatype. These situations usually denote the need for new objects to wrap the numerous parameters.
</description>
<priority>3</priority>
<example>
<![CDATA[
// too many arguments liable to be mixed up
public void addPerson(Integer birthYear, Integer birthMonth, Integer birthDate, Integer height, Integer weight, Integer ssn) {
// ...
}
// preferred approach
public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) {
// ...
}
]]>
</example>
</rule>
<rule
name="ExcessivePublicCount"
language="apex"
since="5.5.0"
message="This class has a bunch of public methods and attributes"
class="net.sourceforge.pmd.lang.apex.rule.design.ExcessivePublicCountRule"
externalInfoUrl="https://pmd.github.io/latest/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
smaller ones not only increases testability and reliability but also allows new variations to be
developed easily.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public String value;
public Bar something;
public Variable var;
// [... more more public attributes ...]
public void doWork() {}
public void doMoreWork() {}
public void doWorkAgain() {}
// [... more more public methods ...]
}
]]>
</example>
</rule>
<rule
name="NcssConstructorCount"
language="apex"
since="5.5.0"
message="The constructor has an NCSS line count of {0}"
class="net.sourceforge.pmd.lang.apex.rule.design.NcssConstructorCountRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#ncssconstructorcount"
>
<description>
This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines
of code for a given constructor. NCSS ignores comments, and counts actual statements. Using this algorithm,
lines of code that are split are counted as one.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo extends Bar {
//this constructor only has 1 NCSS lines
public Foo() {
super();
super.foo();
}
}
]]>
</example>
</rule>
<rule
name="NcssMethodCount"
language="apex"
since="5.5.0"
message="The method ''{0}()'' has an NCSS line count of {1}"
class="net.sourceforge.pmd.lang.apex.rule.design.NcssMethodCountRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#ncssmethodcount"
>
<description>
This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines
of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm,
lines of code that are split are counted as one.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo extends Bar {
//this method only has 1 NCSS lines
public Integer method() {
super.method();
return 1;
}
}
]]>
</example>
</rule>
<rule
name="NcssTypeCount"
language="apex"
since="5.5.0"
message="The type has an NCSS line count of {0}"
class="net.sourceforge.pmd.lang.apex.rule.design.NcssTypeCountRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#ncsstypecount"
>
<description>
This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines
of code for a given type. NCSS ignores comments, and counts actual statements. Using this algorithm,
lines of code that are split are counted as one.
</description>
<priority>3</priority>
<example>
<![CDATA[
//this class only has 6 NCSS lines
public class Foo extends Bar {
public Foo() {
super();
super.foo();
}
}
]]>
</example>
</rule>
<rule
name="StdCyclomaticComplexity"
language="apex"
since="5.5.0"
message="The {0} ''{1}'' has a Standard Cyclomatic Complexity of {2}."
class="net.sourceforge.pmd.lang.apex.rule.design.StdCyclomaticComplexityRule"
externalInfoUrl="https://pmd.github.io/latest/pmd_rules_apex_design.html#stdcyclomaticcomplexity"
>
<description>
Complexity directly affects maintenance costs is determined by the number of decision points in a method