Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apex] switch statements do not contribute to Cyclomatic Complexity numbers #4863

Open
aidan-harding opened this issue Mar 19, 2024 · 1 comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code in:documentation Affects the documentation

Comments

@aidan-harding
Copy link
Contributor

aidan-harding commented Mar 19, 2024

I'm happy to fork the repo and have a go at fixing this if you're happy to accept the changes once you're happy with them.

Affects PMD Version: 6.55

Rule:

Cyclomatic Complexity

Description:

The cyclomatic complexity of a method should be increased by adding a switch statement, but it does not.

Moreover, the documentation states that it covers:

As such, they include all control flow statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.

Which is not only not true, but also syntactically incorrect, as in Apex the keyword when is used instead of case.

Code Sample demonstrating the issue:

public class CyclomaticComplexity {
    void foo() {
        switch on Trigger.operationType {
            when BEFORE_INSERT {
                System.debug('Before Insert');
            }
            when AFTER_INSERT {
                System.debug('After Insert');
            }
        }
    }
}

Expected outcome:

PMD should report a cyclomatic complexity of 3 (starts at one, then +1 for each branch in the switch). It actually reports a cyclomatic complexity of 1.

Running PMD through: CLI

@aidan-harding aidan-harding added the a:false-negative PMD doesn't flag a problematic piece of code label Mar 19, 2024
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Mar 20, 2024
@aidan-harding
Copy link
Contributor Author

I checked this in 7.0.0-rc4 and it has the same behaviour.

@adangel adangel changed the title [Apex] switch statements do not contribute to Cyclomatic Complexity numbers [apex] switch statements do not contribute to Cyclomatic Complexity numbers Mar 21, 2024
@jsotuyod jsotuyod added in:documentation Affects the documentation and removed needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale labels Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code in:documentation Affects the documentation
Projects
None yet
Development

No branches or pull requests

2 participants