Skip to content

Array object nested types are not properly identified #7976

Open

Description

@razvan-tache

Bug report

Hi, I have an issue with with a build that was passing on 1.8.2. I get:

should return ArrayObject<int|string, array<string,  mixed>> but returns ArrayObject<int|string, array>. 

Did some default option changed? To me array<string, mixed> says more than just array.

My config:

level: 8
    paths:
        - src
    checkGenericClassInNonGenericObjectType: false
    reportUnmatchedIgnoredErrors: false
    phpVersion: 70400

Code snippet that reproduces the problem

https://phpstan.org/r/84c094da-91ef-47b2-9161-b897debc6789

Expected output

No errors

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

Activity

  1. razvan-tache commented on Sep 9, 2022

    @razvan-tache
    Author

    Original discussion: #7892 (comment)

    Relevant comments:

    I managed by changing the typehint of the init data, to be the type in the ArrayObject typehint, to receive an error:

    It basically tells me that should return A but returns A.

    Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>

    I managed by changing the typehint of the init data, to be the type in the ArrayObject typehint, to receive an error:

    It basically tells me that should return A but returns A.

    Method HelloWorld::sayHello() should return ArrayObject<int\|string, non-empty-array<string, mixed>> but returns ArrayObject<int\|string, non-empty-array<string, mixed>>

    https://phpstan.org/r/4062d367-a8eb-4154-a57a-cdbd33f02bbd

    From @ondrejmirtes :

    This is the reason why the type isn't accepted: https://phpstan.org/r/bbccbef1-6759-437d-9081-3c309efcd700

  2. added this to the Generics milestone on Sep 21, 2022
  3. phpstan-bot commented on Sep 21, 2022

    @phpstan-bot
    Contributor

    @razvan-tache After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -56: Dumped type: ArrayObject<int|string, non-empty-array<string, mixed>&hasOffsetValue('id', 1|2|3)&hasOffsetValue('name', 'name_1'|'name_2'|'name_3')&hasOffsetValue('value', 1.2|1.4|null)>
    +56: Dumped type: ArrayObject<int|string, non-empty-array<string, mixed>&hasOffsetValue('id', int)&hasOffsetValue('name', string)&hasOffsetValue('value', float|null)>
     57: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>.
    Full report
    Line Error
    56 `Dumped type: ArrayObject<int
    57 `Method HelloWorld::sayHello() should return ArrayObject<int
  4. razvan-tache commented on Sep 26, 2022

    @razvan-tache
    Author

    Cool, thanks, we'll check it out, as soon as we have the time!

  5. razvan-tache commented on Sep 26, 2022

    @razvan-tache
    Author

  6. phpstan-bot commented on Sep 16, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
      7: Class Arr extends generic class ArrayObject but does not specify its types: TKey, TValue
    -50: Method HelloWorld::sayHello() should return ArrayObject<int|string, array<string, mixed>> but returns ArrayObject<int|string, non-empty-array>.
    +50: Method HelloWorld::sayHello() should return ArrayObject<int|string, array<string, mixed>> but returns ArrayObject<1|2|3|string, non-empty-array>.
    Full report
    Line Error
    7 Class Arr extends generic class ArrayObject but does not specify its types: TKey, TValue
    50 `Method HelloWorld::sayHello() should return ArrayObject<int
  7. phpstan-bot commented on Sep 16, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -55: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>.
    +55: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<1|2|3|string, non-empty-array<string, mixed>>.
    Full report
    Line Error
    55 `Method HelloWorld::sayHello() should return ArrayObject<int
  8. phpstan-bot commented on Sep 16, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -56: Dumped type: ArrayObject<int|string, non-empty-array<string, mixed>&hasOffsetValue('id', 1|2|3)&hasOffsetValue('name', 'name_1'|'name_2'|'name_3')&hasOffsetValue('value', 1.2|1.4|null)>
    -57: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>.
    +56: Dumped type: ArrayObject<1|2|3|string, non-empty-array<string, mixed>&hasOffsetValue('id', 1|2|3)&hasOffsetValue('name', 'name_1'|'name_2'|'name_3')&hasOffsetValue('value', 1.2|1.4|null)>
    +57: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<1|2|3|string, non-empty-array<string, mixed>>.
    Full report
    Line Error
    56 `Dumped type: ArrayObject<1
    57 `Method HelloWorld::sayHello() should return ArrayObject<int
  9. phpstan-bot commented on Jan 31, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
      7: Class Arr extends generic class ArrayObject but does not specify its types: TKey, TValue
    -50: Method HelloWorld::sayHello() should return ArrayObject<int|string, array<string, mixed>> but returns ArrayObject<int|string, non-empty-array>.
    +50: Method HelloWorld::sayHello() should return ArrayObject<int|string, array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<mixed>>.
    Full report
    Line Error
    7 Class Arr extends generic class ArrayObject but does not specify its types: TKey, TValue
    50 `Method HelloWorld::sayHello() should return ArrayObject<int
  10. phpstan-bot commented on Mar 20, 2026

    @phpstan-bot
    Contributor

    @razvan-tache After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -56: Dumped type: ArrayObject<int|string, non-empty-array<string, mixed>&hasOffsetValue('id', 1|2|3)&hasOffsetValue('name', 'name_1'|'name_2'|'name_3')&hasOffsetValue('value', 1.2|1.4|null)>
    +56: Dumped type: ArrayObject<int|string, non-empty-array<string, mixed>&hasOffsetValue('name', 'name_1'|'name_2'|'name_3')&hasOffsetValue('value', 1.2|1.4|null)>
     57: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>.
    Full report
    Line Error
    56 `Dumped type: ArrayObject<int
    57 `Method HelloWorld::sayHello() should return ArrayObject<int
  11. phpstan-bot commented on Apr 23, 2026

    @phpstan-bot
    Contributor

    @razvan-tache After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
      7: Class Arr extends generic class ArrayObject but does not specify its types: TKey, TValue
    -50: Method HelloWorld::sayHello() should return ArrayObject<int|string, array<string, mixed>> but returns ArrayObject<int|string, non-empty-array>.
    +50: Method HelloWorld::sayHello() should return ArrayObject<int|string, array<string, mixed>> but returns ArrayObject<int|string, array<mixed>>.
    Full report
    Line Error
    7 Class Arr extends generic class ArrayObject but does not specify its types: TKey, TValue
    50 `Method HelloWorld::sayHello() should return ArrayObject<int
  12. phpstan-bot commented on Apr 23, 2026

    @phpstan-bot
    Contributor

    @razvan-tache After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -55: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>.
    +55: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, array<string, mixed>>.
    Full report
    Line Error
    55 `Method HelloWorld::sayHello() should return ArrayObject<int
  13. phpstan-bot commented on Apr 23, 2026

    @phpstan-bot
    Contributor

    @razvan-tache After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -56: Dumped type: ArrayObject<int|string, non-empty-array<string, mixed>&hasOffsetValue('id', 1|2|3)&hasOffsetValue('name', 'name_1'|'name_2'|'name_3')&hasOffsetValue('value', 1.2|1.4|null)>
    -57: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, non-empty-array<string, mixed>>.
    +56: Dumped type: ArrayObject<int|string, array<string, mixed>>
    +57: Method HelloWorld::sayHello() should return ArrayObject<int|string, non-empty-array<string, mixed>> but returns ArrayObject<int|string, array<string, mixed>>.
    Full report
    Line Error
    56 `Dumped type: ArrayObject<int
    57 `Method HelloWorld::sayHello() should return ArrayObject<int
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