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