Skip to content

while-true-if-break: Variable might not be defined #11545

@donquixote

Description

@donquixote

Bug report

If a loop has no exit condition in the regular place, but a custom exit condition with if()..break, then phpstan fails to see a variable as defined that is defined inside the exit condition conditional block.

Similar to:

This problem might have the same technical cause, but it is a different test case, and hence it deserves its own issue - I think.

Code snippet that reproduces the problem

https://phpstan.org/r/1b3c4c9e-643a-4a50-90a9-875ad27af465

function foo(int $max): void {
	$i = 0;
	while (true) {
		if ($i > $max) {
			$result = 'done';
			break;
		}
		++$i;
	}
	print $result;  // Variable $result might not be defined.
}

Expected output

No inspection warning.

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions