Multiline method calls increase method LOC (PHP:S138)

Environment specification:
SonarQube: 8.7.0.41497
SonarPHP: 3.15.0.7197
Scanner: 4.6.2.2472


Hi, my colleagues and I have noticed a specific (and quite irritating) behavior of the PHP plugin regarding LOC measurement for methods done by rule PHP:S138 combined with IDE code formatting for lines with more than 120 characters. In our case the rule is configured to allow max. 30 lines.

Imagine the following class Example with several methods. The class itself doesn’t make any sense. :wink: Due to code formatting automatically splitting lines with > 120 chars into separate lines, the method call of methodToCall got split into multiple lines producing a total line count of 34 for doSomething which is more than the allowed 30 defined by the mentioned rule.

class Example
{
    public function doSomething(int $a): bool
    {
        $veryLongVariableNameA = $a;
        $veryLongVariableNameB = 2;
        $veryLongVariableNameC = 3;
        $veryLongVariableNameD = 4;
        $veryLongVariableNameE = 5;
        $veryLongVariableNameF = 6;

        if ($veryLongVariableNameA > 5) {
            $this->methodToCall(
                $veryLongVariableNameA,
                $veryLongVariableNameB,
                $veryLongVariableNameC,
                $veryLongVariableNameD,
                $veryLongVariableNameE,
                $veryLongVariableNameF,
                4711
            );

            return true;
        }
        
        if ($veryLongVariableNameA !== 1) {
            $this->methodToCall(
                $this->pong($veryLongVariableNameA),
                $this->pong($veryLongVariableNameB),
                $this->pong($veryLongVariableNameC),
                $this->pong($veryLongVariableNameD),
                $this->pong($veryLongVariableNameE),
                $this->pong($veryLongVariableNameF),
                $this->otherStuff()
            );
            
            return true;
        }
        
        return false;
    }

    private function otherStuff(): int
    {
        return 4711;
    }

    private function pong(int $input): int
    {
        return $input;
    }

    private function methodToCall(int $a, int $b, int $c, int $d, int $e, int $f, int $additional): void
    {
        var_dump([$a, $b, $c, $d, $e, $f, $additional]);
    }
}

Our first thought was, that it is OK-ish to include that sort of line breaks into LOC measure in case other methods are invoked to retrieve dynamic values passed as parameters (as done at the bottom of doSomething). In that case “external” code is executed in these lines.
We don’t understand why a method call with only variables passed as parameters (and line breaks due to line length) increases the actual LOC for method doSomething. This could/would/should be just one line, in our opinion.

Can someone please explain how rule PHP:S138 actually counts the LOC? Did we misunderstood the rule and should better increase our configured threshold (which is 30; the default is 120) or is this an incorrect plugin behavior?

Kind regards,
Steven

Hey @justus_bunsi,

The way the rule works is the same as the “Lines of Code” measure is calculated for the files: Every line that contains actual code counts, but comment or empty lines do not.
I believe this is in accordance with the title of the rule “Functions should not have too many lines of code”, and it is also how the rule works for all the other languages.

Setting the threshold to 30 might be too low. Especially if you have this kind of breaks often in your code.

I do see your point. It might be an idea for another rule about limiting the number of statements. But, I believe we did not see enough value in it yet to have it.

If you want to limit the complexity of your functions’ bodies, rule S3776 might be something to look into too.

Best,
Karim.

Marking this thread as resolved. Let me know if you don’t see it as such.

Best,
Karim.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.