Skip to content

Union of optional keyed arrays doesn't mean both keys exists at same time #7143

Closed
phpstan/phpstan-src
#4399

Description

@rajyan

Bug report

I'm investigating
#4809
#6508
#7000
and splitting them into smaller related issues.

If I union two optional keyed constant arrays array{foo?: string, bar: string}|array{foo: string, bar?: string} PHPStan generalize it as array{foo?: string, bar?: string}&non-empty-array (which I think is a correct and smart behavior) but this make it not report an error on offset access on both optional keys.

Code snippet that reproduces the problem

https://phpstan.org/r/519053c6-5183-449b-91ae-b27661281395

Expected output

Report for maybe not existing offset access in line 8,9,17,18

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

Yes! It' definitely a great tool already, and want to help making it even better!

Activity

  1. phpstan-bot commented on Oct 13, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -No errors
    +16: Expected type array{foo?: string, bar?: string}&non-empty-array, actual: non-empty-array{foo?: string, bar?: string}
    Full report
    Line Error
    16 Expected type array{foo?: string, bar?: string}&non-empty-array, actual: non-empty-array{foo?: string, bar?: string}
  2. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#4399

  3. github-actions commented on Mar 30, 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.

  4. locked as resolved and limited conversation to collaborators on Mar 30, 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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions