Skip to content

Commit

Permalink
Fix #5001 - update variables after context after type change from empty
Browse files Browse the repository at this point in the history
Empty is the invalid state, and some old logic here was causing a bug
  • Loading branch information
muglug committed Jan 13, 2021
1 parent a53cc23 commit d10a068
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,7 @@ public function getRedefinedVars(array $new_vars_in_scope, bool $include_new_var

$new_type = $new_vars_in_scope[$var_id];

if (!$this_type->isEmpty()
&& !$new_type->isEmpty()
&& !$this_type->equals($new_type)
) {
if (!$this_type->equals($new_type)) {
$redefined_vars[$var_id] = $this_type;
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Loop/WhileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,44 @@ public function alter() : void {
}
}'
],
'ifNestedInsideLoop' => [
'<?php
function analyse(): int {
$state = 1;
while (rand(0, 1)) {
if ($state === 3) {
echo "here";
} elseif ($state === 2) {
if (rand(0, 1)) {
$state = 3;
}
} else {
$state = 2;
}
}
return $state;
}'
],
'ifNotNestedInsideLoop' => [
'<?php
function analyse(): int {
$state = 1;
while (rand(0, 1)) {
if ($state === 3) {
echo "here";
} elseif ($state === 2) {
$state = 3;
} else {
$state = 2;
}
}
return $state;
}'
],
];
}

Expand Down

0 comments on commit d10a068

Please sign in to comment.