`assertArrayHasKey()` does not accept `ArrayAccess` implementations with a specific value type when test code is analysed with PHPStan at level 9 · Issue #6833 · sebastianbergmann/phpunit · GitHub
Skip to content

assertArrayHasKey() does not accept ArrayAccess implementations with a specific value type when test code is analysed with PHPStan at level 9 #6833

Closed

Description

@tacovandenbroek

Methods like assertArrayHasKey accept array<mixed>|ArrayAccess<array-key, mixed> as subject array. The TValue template in ArrayAccess however is invariant, which makes phpstan unhappy at level 9 when analyzing code that passes an ArrayAccess implementation containing anything but mixed.

Using call-site variance (ArrayAccess<array-key, covariant mixed>) these methods can accept ArrayAccess objects with any value type (as it should, since it only checks the keys).

Activity

  1. added
    feature/assertionIssues related to assertions and expectations
    type/bugSomething is broken
    and removed
    type/enhancementA new idea that should be implemented
    on Jul 16, 2026
  2. changed the title [-]Use call-site variance in assertion methods like assertArrayHasKey[/-] [+]`assertArrayHasKey()` does not accept `ArrayAccess` implementations with a specific value type when test code is analysed with PHPStan at level 9[/+] on Jul 16, 2026
  3. added a commit that references this issue on Jul 16, 2026
    2a6bab8
  4. added 2 commits that reference this issue on Jul 19, 2026
    dbff14b
    e31d43f
  5. locked as resolved and limited conversation to collaborators on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature/assertionIssues related to assertions and expectationstype/bugSomething is broken

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions