Skip to content

Incorrect unreachable statement error when breaking do-while() in a switch() #2913

@Muqsit

Description

@Muqsit

Bug report

phpstan doesn't account for break 2 in a do-while loop when reporting unreachable statement errors.

Code snippet that reproduces the problem

https://phpstan.org/r/74a76359-70bf-44b7-88a1-e20fb6313b34

<?php

declare(strict_types=1);

function generateRandomTierForWeapon() : int{
	do{
		$tier = mt_rand(1, 6); // generate random tier
		switch($tier){
			case 6:
				// tier 6 too high for weapons, try generating
				// another random tier value.
				break;
			default:
				break 2;
		}
	}while(true);
	return $tier;
}

echo "Generated tier " . generateRandomTierForWeapon() . PHP_EOL;

Expected output

Expected no errors to output as the do-while loop can be broken when the condition $tier !== 6 is satisfied.

Actual output

+--------------------------------------------------------------+
| Line | test.php                                              |
+--------------------------------------------------------------+
| 17   | Unreachable statement - code above always terminates. |
+--------------------------------------------------------------+

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions