Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Rules/UnusedFunctionParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,21 @@ private function getUsedVariables(Scope $scope, $node): array
if ($node instanceof Node\Expr\Include_ || $node instanceof Node\Expr\Eval_) {
return $scope->getDefinedVariables();
}
if ($node instanceof Variable && is_string($node->name) && $node->name !== 'this') {
return [$node->name];
if ($node instanceof Variable) {
if (is_string($node->name)) {
if ($node->name !== 'this') {
return [$node->name];
}
} else {
$nameType = $scope->getType($node->name);
if ($nameType->getConstantStrings() === []) {
return $scope->getDefinedVariables();
}

foreach ($nameType->getConstantStrings() as $constantString) {
$variableNames[] = $constantString->getValue();
}
}
}
if ($node instanceof Node\ClosureUse && is_string($node->var->name)) {
return [$node->var->name];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ public function __construct(
}

}

class Bar
{

public function __construct(
$usedViaVariableVariable,
$name
)
{
echo $$name;
}

}
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function testUnusedClosureUses(): void
'Anonymous function has an unused use $usedInClosureUse.',
10,
],
[
'Anonymous function has an unused use $container.',
43,
],
]);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/data/unused-closure-uses.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ function () use ($container) {
function () use ($container) {
eval('echo $container;');
};

$aap = 684;

$a = function () use ($aap) {
require __DIR__ . '/echo_the_value_of_aap.php';
};

$name = 'container';

function () use ($container, $name) {
echo $$name;
};

$otherName = 'notContainer';

function () use ($container, $otherName) {
echo $$otherName;
};
Loading