Skip to content

Array shape is oversimplified when defined with a variable key #9907

Closed

Description

@VincentLanglet

Bug report

When an array key is 'foo'|'bar' the array shape is too much simplified

Code snippet that reproduces the problem

https://phpstan.org/r/42797189-cf51-44e5-a0f7-fef18ce9545a

Expected output

Best would be

array{id: null, bar: string}|array{id: null, foo: string}

but I would expect at least

array{id: null, bar?: string, foo?: string}

Is something something possible without impacting (too much ?) the performance ?

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

Sure, I'm adding phpdoc for arrays and it detects so much

Activity

  1. nusphere commented on Sep 25, 2023

    @nusphere

    I recognized a similar problem.

    https://phpstan.org/r/32f37b9f-fdd9-472b-a040-9218f14b62e5

    the optimal return value is:

    array{street?: array{change_to: string}, city?: array{change_to: string}, variation_count?: int<1, max>}
    

    or

    array<'city'|'street', array{change_to: string}>&array{variation_count?: int<1, max>}
    

    I know that this is basically an array combination. But unfortunately I can't find a "good" option. Unfortunately, the proposed solution for my example feels wrong, because variation_count will not contain any change information.

    array<'city'|'street'|'variation_count', array{change_to: string}|int<1, max>>.
    

    Unfortunately that feels wrong.

  2. nusphere commented on Sep 25, 2023

    @nusphere

    also i tried to use own phpstan-type to combine them as intersections, but this also fails

    https://phpstan.org/r/d0127c2e-8c01-4738-9f3f-54c29220349c

  3. phpstan-bot commented on Oct 1, 2023

    @phpstan-bot
    Contributor

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

    @@ @@
    -21: PHPDoc tag @return contains unresolvable type.
    +36: Method Demo::diffAddresses() should return array{city?: array{change_to: string}, street?: array{change_to: string}} but returns array<'city'|'street'|'variation_count', array{change_to: string}|int<1, max>>.
    Full report
    Line Error
    36 `Method Demo::diffAddresses() should return array{city?: array{change_to: string}, street?: array{change_to: string}} but returns array<'city'
  4. nusphere commented on Oct 2, 2023

    @nusphere

    The new error appears to resolve the phpstan type, unfortunately not yet as an intersection.

    In my case at least it doesn't work without a clean intersection. but I don't know whether phpstan can process this internally, since I think it works with type elimination.

    Maybe one of the developers can say something about it.

  5. added a commit that references this issue on Feb 16, 2026
  6. phpstan-bot commented on Mar 14, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5193

  7. github-actions commented on Apr 15, 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.

  8. locked as resolved and limited conversation to collaborators on Apr 15, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions