Skip to content
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

Closed
mewejo opened this issue Jan 17, 2022 · 7 comments
Closed

Match reporting unhandled enum #6394

mewejo opened this issue Jan 17, 2022 · 7 comments
Labels

Comments

@mewejo
Copy link

mewejo commented Jan 17, 2022

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 throwing Match 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

<?php declare(strict_types = 1);

enum EntryType: string
{
	case CREDIT = 'credit';
	case DEBIT = 'debit';
}

function getType(int $x): EntryType
{
	return $x > 0 ? EntryType::CREDIT : EntryType::DEBIT;
}

function getAmount(): int
{
	return match(getType(rand(0, 1))) {
		EntryType::DEBIT => 100,
		EntryType::CREDIT => -100
	};
}

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 :-)

@ondrejmirtes
Copy link
Member

Hi, thank you for your kind words! This is just a name clash, gettype function is already in PHP and it returns string. You need to rename that.

@mewejo
Copy link
Author

mewejo commented Jan 17, 2022

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.

@ondrejmirtes
Copy link
Member

Please post the real-world code here and put a \PHPStan\dumpType($x) before the match where $x is the thing in match($x) :) And see what the output is.

The playground not working is weird, I'm gonna look into it.

@mewejo
Copy link
Author

mewejo commented Jan 17, 2022

Thanks for looking! :-)

 ------ ------------------------------------------------------------------------------------ 
  Line   Class.php                                                                    
 ------ ------------------------------------------------------------------------------------ 
  153    Dumped type: EntryType                                       
  155    Match expression does not handle remaining value: EntryType  
 ------ ------------------------------------------------------------------------------------

I just stumbled across a workaround though, if I assign the result of the getType() call to a local variable, PHPStan is happy. If I use that call result direct in a match(), it complains.

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: Dumped type: Grizzlyware\Cub\Ledger\EntryType

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
	};
}

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@ab0245c

@mewejo
Copy link
Author

mewejo commented Jan 18, 2022

Thank you @ondrejmirtes 💯

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants