Pmd: [apex]聽NCSS Method length is incorrect when using method chaining

Created on 10 Jan 2017  路  8Comments  路  Source: pmd/pmd

If you analyze the following source code with the NcssMethodCountRule

https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/complexity/NcssMethodCountRule.java

@isTest
private class AcceptanceTests_Test {

    @isTest
    private static void test() {  

        // Setup
        Opportunity o1 = new Opportunity()
                                    .add(new Contact().foo(1)  .bar(1).year(2012)  .bar(1).price(5)  .vol(100))
                                    .add(new Contact().foo(1)  .bar(2).year(2013)  .bar(1).price(5)  .vol(110))
                                    .add(new Contact().foo(1)  .bar(3).year(2014)  .bar(1).price(5)  .vol(120))
                                    .add(new Contact().foo(1)  .bar(4).year(2015)  .bar(1).price(5)  .vol(130))
                                    .persist();

        Opportunity o2 = new Opportunity()
                                    .add(new Contact().foo(1)  .PRS(1).year(2012)  .bar(1).price(5)  .vol(110))
                                    .add(new Contact().foo(1)  .PRS(2).year(2013)  .bar(1).price(5)  .vol(120))
                                    .add(new Contact().foo(1)  .PRS(3).year(2014)  .bar(1).price(5)  .vol(130))
                                    .add(new Contact().foo(1)  .PRS(4).year(2015)  .bar(1).price(5)  .vol(140))
                                    .persist();

        Opportunity o3 = new Opportunity()
                                    .add(new Contact().foo(1)  .PRS(1).year(2012)  .bar(1).price(5)  .vol(110))
                                    .add(new Contact().foo(1)  .PRS(2).year(2013)  .bar(1).price(5)  .vol(120))
                                    .add(new Contact().foo(1)  .PRS(3).year(2014)  .bar(1).price(5)  .vol(130))
                                    .add(new Contact().foo(1)  .PRS(4).year(2015)  .bar(1).price(5)  .vol(140))
                                    .persist();


        // Exercise     
        callMethod();


        // Verify
        System.assert(attribute());
    }
}

the test method marked as issue with over 80 NCSS. Actually it is less than 30. The method chaining in // Setup seems to confuse the PMD line detection.
cursor_und_adm_acceptancetests_test_cls_at_master_ _up2go_adm

bug

All 8 comments

@adangel I recall that back then when we integrated the Salesforce Apex parser you wrote the startLine() endLine() implementation because we didn't get how it works.

Would you mind checking if method chaining is confusing you implementation?

@up2go-rsoesemann Thanks for the report. I can confirm the issue is reproducible. I'm working on a fix.

I just submitted a fix for this issue. Bare in mind however that this check is not actually counting lines but statements. In your example, it would count 5 + 1 (method name).

This is explicit in the documentation:

NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

Check it here

@jsotuyod I still see that behaviour in PMD 5.5.3. Can you please reopen this?

@up2go-rsoesemann sure, can you please provide an updated code snippet failing under PMD 5.5.3? The original was added to unit tests and is for sure passing.

Sure @jsotuyod It happened in private class so I had to obfuscate it a bit. Please find it here: https://dl.dropboxusercontent.com/u/240888/WrongNcssCount.cls

PMD says The type has an NCSS line count of 704 although there are only 486 lines.

@up2go-rsoesemann awesome!

I'll try to reproduce it in a unit test and let you know.

PMD says The type has an NCSS line count of 704 although there are only 486 lines.

@up2go-rsoesemann wait, this is a different issue then. It's not an issue on NcssMethodCountRule, but on NcssTypeCountRule.

I'll move this to a new issue to keep it tidy / better changelog followup.

Was this page helpful?
0 / 5 - 0 ratings