[apex] Fix AvoidDeeplyNestedIfStmts for triggers

This commit is contained in:
Andreas Dangel 2024-09-06 18:40:43 +02:00
parent 7ba88e9eba
commit 92bab6a38f
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
2 changed files with 35 additions and 4 deletions

View File

@ -7,10 +7,10 @@ package net.sourceforge.pmd.lang.apex.rule.design;
import static net.sourceforge.pmd.properties.NumericConstraints.positive;
import net.sourceforge.pmd.lang.apex.ast.ASTIfBlockStatement;
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.reporting.RuleContext;
public class AvoidDeeplyNestedIfStmtsRule extends AbstractApexRule {
@ -28,11 +28,9 @@ public class AvoidDeeplyNestedIfStmtsRule extends AbstractApexRule {
}
@Override
public Object visit(ASTUserClass node, Object data) {
public void start(RuleContext ctx) {
depth = 0;
depthLimit = getProperty(PROBLEM_DEPTH_DESCRIPTOR);
return super.visit(node, data);
}
@Override

View File

@ -90,6 +90,39 @@ public class Foo {
}
}
}
]]></code>
</test-code>
<test-code>
<description>if statements within trigger (#5138)</description>
<rule-property name="problemDepth">3</rule-property>
<expected-problems>2</expected-problems>
<expected-linenumbers>11,14</expected-linenumbers>
<code><![CDATA[
// https://github.com/SalesforceLabs/Partner-Onboarding/blob/main/Partner%20Onboarding/triggers/AccountTrigger.trigger
trigger AccountTrigger on Account (after insert, after update) {
public List<Account> updatedAccounts = Trigger.new;
if(trigger.isAfter){
Map<Id,Account> mapOfOldValues = trigger.oldMap;
Map<Id,Id> accountWithOnboardingTemplateIDs = new Map<Id,Id>();
for(Account updated_Account : updatedAccounts) {
if(updated_Account.Onboarding_Plan__c != null)
{
if(mapOfOldValues == null){
accountWithOnboardingTemplateIDs.put(updated_Account.Id, updated_Account.Onboarding_Plan__c);
} else {
if(updated_Account.Onboarding_Plan__c != mapOfOldValues.get(updated_Account.Id).Onboarding_Plan__c)
accountWithOnboardingTemplateIDs.put(updated_Account.Id, updated_Account.Onboarding_Plan__c);
}
}
} //End For
if(accountWithOnboardingTemplateIDs.size() > 0){
OnboardingPlanService.updateContactsWithOndbPlan(accountWithOnboardingTemplateIDs);
}
}
}
]]></code>
</test-code>
</test-data>