1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #5578 - variables always set in conditionals should be combined

This commit is contained in:
Matt Brown 2021-04-06 11:11:40 -04:00
parent 6851b5c1f1
commit 7080bc3bff
2 changed files with 20 additions and 1 deletions

View File

@ -368,7 +368,11 @@ class OrAnalyzer
$codebase
);
} elseif (isset($left_context->vars_in_scope[$var_id])) {
$if_context->vars_in_scope[$var_id] = $left_context->vars_in_scope[$var_id];
$if_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
$type,
$left_context->vars_in_scope[$var_id],
$codebase
);
}
}

View File

@ -471,6 +471,21 @@ class AssignmentInConditionalTest extends \Psalm\Tests\TestCase
}',
'error_message' => 'InvalidReturnStatement',
],
'assignmentInBranchOfOr' => [
'<?php
function getPath(): string|object {
return rand(0, 1) ? "a" : new stdClass();
}
function foo(string $s) : string {
if (($path = $s) || ($path = getPath())) {
return $path;
}
return "b";
}',
'error_message' => 'InvalidReturnStatement',
],
];
}
}