phpmd/phpmd
False positive unused variable when that variable is a reference
Fechada
#927 aberto em 12 de dez. de 2021
BugGood first issue
Métricas do repositório
- Stars
- (2.137 estrelas)
- Métricas de merge de PR
- (Mesclagem média 31d 6h) (2 fundiu PRs em 30d)
Description
- PHPMD version: 2.10.2snapshot202107221018
- PHP Version: 8.0.13
- Installation type: phar file
- Operating System / Distribution & Version: Arch Linux
Current Behavior
PHPMD doesn't seem to have special handling for variables that are references (though it does seem to handle reference parameters correctly).
<?php
function func() {
$a = 1;
$b = &$a;
$b = 2;
print($a);
}
func();
$ ./phpmd.phar test.php text unusedcode
test.php:5 Avoid unused local variables such as '$b'.
However, $b is used.
#720 seems relevant but distinct.
Here's a less simplified example demonstrating why someone might use this:
<?php
class A {
private static $cache = [];
private static function &GetCache() {
// Imagine that instead of returning the same cache, this instead
// was shared logic to decide one of a number of caches to return
return static::$cache;
}
public static function ClearCache() {
$cache = &static::GetCache();
$cache = [];
}
public static function InsertToCache($key, $value) {
$cache = &static::GetCache();
$cache[$key] = $value;
}
}
PHPMD warns about ClearCache. (Curiously, it doesn't warn about InsertToCache- testing elsewhere, it seems like an array insertion is considered using a variable?)