[apex] move to style ruleset and add literal expression in arrayload exception

This commit is contained in:
Jan Aertgeerts
2017-10-07 23:28:38 +02:00
parent 2639b4fe66
commit dedf954cb0
8 changed files with 73 additions and 74 deletions

View File

@ -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" />
```

View File

@ -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

View File

@ -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>

View File

@ -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 -->

View File

@ -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>

View File

@ -14,6 +14,5 @@ public class PerformanceRulesTest extends SimpleAggregatorTst {
public void setUp() {
addRule(RULESET, "AvoidSoqlInLoops");
addRule(RULESET, "AvoidDmlStatementsInLoops");
addRule(RULESET, "AvoidDirectAccessTriggerMap");
}
}

View File

@ -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");

View File

@ -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.