Skip to content

"Unreachable statement - code above always terminates." with switch/break/return statements #4076

@klausi

Description

@klausi

Bug report

When using switch cases with if, return and break statements then PHPStan gets confused.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

function test(int $x, int $y): int {
	switch($x) {
		case 0:
			return 0;
		case 1:
			if ($y == 2) {
				// continue after the switch().
				break;
			}
			// else fall through to default clause
		default:
			return 99;
	}
	
	return -1;
}

https://phpstan.org/r/06e8ebd7-6062-4b53-8ad5-fd71b242fd8b

Expected output

no output because no error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions