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:
parent
dc15afe781
commit
86a89b2d27
@ -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];
|
||||
}
|
||||
}
|
||||
|
@ -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];
|
||||
}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user