-
-
Notifications
You must be signed in to change notification settings - Fork 915
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 expression does not handle remaining value with an array value #4451
Comments
@jkuchar After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
PHP 8.0 (1 error)
==========
- 9: Match expression does not handle remaining value: array(bool, bool)
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
PHP 7.4 (4 errors)
========== Full reportPHP 8.0 (1 error)
PHP 7.4 (4 errors)
PHP 7.1 – 7.3 (5 errors)
|
@jkuchar After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
PHP 8.0 (1 error)
==========
- 9: Match expression does not handle remaining value: array(bool, bool)
+ 5: Method HelloWorld::sayHello() should return int but return statement is missing.
PHP 7.4 (4 errors)
========== Full reportPHP 8.0 (1 error)
PHP 7.4 (4 errors)
PHP 7.1 – 7.3 (5 errors)
|
same goes for string: $value *= match ($identifier) {
'g', 'm', 'k' => 1024,
}; |
@solverat Your issue is unrelated, and the reported error is legit. If someone passes 'a' into your method, the match expression will throw an exception. You can either handle all possible strings, or use advanced types in PHPDocs: https://phpstan.org/writing-php-code/phpdoc-types#literals-and-constants |
@ondrejmirtes damn you're right - I guess I misinterpreted the message then. Thanks for clarifying! |
@jkuchar After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
PHP 8.0 (1 error)
==========
- 9: Match expression does not handle remaining value: array(bool, bool)
+ 9: Match expression does not handle remaining value: array{bool, bool}
PHP 7.4 (4 errors)
========== Full reportPHP 8.0 (1 error)
PHP 7.4 (4 errors)
PHP 7.1 – 7.3 (5 errors)
|
For those who come from google, you probably just want to add a |
AFAIK, the |
exactly currently we have to do this
to please phpstan while keeping the default (no pun intended) behaviour |
same for spaceship operator
|
@mostafa-rz After the latest commit in 1.7.x, PHPStan now reports different result with your code snippet: @@ @@
-PHP 8.1 (1 error)
+PHP 8.1
==========
-16: Match expression does not handle remaining value: int<-1, 1>
+No errors
PHP 7.1 – 8.0 (2 errors)
========== Full reportPHP 8.1No errors PHP 7.1 – 8.0 (2 errors)
|
I agree with #4451 (comment) that it should be possible to use the default "\UnhandeledMatchError" without explicitly adding a default case. |
@SomeBdyElse There's a feature request about this: #8042 (it has a hint on how to implement it) |
What about this $propertyValue = match ($property) {
ReceiveOperation::OPERATION_ID => $value->getOperationId(),
ReceiveOperation::OPERATION_CODE => $value->getOperationCode(),
ReceiveOperation::DATA => $value->getData(),
'default' => throw new \LogicException('Unknown property ' . var_export($property, true)),
};
Is it intended?
|
@jaroslavtyc Please reproduce your problem on phpstan.org/try and open a separate issue. |
|
@mostafa-rz After the latest push in 1.11.x, PHPStan now reports different result with your code snippet: @@ @@
-PHP 8.1 (1 error)
+PHP 8.1 (3 errors)
==========
-16: Match expression does not handle remaining value: int<-1, 1>
+ 4: Enum case TrendEnum::UP without type doesn't match the "string" type.
+ 5: Enum case TrendEnum::NEUTRAL without type doesn't match the "string" type.
+ 6: Enum case TrendEnum::DOWN without type doesn't match the "string" type.
PHP 7.1 – 8.0 (2 errors)
========== Full reportPHP 8.1 (3 errors)
PHP 7.1 – 8.0 (2 errors)
|
@mostafa-rz After the latest push in 1.11.x, PHPStan now reports different result with your code snippet: @@ @@
-PHP 8.1 (1 error)
+PHP 8.1 (3 errors)
==========
-16: Match expression does not handle remaining value: int<-1, 1>
+ 4: Enum case TrendEnum::UP does not have a value but the enum is backed with the "string" type.
+ 5: Enum case TrendEnum::NEUTRAL does not have a value but the enum is backed with the "string" type.
+ 6: Enum case TrendEnum::DOWN does not have a value but the enum is backed with the "string" type.
PHP 7.1 – 8.0 (2 errors)
========== Full reportPHP 8.1 (3 errors)
PHP 7.1 – 8.0 (2 errors)
|
Note that this is now fixed for the spaceship operator with a correct syntax: https://phpstan.org/r/200e398c-d2a5-4370-aaf9-2cfa6864a409 <?php declare(strict_types = 1);
enum TrendEnum
{
case UP;
case NEUTRAL;
case DOWN;
}
class HelloWorld
{
public function sayHello(): mixed
{
$foo = fn(): int => rand();
$bar = fn(): int => rand();
return match ($foo <=> $bar) {
1 => TrendEnum::UP,
0 => TrendEnum::NEUTRAL,
-1 => TrendEnum::DOWN,
};
}
} Commenting one match-arm also lead to the expected result. I think this can be closed. But the array of bool still is not handled. <?php declare(strict_types = 1);
class HelloWorld
{
public function sayHello(): int
{
$verified = fn(): bool => rand() === 1;
return match([$verified(), $verified()]) {
[true, true] => 1,
[true, false] => 2,
[false, true] => 3,
[false, false] => 4,
};
}
} |
Is this related: https://phpstan.org/r/0c53d9eb-f719-480a-9fa6-6d8fbaf3546e ? |
@bwaidelich No, your code would be correct only if those classes were final. |
Oh, of course.. https://phpstan.org/r/10db3101-89fa-44dd-92c1-682fe650f865 |
This was possible to solve thanks to: phpstan/phpstan-src@7912caf I realized that After that I took advantage of the new method when removing types: phpstan/phpstan-src@b5cf52b And finally I made it work inside |
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
match()
definitely handles all values, but phpstan do not think so.Code snippet that reproduces the problem
https://phpstan.org/r/b07c57ab-5084-4149-9c8b-21c1e14d2377
Expected output
No error.
The text was updated successfully, but these errors were encountered: