Hi, we are currently running a self-hosted SonarQube for our PHP 8.3 project but receive some issues when using function calls in combination with variables passend by reference to functions. In these cases SonarLint and SonarQube shows an issue for the inner variable “php:S1481: Unused local variables should be removed” although it is used in the following code.
- Language: PHP 8.3
- Rule: php:S1481 “Unused local variables should be removed”
- SonarQube Developer Edition v10.7
Example with 1 issue:
Line 7: Remove this unused "$someVariable" local variable.
class SomeClassWithAnIssue
{
public function doSomething()
{
$someVariable = 'some value';
$this->task("Reading File", function () use (&$someVariable) {
$someVariable = 'some other value';
});
$this->task("Some task name", function () use ($someVariable) {
$this->doSomethingMore($someVariable);
});
}
private function doSomethingMore($someVariable)
{
echo $someVariable . "\n";
}
}
The variable $someVariable
is used in the following and should not trigger an issue. The issue arises when the variable is passed to another function like above, and does not occure, when it is used like in the following example:
Example with no issues:
class SomeClassWithoutAnyIssues
{
public function doSomething()
{
$someVariable = 'some value';
$this->task("Reading File", function () use (&$someVariable) {
$someVariable = 'some other value';
});
$this->doSomethingMore($someVariable);
}
private function doSomethingMore($someVariable)
{
echo $someVariable . "\n";
}
}
Therefore i think this is an false-positive (or Bug) in the detection.
Thanks.