diff --git a/docs/pages/pmd/rules/apex/performance.md b/docs/pages/pmd/rules/apex/performance.md index 5b87cd8cc2..cda3581ea8 100644 --- a/docs/pages/pmd/rules/apex/performance.md +++ b/docs/pages/pmd/rules/apex/performance.md @@ -78,41 +78,3 @@ public class Something { ``` xml ``` - - -## AvoidDirectAccessTriggerMap - -**Since:** PMD 6.0.0 - -**Priority:** Medium (3) - -Avoid directly accessing Trigger.old and Trigger.new as it can lead to a bug. Triggers should be bulkified and iterate through the map to handle the actions for each item separately. - -``` -//ArrayLoadExpression/TriggerVariableExpression -``` - -**Example(s):** -``` -trigger AccountTrigger on Account (before insert, before update) { - Account a = Trigger.new[0]; //Bad: Accessing the trigger array directly is not recommended. - - foreach ( Account a : Trigger.new ){ - //Good: Iterate through the trigger.new array instead. - } -} -``` - -**This rule has the following properties:** - -|Name|Default Value|Description| -|----|-------------|-----------| -|cc_categories|[Style]|Code Climate Categories| -|cc_remediation_points_multiplier|1|Code Climate Remediation Points multiplier| -|cc_block_highlighting|false|Code Climate Block Highlighting| - -**Use this rule by referencing it:** -``` xml - -``` - diff --git a/docs/pages/pmd/rules/apex/style.md b/docs/pages/pmd/rules/apex/style.md index 660db734fd..4f3a16c5c4 100644 --- a/docs/pages/pmd/rules/apex/style.md +++ b/docs/pages/pmd/rules/apex/style.md @@ -5,8 +5,44 @@ permalink: pmd_rules_apex_style.html folder: pmd/rules/apex sidebaractiveurl: /pmd_rules_apex.html editmepath: ../pmd-apex/src/main/resources/rulesets/apex/style.xml -keywords: Style, VariableNamingConventions, MethodNamingConventions, ClassNamingConventions, MethodWithSameNameAsEnclosingClass, AvoidLogicInTrigger, AvoidGlobalModifier +keywords: Style, VariableNamingConventions, MethodNamingConventions, ClassNamingConventions, MethodWithSameNameAsEnclosingClass, AvoidLogicInTrigger, AvoidGlobalModifier, AvoidDirectAccessTriggerMap --- +## AvoidDirectAccessTriggerMap + +**Since:** PMD 6.0.0 + +**Priority:** Medium (3) + +Avoid directly accessing Trigger.old and Trigger.new as it can lead to a bug. Triggers should be bulkified and iterate through the map to handle the actions for each item separately. + +``` +//ArrayLoadExpression/TriggerVariableExpression | //ArrayLoadExpression/LiteralExpression +``` + +**Example(s):** +``` +trigger AccountTrigger on Account (before insert, before update) { + Account a = Trigger.new[0]; //Bad: Accessing the trigger array directly is not recommended. + + foreach ( Account a : Trigger.new ){ + //Good: Iterate through the trigger.new array instead. + } +} +``` + +**This rule has the following properties:** + +|Name|Default Value|Description| +|----|-------------|-----------| +|cc_categories|[Style]|Code Climate Categories| +|cc_remediation_points_multiplier|1|Code Climate Remediation Points multiplier| +|cc_block_highlighting|false|Code Climate Block Highlighting| + +**Use this rule by referencing it:** +``` xml + +``` + ## AvoidGlobalModifier **Since:** PMD 5.5.0 diff --git a/pmd-apex/src/main/resources/rulesets/apex/performance.xml b/pmd-apex/src/main/resources/rulesets/apex/performance.xml index a9bf247bc1..89e47f739c 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/performance.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/performance.xml @@ -50,38 +50,6 @@ public class Something { } } } -]]> - - - - - -Avoid directly accessing Trigger.old and Trigger.new as it can lead to a bug. Triggers should be bulkified and iterate through the map to handle the actions for each item separately. - - 3 - - - - - - - - - - diff --git a/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml b/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml index 583081b964..6d15c33a35 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/ruleset.xml @@ -113,7 +113,7 @@ - + 3 diff --git a/pmd-apex/src/main/resources/rulesets/apex/style.xml b/pmd-apex/src/main/resources/rulesets/apex/style.xml index 7c86b03a41..d6f5b9347b 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/style.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/style.xml @@ -138,4 +138,37 @@ global class Unchangeable { + + + +Avoid directly accessing Trigger.old and Trigger.new as it can lead to a bug. Triggers should be bulkified and iterate through the map to handle the actions for each item separately. + + 3 + + + + + + + + + + + + + diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/performance/PerformanceRulesTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/performance/PerformanceRulesTest.java index a64b338a5a..c89531d091 100644 --- a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/performance/PerformanceRulesTest.java +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/performance/PerformanceRulesTest.java @@ -14,6 +14,5 @@ public class PerformanceRulesTest extends SimpleAggregatorTst { public void setUp() { addRule(RULESET, "AvoidSoqlInLoops"); addRule(RULESET, "AvoidDmlStatementsInLoops"); - addRule(RULESET, "AvoidDirectAccessTriggerMap"); } } diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/style/StyleRulesTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/style/StyleRulesTest.java index f16234d462..88495c67a3 100644 --- a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/style/StyleRulesTest.java +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/style/StyleRulesTest.java @@ -12,6 +12,7 @@ public class StyleRulesTest extends SimpleAggregatorTst { @Override public void setUp() { + addRule(RULESET, "AvoidDirectAccessTriggerMap"); addRule(RULESET, "AvoidGlobalModifier"); addRule(RULESET, "AvoidLogicInTrigger"); addRule(RULESET, "ClassNamingConventions"); diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/performance/xml/AvoidDirectAccessTriggerMap.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/style/xml/AvoidDirectAccessTriggerMap.xml similarity index 95% rename from pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/performance/xml/AvoidDirectAccessTriggerMap.xml rename to pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/style/xml/AvoidDirectAccessTriggerMap.xml index 5a9287532a..9e3c7e384e 100644 --- a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/performance/xml/AvoidDirectAccessTriggerMap.xml +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/style/xml/AvoidDirectAccessTriggerMap.xml @@ -7,7 +7,7 @@ Directly accessing the Trigger.old map. - 1 + 2