-
-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Match reporting unhandled enum #6394
Comments
Hi, thank you for your kind words! This is just a name clash, |
Hey @ondrejmirtes - Yeah.. bad example.. whoops. In my real code it's a class method. I can't change the example on the playground at the moment though as it's coming back with a server error. |
Please post the real-world code here and put a The playground not working is weird, I'm gonna look into it. |
Thanks for looking! :-)
I just stumbled across a workaround though, if I assign the result of the Original code: enum EntryType: string
{
case CREDIT = 'credit';
case DEBIT = 'debit';
}
public function getType(): EntryType
{
return $this->type;
}
public function getAmount(): Money
{
return match($this->getType()) {
EntryType::DEBIT => Money:of(...),
EntryType::CREDIT => Money:of(...)
};
} Workaround: public function getAmount(): Money
{
$entryType = $this->getType();
return match($entryType) {
EntryType::DEBIT => Money:of(...),
EntryType::CREDIT => Money:of(...)
};
} Dumping the type with a variable and directly from the method both come back correct: What's more weird is the playground breaks if I run this code through it, which should replicate the issue (it does locally). <?php declare(strict_types = 1);
enum EntryType: string
{
case CREDIT = 'credit';
case DEBIT = 'debit';
}
function getTheType(int $x): EntryType
{
return $x > 0 ? EntryType::CREDIT : EntryType::DEBIT;
}
function getAmount(): int
{
return match(getTheType(rand(0, 1))) {
EntryType::DEBIT => 100,
EntryType::CREDIT => -100
};
} |
Fixed: phpstan/phpstan-src@ab0245c |
Thank you @ondrejmirtes 💯 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
I have an enum, and I'm using it in a
match()
and providing options for all of the enum's cases. PHPStan is throwingMatch expression does not handle remaining value: EntryType
In my example it's instead throwing
Match expression does not handle remaining value: string
which is wrong too.The same result happens with backed and unbacked enums.
Code snippet that reproduces the problem
https://phpstan.org/r/dc525a54-542e-40a9-914f-986088e5c791
Expected output
All cases are matched, I don't expect an error.
Did PHPStan help you today? Did it make you happy in any way?
PHPStan does make me happy. Appreciate all the work you do! Thank you :-)
The text was updated successfully, but these errors were encountered: