[apex] move to style ruleset and add literal expression in arrayload exception
This commit is contained in:
@ -78,41 +78,3 @@ public class Something {
|
||||
``` xml
|
||||
<rule ref="rulesets/apex/performance.xml/AvoidSoqlInLoops" />
|
||||
```
|
||||
|
||||
|
||||
## 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
|
||||
<rule ref="rulesets/apex/performance.xml/AvoidDirectAccessTriggerMap" />
|
||||
```
|
||||
|
||||
|
@ -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
|
||||
<rule ref="rulesets/apex/style.xml/AvoidDirectAccessTriggerMap" />
|
||||
```
|
||||
|
||||
## AvoidGlobalModifier
|
||||
|
||||
**Since:** PMD 5.5.0
|
||||
|
@ -50,38 +50,6 @@ public class Something {
|
||||
}
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidDirectAccessTriggerMap"
|
||||
since="6.0.0"
|
||||
message="Avoid directly accessing Trigger.old and Trigger.new"
|
||||
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_performance.html#avoiddirectaccesstriggermap">
|
||||
<description>
|
||||
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.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//ArrayLoadExpression/TriggerVariableExpression
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
|
||||
<example>
|
||||
<![CDATA[
|
||||
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.
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
@ -113,7 +113,7 @@
|
||||
<property name="cc_block_highlighting" value="false" />
|
||||
</properties>
|
||||
</rule>
|
||||
<rule ref="rulesets/apex/performance.xml/AvoidDirectAccessTriggerMap" message="Avoid directly accessing Trigger.old and Trigger.new">
|
||||
<rule ref="rulesets/apex/style.xml/AvoidDirectAccessTriggerMap" message="Avoid directly accessing Trigger.old and Trigger.new">
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<!-- relevant for Code Climate output only -->
|
||||
|
@ -138,4 +138,37 @@ global class Unchangeable {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
|
||||
<rule name="AvoidDirectAccessTriggerMap"
|
||||
since="6.0.0"
|
||||
message="Avoid directly accessing Trigger.old and Trigger.new"
|
||||
class="net.sourceforge.pmd.lang.apex.rule.ApexXPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_style.html#avoiddirectaccesstriggermap">
|
||||
<description>
|
||||
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.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//ArrayLoadExpression/TriggerVariableExpression | //ArrayLoadExpression/LiteralExpression
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
|
||||
<example>
|
||||
<![CDATA[
|
||||
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.
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
|
@ -14,6 +14,5 @@ public class PerformanceRulesTest extends SimpleAggregatorTst {
|
||||
public void setUp() {
|
||||
addRule(RULESET, "AvoidSoqlInLoops");
|
||||
addRule(RULESET, "AvoidDmlStatementsInLoops");
|
||||
addRule(RULESET, "AvoidDirectAccessTriggerMap");
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
<test-code>
|
||||
<description>Directly accessing the Trigger.old map.</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-problems>2</expected-problems>
|
||||
<code><![CDATA[
|
||||
trigger AccountTrigger on Account (before insert, before update) {
|
||||
Account a = Trigger.new[0]; //Bad: Accessing the trigger array directly is not recommended.
|
Reference in New Issue
Block a user