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

Fix #3034 - expand out while expressions for more accurate variable initialisation

This commit is contained in:
Matthew Brown 2020-03-28 18:22:00 -04:00
parent dc15afe781
commit 86a89b2d27
2 changed files with 27 additions and 1 deletions

View File

@ -54,7 +54,7 @@ class WhileAnalyzer
if (LoopAnalyzer::analyze(
$statements_analyzer,
$stmt->stmts,
[$stmt->cond],
self::getAndExpressions($stmt->cond),
[],
$loop_scope,
$inner_loop_context
@ -178,4 +178,20 @@ class WhileAnalyzer
return null;
}
/**
* @return list<PhpParser\Node\Expr>
*/
private static function getAndExpressions(
PhpParser\Node\Expr $expr
) : array {
if ($expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) {
return array_merge(
self::getAndExpressions($expr->left),
self::getAndExpressions($expr->right)
);
}
return [$expr];
}
}

View File

@ -341,6 +341,16 @@ class WhileTest extends \Psalm\Tests\TestCase
$position++;
}'
],
'variableDefinedInWhileConditional' => [
'<?php
function foo() : void {
$pointers = ["hi"];
while (rand(0, 1) && 0 < ($parent = 0)) {
print $pointers[$parent];
}
}'
],
];
}