Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/Rules/Variables/DefinedVariableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$errors = [];

if ($scope->isUndefinedExpressionAllowed($node)) {
return [];
}

if (is_string($node->name)) {
$variableNameScopes = [$node->name => $scope];
} else {
Expand Down Expand Up @@ -78,7 +83,7 @@ private function processSingleVariable(Scope $scope, Variable $node, string $var
}
}

if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) {
if ($scope->isInExpressionAssign($node)) {
return [];
}

Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1169,4 +1169,23 @@ public function testBug8719(): void
$this->analyse([__DIR__ . '/data/bug-8719.php'], []);
}

public function testBug13353(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-13353.php'], [
[
'Variable $bar might not be defined.',
11,
],
[
'Undefined variable: $bar',
20,
],
]);
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,11 @@ public function testIssetAfterRememberedConstructor(): void
]);
}

public function testBug13353(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-13353.php'], []);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13353.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Bug13353;

$foo = 'bar';

if (!isset($$foo)) {
echo 'Wololo';
}

echo $$foo;

function () {
$foo = 'bar';

if (!isset($$foo)) {
echo 'Wololo';
}

echo $$foo;
};
Loading