Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/issue 3601 #1240

Merged
merged 2 commits into from Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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];
}