Skip to content

Commit 501ac99

Browse files
phpstan-botstaabm
andauthored
Treat all in-scope variables as used when a closure or constructor body contains include/require/eval (#6056)
Co-authored-by: staabm <120441+staabm@users.noreply.github.com> Co-authored-by: Markus Staab <markus.staab@redaxo.de>
1 parent e7b5024 commit 501ac99

5 files changed

Lines changed: 37 additions & 12 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@ parameters:
150150
count: 1
151151
path: src/Collectors/Registry.php
152152

153-
-
154-
rawMessage: Anonymous function has an unused use $container.
155-
identifier: closure.unusedUse
156-
count: 1
157-
path: src/Command/CommandHelper.php
158-
159153
-
160154
rawMessage: 'Call to static method expand() of internal class Nette\DI\Helpers from outside its root namespace Nette.'
161155
identifier: staticMethod.internalClass
@@ -771,12 +765,6 @@ parameters:
771765
count: 1
772766
path: src/Testing/LevelsTestCase.php
773767

774-
-
775-
rawMessage: Anonymous function has an unused use $container.
776-
identifier: closure.unusedUse
777-
count: 1
778-
path: src/Testing/PHPStanTestCase.php
779-
780768
-
781769
rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.'
782770
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_ || $node instanceof Node\Expr\Eval_) {
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/Classes/UnusedConstructorParametersRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public function testBug11454(): void
7676
$this->analyse([__DIR__ . '/data/bug-11454.php'], []);
7777
}
7878

79+
public function testParameterUsedInIncludedFile(): void
80+
{
81+
$this->analyse([__DIR__ . '/data/unused-constructor-parameters-include.php'], []);
82+
}
83+
7984
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace UnusedConstructorParametersInclude;
4+
5+
class Foo
6+
{
7+
8+
public function __construct(
9+
$usedInIncludedFile
10+
)
11+
{
12+
require_once __DIR__ . '/foo.php';
13+
}
14+
15+
}

‎tests/PHPStan/Rules/Functions/data/unused-closure-uses.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@ function ($anotherUnused) use ($usedInClosureUse) {
1111
echo $anotherUnused; // different scope
1212
};
1313
};
14+
15+
$container = new stdClass();
16+
17+
function () use ($container) {
18+
require_once __DIR__ . '/foo.php';
19+
};
20+
21+
function () use ($container) {
22+
include 'foo.php';
23+
};
24+
25+
function () use ($container) {
26+
eval('echo $container;');
27+
};

0 commit comments

Comments
 (0)