Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19b8d7a

Browse files
fix: issue in defined variable rule instead of isset
1 parent 4782556 commit 19b8d7a

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

‎src/Rules/IssetCheck.php‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ public function __construct(
4141
*/
4242
public function check(Expr $expr, Scope $scope, string $operatorDescription, string $identifier, callable $typeMessageCallback, ?IdentifierRuleError $error = null): ?IdentifierRuleError
4343
{
44-
// [FIX] Ignore isset($$foo): dynamic variable
45-
if (
46-
$expr instanceof Node\Expr\Variable &&
47-
$expr->name instanceof Node\Expr\Variable
48-
) {
49-
return null;
50-
}
51-
5244
// mirrored in PHPStan\Analyser\MutatingScope::issetCheck()
5345
if ($expr instanceof Node\Expr\Variable && is_string($expr->name)) {
5446
$hasVariable = $scope->hasVariableType($expr->name);

‎src/Rules/Variables/DefinedVariableRule.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function getNodeType(): string
4141
public function processNode(Node $node, Scope $scope): array
4242
{
4343
$errors = [];
44+
45+
if ($scope->isUndefinedExpressionAllowed($node)) {
46+
return [];
47+
}
48+
4449
if (is_string($node->name)) {
4550
$variableNameScopes = [$node->name => $scope];
4651
} else {
@@ -78,7 +83,7 @@ private function processSingleVariable(Scope $scope, Variable $node, string $var
7883
}
7984
}
8085

81-
if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) {
86+
if ($scope->isInExpressionAssign($node)) {
8287
return [];
8388
}
8489

‎tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,4 +1169,13 @@ public function testBug8719(): void
11691169
$this->analyse([__DIR__ . '/data/bug-8719.php'], []);
11701170
}
11711171

1172+
public function testDynamicVariable(): void
1173+
{
1174+
$this->cliArgumentsVariablesRegistered = true;
1175+
$this->polluteScopeWithLoopInitialAssignments = true;
1176+
$this->checkMaybeUndefinedVariables = true;
1177+
$this->polluteScopeWithAlwaysIterableForeach = true;
1178+
1179+
$this->analyse([__DIR__ . '/data/dynamic-variable.php'], []);
1180+
}
11721181
}

‎tests/PHPStan/Rules/Variables/IssetRuleTest.php‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,4 @@ public function testIssetAfterRememberedConstructor(): void
484484
],
485485
]);
486486
}
487-
488-
public function testDynamicVariable(): void
489-
{
490-
$this->treatPhpDocTypesAsCertain = true;
491-
492-
$this->analyse([__DIR__ . '/data/dynamic-variable.php'], []);
493-
}
494487
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /