Skip to content

BenevolentUnionType with generics produce confusing errors #9732

Closed

Description

@dktapps

Bug report

When passing $GLOBALS to a function which accepts array<K of string, V>, K is not able to be inferred for some reason.

This started getting reported around 1.10.17 or so.

Code snippet that reproduces the problem

https://phpstan.org/r/63496f72-9ba6-4b08-b393-1442222c6e39

Expected output

The error on line 23 should not be reported, as the type of $GLOBALS is array<string, mixed>.

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 Aug 7, 2023
  2. dktapps commented on Sep 21, 2023

    @dktapps
    ContributorAuthor

    Seems like this is something to do with the BenevolentUnionType used for the keys of globals. The same also happens with $_SERVER and other superglobals.

    Again, I'd be happy to work on a fix if I can get a pointer where to start looking for the problem.

  3. ondrejmirtes commented on Sep 21, 2023

    @ondrejmirtes
    Member

    I'd look what happens in ArrayType::inferTemplateTypes() first.

  4. dktapps commented on Sep 21, 2023

    @dktapps
    ContributorAuthor

    Thanks, I'll take a look.

  5. dktapps commented on Sep 21, 2023

    @dktapps
    ContributorAuthor

    Looks like the problem is actually the type itself, rather than the superglobals. I assumed that $GLOBALS would be considered to have type array<string, mixed>. When using an array with array-key for the key, the problem is the same.

    It appears that the real issue here is BenevolentUnionType is suppressing the errors that would make this problem understandable.
    When using a non-benevolent type like array<int|string, mixed> it reports two errors, the second of which makes it very clear where the problem is coming from (see line 34 errors):
    https://phpstan.org/r/a24f065e-16d5-46a4-bc4f-11db8605284c

  6. changed the title [-]Generic key inference from $GLOBALS fails[/-] [+]BenevolentUnionType with generics produce confusing errors[/+] on Sep 21, 2023
  7. dktapps commented on Sep 21, 2023

    @dktapps
    ContributorAuthor
  8. phpstan-bot commented on Sep 26, 2025

    @phpstan-bot
    Contributor

    @dktapps After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
     16: No error to ignore is reported on line 16.
    -23: Unable to resolve the template type TKeyType in call to method static method HelloWorld::stringifyKeys()
    +23: Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
    Full report
    Line Error
    16 No error to ignore is reported on line 16.
    23 Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
  9. phpstan-bot commented on Sep 26, 2025

    @phpstan-bot
    Contributor

    @dktapps After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -26: Unable to resolve the template type TKeyType in call to method static method HelloWorld::stringifyKeys()
    +26: Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
     34: Parameter #1 $array of static method HelloWorld::stringifyKeys() expects array<string, mixed>, array<int|string, mixed> given.
    -34: Unable to resolve the template type TKeyType in call to method static method HelloWorld::stringifyKeys()
    +34: Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
    Full report
    Line Error
    26 Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
    34 `Parameter #1 $array of static method HelloWorld::stringifyKeys() expects array<string, mixed>, array<int
    34 Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
  10. added a commit that references this issue on Mar 2, 2026
    0a4583d
  11. phpstan-bot commented on Mar 2, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5070

  12. added 2 commits that reference this issue on Mar 2, 2026
    0a4583d
    dcfce10
  13. github-actions commented on Apr 3, 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.

  14. locked as resolved and limited conversation to collaborators on Apr 3, 2026
  15. added a commit that references this issue on Apr 7, 2026
    f7c2d72
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