Skip to content

Explicit mixed array becomes implicit in if statement #10559

Closed
phpstan/phpstan-src
#5050

Description

@uh-zuh

Bug report

Array with explicit type:

/** @var mixed[] $arr */

becomes implicit mixed[] after if statement:

if ($arr['key'] > 1) {}

and if there is parameter checkImplicitMixed: true than it reports error:

Cannot cast mixed to string.

Note: There is no way to fully reproduce the issue in the playground because there is no possibility to set parameter checkImplicitMixed: true in it.

Code snippet that reproduces the problem

https://phpstan.org/r/6f6e5ab8-433a-464e-a37e-8ae479c04369

Expected output

No errors

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

It is awesome!

Activity

  1. ondrejmirtes commented on May 30, 2024

    @ondrejmirtes
    Member
  2. added this to the Easy fixes milestone on May 30, 2024
  3. phpstan-bot commented on Oct 12, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -13: Dumped type: array
    +13: Dumped type: array<mixed>
     14: Dumped type: mixed
     26: Dumped type: array&hasOffsetValue('a1', mixed)
     27: Dumped type: mixed
    Full report
    Line Error
    13 Dumped type: array<mixed>
    14 Dumped type: mixed
    26 Dumped type: array&hasOffsetValue('a1', mixed)
    27 Dumped type: mixed
  4. phpstan-bot commented on Oct 12, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -13: Dumped type: array
    +13: Dumped type: array<mixed>
     14: Dumped type: mixed
     16: Cannot cast mixed to string.
     26: Dumped type: array&hasOffsetValue('a1', mixed)
     27: Dumped type: mixed
    Full report
    Line Error
    13 Dumped type: array<mixed>
    14 Dumped type: mixed
    16 Cannot cast mixed to string.
    26 Dumped type: array&hasOffsetValue('a1', mixed)
    27 Dumped type: mixed
  5. phpstan-bot commented on Oct 13, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -13: Dumped type: array
    +13: Dumped type: array<mixed>
     14: Dumped type: mixed
    -26: Dumped type: array&hasOffsetValue('a1', mixed)
    +26: Dumped type: non-empty-array&hasOffsetValue('a1', mixed)
     27: Dumped type: mixed
    Full report
    Line Error
    13 Dumped type: array<mixed>
    14 Dumped type: mixed
    26 Dumped type: non-empty-array&hasOffsetValue('a1', mixed)
    27 Dumped type: mixed
  6. phpstan-bot commented on Oct 13, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
    -13: Dumped type: array
    +13: Dumped type: array<mixed>
     14: Dumped type: mixed
     16: Cannot cast mixed to string.
    -26: Dumped type: array&hasOffsetValue('a1', mixed)
    +26: Dumped type: non-empty-array&hasOffsetValue('a1', mixed)
     27: Dumped type: mixed
    Full report
    Line Error
    13 Dumped type: array<mixed>
    14 Dumped type: mixed
    16 Cannot cast mixed to string.
    26 Dumped type: non-empty-array&hasOffsetValue('a1', mixed)
    27 Dumped type: mixed
  7. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -13: Dumped type: array
    +13: Dumped type: array<mixed>
     14: Dumped type: mixed
    -26: Dumped type: array&hasOffsetValue('a1', mixed)
    +26: Dumped type: non-empty-array<mixed>&hasOffsetValue('a1', mixed)
     27: Dumped type: mixed
    Full report
    Line Error
    13 Dumped type: array<mixed>
    14 Dumped type: mixed
    26 Dumped type: non-empty-array<mixed>&hasOffsetValue('a1', mixed)
    27 Dumped type: mixed
  8. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -13: Dumped type: array
    +13: Dumped type: array<mixed>
     14: Dumped type: mixed
     16: Cannot cast mixed to string.
    -26: Dumped type: array&hasOffsetValue('a1', mixed)
    -27: Dumped type: mixed
    +26: Dumped type: non-empty-array<mixed>&hasOffsetValue('a1', mixed)
    +27: Dumped type: mixed
    +29: Cannot cast mixed to string.
    Full report
    Line Error
    13 Dumped type: array<mixed>
    14 Dumped type: mixed
    16 Cannot cast mixed to string.
    26 Dumped type: non-empty-array<mixed>&hasOffsetValue('a1', mixed)
    27 Dumped type: mixed
    29 Cannot cast mixed to string.
  9. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5050

  10. 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.

  11. 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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions