Skip to content

UnhandledMatchError since 2.1.41 #14396

Closed
phpstan/phpstan-src
#5857

Description

@florianhofsaessC24

Bug report

Affected Version:

= 2.1.41

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"

Code snippet that reproduces the problem

https://phpstan.org/r/f6ae2fe6-cd75-4191-a7c8-96bfa4714b35

Expected output

I would expect no error to happen.

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. added this to the Easy fixes milestone on Mar 29, 2026
  2. added a commit that references this issue on Mar 29, 2026
    d925bdd
  3. VincentLanglet commented on Mar 29, 2026

    @VincentLanglet
    Contributor

    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
        ) {}
    }
    

    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

  4. florianhofsaessC24 commented on Mar 29, 2026

    @florianhofsaessC24
    Author

    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?

    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

  5. ondrejmirtes commented on Mar 29, 2026

    @ondrejmirtes
    Member

    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.

  6. VincentLanglet commented on Mar 29, 2026

    @VincentLanglet
    Contributor

    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.

    The MatchExpressionRule only use getType https://github.com/phpstan/phpstan-src/blob/dc813fe0538dd7edad72449f86f2ca35c769adbb/src/Rules/Comparison/MatchExpressionRule.php#L145
    so 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

    • to report an always true condition
    • to know if the checks are exhaustive

    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.

  7. added a commit that references this issue on Jun 20, 2026
  8. added a commit that references this issue on Jul 16, 2026
  9. 17 remaining items

  10. github-actions commented on Oct 4, 2026

    @github-actions

    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.

  11. locked as resolved and limited conversation to collaborators on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions