Skip to content

Commit

Permalink
Fix/issue 3601
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Apr 24, 2022
1 parent 0127e50 commit 1ffed9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -2383,6 +2383,8 @@ static function (?Type $offsetType, Type $valueType) use (&$arrayType): void {
$hasYield = $hasYield || $result->hasYield();
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
$nonNullabilityResults[] = $nonNullabilityResult;
}
foreach (array_reverse($expr->vars) as $var) {
$scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var);
}
foreach (array_reverse($nonNullabilityResults) as $nonNullabilityResult) {
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Expand Up @@ -862,4 +862,13 @@ public function testBug6112(): void
$this->analyse([__DIR__ . '/data/bug-6112.php'], []);
}

public function testBug3601(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-3601.php'], []);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-3601.php
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Bug3601;

if (rand(0, 1)) {
$a = 'b';
}

if (rand(0, 1)) {
$c = ['b' => 'everything is fine'];
}

if (isset($a, $c, $c[$a])) {
echo $c[$a];
}

0 comments on commit 1ffed9b

Please sign in to comment.