You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem:
Using a match expression, covering all possible options leads to an error, when it is used inside a foreach loop.
Running the same match expression on a single item, the error is gone.
Interstingly the error is gone, enabling "treatPhpDocTypesAsCertain"
Interstingly the error is gone, enabling "treatPhpDocTypesAsCertain"
That should help you to understand the reason.
If you don't trust your phpdoc, you cannot be sure the element are really instanceof Item. Then you cannot be sure ->status is really A, B, C or null.
I could put some
class Item2 {
public function __construct(
public Status|int|null $status
) {}
}
That should help you to understand the reason.
I understand the reason.
And i understand that instance of check is fixing it
But i didnt read that the behaviour of treatPhpDocTypesAsCertain changed with your bugfix.
In addition the behaviour shouldnt be intended like this, since its rather inconsistent.
Shouldnt be the behaviour consistent along all phpstan checks?
If the type is not inferred from the docs, fine - but then i dont understand why it is expected, that the UnhandleMatchError occurs, but it is not expected that Cannot access property x on mixed. occurs.
Considering the change in a patch version within a bugfix its still a bug from my point of view, especially due to inconsistency with other checks
Feels like a bug. I don't get why we don't get "Match expression does not handle remaining..." error in either case, but we only get uncaught exception when the match is in a foreach. Really weird.
Feels like a bug. I don't get why we don't get "Match expression does not handle remaining..." error in either case, but we only get uncaught exception when the match is in a foreach. Really weird.
Since the threatPhpDocAsCertain config is expected to be here to avoid false-positive error reported by PHPStan, I think we should add consistency by not making the throw point relying on the threatPhpDocAsCertain option.
Activity
Fix phpstan/phpstan#14396: False UnhandledMatchError in foreach with …
VincentLanglet commentedon Mar 29, 2026
That should help you to understand the reason.
If you don't trust your phpdoc, you cannot be sure the element are really
instanceof Item. Then you cannot be sure->statusis really A, B, C or null.I could put some
in the array and pass them to this method.
You solve the issue with an instanceof check:
https://phpstan.org/r/09768752-f3c5-4b49-aa12-31e2fc635c7a
florianhofsaessC24 commentedon Mar 29, 2026
But i didnt read that the behaviour of treatPhpDocTypesAsCertain changed with your bugfix.
In addition the behaviour shouldnt be intended like this, since its rather inconsistent.
Shouldnt be the behaviour consistent along all phpstan checks?
Especially because:
If the type is not inferred from the docs, fine - but then i dont understand why it is expected, that the UnhandleMatchError occurs, but it is not expected that
Cannot access property x on mixed.occurs.Considering the change in a patch version within a bugfix its still a bug from my point of view, especially due to inconsistency with other checks
ondrejmirtes commentedon Mar 29, 2026
Feels like a bug. I don't get why we don't get "Match expression does not handle remaining..." error in either case, but we only get uncaught exception when the match is in a foreach. Really weird.
VincentLanglet commentedon Mar 29, 2026
The MatchExpressionRule only use
getTypehttps://github.com/phpstan/phpstan-src/blob/dc813fe0538dd7edad72449f86f2ca35c769adbb/src/Rules/Comparison/MatchExpressionRule.php#L145so maybe getNativeType should be used here.
On the other side the MatchHandler rely on the threatPhpDocAsCertain https://github.com/phpstan/phpstan-src/blob/dc813fe0538dd7edad72449f86f2ca35c769adbb/src/Analyser/ExprHandler/MatchHandler.php#L403
Since the threatPhpDocAsCertain config is expected to be here to avoid false-positive error reported by PHPStan, I think we should add consistency by not making the throw point relying on the threatPhpDocAsCertain option.
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
17 remaining items
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
Add regression test for #14396
github-actions commentedon Oct 4, 2026
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.