Skip to content

False/negative on nullable check #7905

Closed

Description

@olivernybroe

Bug report

$key = $data === null ? null : array_key_first($data);
$key === null ? null : $data[$key];

In the following example, we know that if $key is not null, then $data is not null, as key will be set to null if $data is null.
However phpstan does not figure this out.

Code snippet that reproduces the problem

https://phpstan.org/r/37fdd6a3-9478-43a1-92d1-e4c5abe6f1d0

Expected output

No nullable error

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

Love your work! Phpstan is one of our most essential tools, so glad that we are sponsoring such an amazing project 😍

Activity

  1. added this to the Dependent types milestone on Sep 2, 2022
  2. phpstan-bot commented on Nov 4, 2023

    @phpstan-bot
    Contributor

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

    @@ @@
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
     11: Offset string does not exist on array<string, string>|null.
     
    -PHP 7.1 – 7.2 (3 errors)
    +PHP 7.1 – 7.2 (2 errors)
     ==========
     
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    -10: Function array_key_first not found.
    -11: Offset mixed does not exist on array<string, string>|null.
    +10: Function array_key_first not found.
    Full report

    PHP 8.0 – 8.2 (1 error)

    Line Error
    11 `Offset string does not exist on array<string, string>

    PHP 7.3 – 7.4 (2 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    11 `Offset string does not exist on array<string, string>

    PHP 7.1 – 7.2 (2 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    10 Function array_key_first not found.
  3. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     
     11: Offset string does not exist on array<string, string>|null.
     
    -PHP 7.3 – 7.4 (2 errors)
    +PHP 7.3 – 7.4 (5 errors)
     ==========
     
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    -11: Offset string does not exist on array<string, string>|null.
    + 8: Parameter $data of method HelloWorld::sayHello() has invalid type null.
    +10: Parameter #1 $array of function array_key_first expects array, array<string, string>|null given.
    +10: Strict comparison using === between array<string, string>|null and null will always evaluate to false.
    +11: Cannot access offset mixed on array<string, string>|null.
     
    -PHP 7.1 – 7.2 (3 errors)
    +PHP 7.1 – 7.2 (5 errors)
     ==========
     
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    + 8: Parameter $data of method HelloWorld::sayHello() has invalid type null.
     10: Function array_key_first not found.
    -11: Offset mixed does not exist on array<string, string>|null.
    +10: Strict comparison using === between array<string, string>|null and null will always evaluate to false.
    +11: Cannot access offset mixed on array<string, string>|null.
    Full report

    PHP 8.0 – 8.2 (1 error)

    Line Error
    11 `Offset string does not exist on array<string, string>

    PHP 7.3 – 7.4 (5 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    8 Parameter $data of method HelloWorld::sayHello() has invalid type null.
    10 `Parameter #1 $array of function array_key_first expects array, array<string, string>
    10 `Strict comparison using === between array<string, string>
    11 `Cannot access offset mixed on array<string, string>

    PHP 7.1 – 7.2 (5 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    8 Parameter $data of method HelloWorld::sayHello() has invalid type null.
    10 Function array_key_first not found.
    10 `Strict comparison using === between array<string, string>
    11 `Cannot access offset mixed on array<string, string>
  4. phpstan-bot commented on Sep 23, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     PHP 8.0 – 8.2 (1 error)
     ==========
     
    -11: Offset string does not exist on array<string, string>|null.
    +11: Offset string might not exist on array<string, string>|null.
     
    -PHP 7.3 – 7.4 (2 errors)
    +PHP 7.3 – 7.4 (5 errors)
     ==========
     
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    -11: Offset string does not exist on array<string, string>|null.
    + 8: Parameter $data of method HelloWorld::sayHello() has invalid type null.
    +10: Parameter #1 $array of function array_key_first expects array, array<string, string>|null given.
    +10: Strict comparison using === between array<string, string>|null and null will always evaluate to false.
    +11: Cannot access offset mixed on array<string, string>|null.
     
    -PHP 7.1 – 7.2 (3 errors)
    +PHP 7.1 – 7.2 (5 errors)
     ==========
     
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    + 8: Parameter $data of method HelloWorld::sayHello() has invalid type null.
     10: Function array_key_first not found.
    -11: Offset mixed does not exist on array<string, string>|null.
    +10: Strict comparison using === between array<string, string>|null and null will always evaluate to false.
    +11: Cannot access offset mixed on array<string, string>|null.
    Full report

    PHP 8.0 – 8.2 (1 error)

    Line Error
    11 `Offset string might not exist on array<string, string>

    PHP 7.3 – 7.4 (5 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    8 Parameter $data of method HelloWorld::sayHello() has invalid type null.
    10 `Parameter #1 $array of function array_key_first expects array, array<string, string>
    10 `Strict comparison using === between array<string, string>
    11 `Cannot access offset mixed on array<string, string>

    PHP 7.1 – 7.2 (5 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    8 Parameter $data of method HelloWorld::sayHello() has invalid type null.
    10 Function array_key_first not found.
    10 `Strict comparison using === between array<string, string>
    11 `Cannot access offset mixed on array<string, string>
  5. phpstan-bot commented on Feb 21, 2025

    @phpstan-bot
    Contributor

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

    @@ @@
     PHP 8.0 – 8.2 (1 error)
     ==========
     
    -11: Offset string does not exist on array<string, string>|null.
    +11: Offset string might not exist on array<string, string>|null.
     
    -PHP 7.3 – 7.4 (2 errors)
    +PHP 7.1 – 7.4 (5 errors)
     ==========
     
      8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    -11: Offset string does not exist on array<string, string>|null.
    -
    -PHP 7.1 – 7.2 (3 errors)
    -==========
    -
    - 8: Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    -10: Function array_key_first not found.
    -11: Offset mixed does not exist on array<string, string>|null.
    + 8: Parameter $data of method HelloWorld::sayHello() has invalid type null.
    +10: Parameter #1 $array of function array_key_first expects array, array<string, string>|null given.
    +10: Strict comparison using === between array<string, string>|null and null will always evaluate to false.
    +11: Cannot access offset mixed on array<string, string>|null.
    Full report

    PHP 8.0 – 8.2 (1 error)

    Line Error
    11 `Offset string might not exist on array<string, string>

    PHP 7.1 – 7.4 (5 errors)

    Line Error
    8 Method HelloWorld::sayHello() uses native union types but they're supported only on PHP 8.0 and later.
    8 Parameter $data of method HelloWorld::sayHello() has invalid type null.
    10 `Parameter #1 $array of function array_key_first expects array, array<string, string>
    10 `Strict comparison using === between array<string, string>
    11 `Cannot access offset mixed on array<string, string>
  6. phpstan-bot commented on Sep 11, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#6381

  7. github-actions commented on Oct 13, 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 Oct 13, 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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions