Skip to content

Commit 1b976c4

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
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
1 parent c36922b commit 1b976c4

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,6 @@ parameters:
132132
count: 1
133133
path: src/Collectors/Registry.php
134134

135-
-
136-
rawMessage: Anonymous function has an unused use $container.
137-
identifier: closure.unusedUse
138-
count: 1
139-
path: src/Command/CommandHelper.php
140-
141135
-
142136
rawMessage: 'Call to static method expand() of internal class Nette\DI\Helpers from outside its root namespace Nette.'
143137
identifier: staticMethod.internalClass
@@ -759,12 +753,6 @@ parameters:
759753
count: 1
760754
path: src/Testing/LevelsTestCase.php
761755

762-
-
763-
rawMessage: Anonymous function has an unused use $container.
764-
identifier: closure.unusedUse
765-
count: 1
766-
path: src/Testing/PHPStanTestCase.php
767-
768756
-
769757
rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.'
770758
identifier: phpstanApi.instanceofType

src/Rules/UnusedFunctionParametersCheck.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ private function getUsedVariables(Scope $scope, $node): array
7979
return $scope->getDefinedVariables();
8080
}
8181
}
82+
if ($node instanceof Node\Expr\Include_) {
83+
return $scope->getDefinedVariables();
84+
}
8285
if ($node instanceof Variable && is_string($node->name) && $node->name !== 'this') {
8386
return [$node->name];
8487
}

tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public function testUnusedClosureUses(): void
3535
]);
3636
}
3737

38+
public function testBug13960(): void
39+
{
40+
$this->analyse([__DIR__ . '/data/bug-13960.php'], []);
41+
}
42+
3843
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13960;
4+
5+
$aap = 684;
6+
7+
$a = function() use ($aap) {
8+
require __DIR__ . '/echo_the_value_of_aap.php';
9+
};
10+
11+
$b = function() use ($aap) {
12+
include __DIR__ . '/echo_the_value_of_aap.php';
13+
};
14+
15+
$c = function() use ($aap) {
16+
require_once __DIR__ . '/echo_the_value_of_aap.php';
17+
};
18+
19+
$d = function() use ($aap) {
20+
include_once __DIR__ . '/echo_the_value_of_aap.php';
21+
};

0 commit comments

Comments
 (0)