Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Playground link: https://phpstan.org/r/923770c1-8beb-48a8-8929-c98c43d76697

I use defensive programming, by adding a default case on my switch even tough they are covering all the cases:

enum A: string {
	case A = 'A';
}

$a = A::A;
switch ($a) {
	case A::A: break;
	default:
          throw new \Exception('Unhanded case `' . $a->name . '`');
}

but PHPStan reports:

Binary operation "." between 'Unhanded case `' and mixed results in an error.

because it sees the type as *NEVER*.

The issue is that if a case is not handled PHPStan reports nothing (by default?).

I would like to be able to either:

  1. Asks PHPStan to guarantee the switch is covering
  2. Let PHPStan fallback to a more general type (maybe that is not a good approach as this would mean that some dead if ($a === A::A) would not be reported)

Note that I'm not reporting this as an issue as I do not think that there is any concrete issue here.

You must be logged in to vote

Replies: 2 comments · 2 replies

Comment options

It seems to me that this can be solved in this way: https://phpstan.org/r/3b52ffc8-8a13-4997-b43e-02267e0e01fc

You must be logged in to vote
1 reply
@homersimpsons
Comment options

Oh, that's interesting, PHPStan is not okay to call ->name on *NEVER* but is okay to pass *NEVER* to a BackedEnum.

Answer selected by homersimpsons
Comment options

if you want to enforce errors on missing cases, I would recommend using match over switch, because it will automatically error in case of unhandled expressions

You must be logged in to vote
1 reply
@homersimpsons
Comment options

I considered using a match but it does not really plays nicely in my case in terms of maintainability:

  1. I would have to extract the cases to functions, which would add an indirection and required to pass the function variables for each cases
  2. It would be harder to maintain consistency between the cases handling as their backing functions would be split over the class / codebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.