Skip to content

switch (true) whose cases cover every value reports return.missing, unlike the equivalent if/elseif #15892

Backlog
phpstan/phpstan-src
#7264

Description

@ondrejmirtes

Bug report

A switch (true) whose cases cover every possibility, with a return in each case, still reports a missing return. PHPStan itself knows the last case is always true. The equivalent if/elseif chain has no return.missing error.

function signSwitch(int $n): string
{
	switch (true) {
		case $n < 0:
			return 'negative';
		case $n >= 0:
			return 'non-negative';
	}
}

function signIf(int $n): string
{
	if ($n < 0) {
		return 'negative';
	} elseif ($n >= 0) {
		return 'non-negative';
	}
}

Actual output (line numbers refer to the playground snippet):

5: Function signSwitch() should return string but return statement is missing.
8: Comparison operation ">=" between int<0, max> and 0 is always true.
17: Comparison operation ">=" between int<0, max> and 0 is always true.

Code snippet that reproduces the problem

https://phpstan.org/r/b8813392-3f81-46f0-ab98-8215a774bae4

Expected output

No return.missing on line 5, the same as for signIf(). After a case whose condition true === <expr> is always true and which always terminates, the code after the switch is unreachable. This is exactly how an if/elseif chain with an always-true last condition is already handled. Whether the always-true comparisons themselves are reported is a separate question and is fine either way.

Activity

  1. ondrejmirtes commented on Nov 30, 2026

    @ondrejmirtes
    MemberAuthor

    This needs to:

    1. Report unreachable default case if all cases above terminate (are exhaustive).
    2. Report unreachable code below exhaustive switch statement.
    3. Remove the wrong return.missing if the switch statement is exhaustive.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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