From 1b976c46162ea9420a830a087e97cbbbb5013abf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 30 Mar 2026 23:04:11 +0000 Subject: [PATCH] Fix closure.unusedUse false positive when closure contains include/require - Treat include/require/include_once/require_once as using all variables in scope - Included files inherit the variable scope of the including line - Same approach as existing get_defined_vars() and func_get_args() handling - Removed two now-unnecessary baseline entries for CommandHelper.php and PHPStanTestCase.php - New regression test in tests/PHPStan/Rules/Functions/data/bug-13960.php Fixes phpstan/phpstan#13960 --- phpstan-baseline.neon | 12 ----------- src/Rules/UnusedFunctionParametersCheck.php | 3 +++ .../Functions/UnusedClosureUsesRuleTest.php | 5 +++++ .../Rules/Functions/data/bug-13960.php | 21 +++++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-13960.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6906e22da9d..7c5d2f6b204 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -132,12 +132,6 @@ parameters: count: 1 path: src/Collectors/Registry.php - - - rawMessage: Anonymous function has an unused use $container. - identifier: closure.unusedUse - count: 1 - path: src/Command/CommandHelper.php - - rawMessage: 'Call to static method expand() of internal class Nette\DI\Helpers from outside its root namespace Nette.' identifier: staticMethod.internalClass @@ -759,12 +753,6 @@ parameters: count: 1 path: src/Testing/LevelsTestCase.php - - - rawMessage: Anonymous function has an unused use $container. - identifier: closure.unusedUse - count: 1 - path: src/Testing/PHPStanTestCase.php - - rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.' identifier: phpstanApi.instanceofType diff --git a/src/Rules/UnusedFunctionParametersCheck.php b/src/Rules/UnusedFunctionParametersCheck.php index d35d5c4a369..7f1b497d05b 100644 --- a/src/Rules/UnusedFunctionParametersCheck.php +++ b/src/Rules/UnusedFunctionParametersCheck.php @@ -79,6 +79,9 @@ private function getUsedVariables(Scope $scope, $node): array return $scope->getDefinedVariables(); } } + if ($node instanceof Node\Expr\Include_) { + return $scope->getDefinedVariables(); + } if ($node instanceof Variable && is_string($node->name) && $node->name !== 'this') { return [$node->name]; } diff --git a/tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php b/tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php index 137257162c7..86ff28ca4e7 100644 --- a/tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php @@ -35,4 +35,9 @@ public function testUnusedClosureUses(): void ]); } + public function testBug13960(): void + { + $this->analyse([__DIR__ . '/data/bug-13960.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-13960.php b/tests/PHPStan/Rules/Functions/data/bug-13960.php new file mode 100644 index 00000000000..82dcb25e1bd --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-13960.php @@ -0,0 +1,21 @@ +