Skip to content

False positive: Offset ... on array... on left side of ?? always exists and is not nullable. #5933

Closed

Description

@schlndh

Bug report

Phpstan gets confused when an associative array is created in a loop and subsequently the array is foreach-ed over. It thinks that all the offsets that may have been assigned are assigned and it complains about ?? null. It's one of the new errors that I saw after upgrading to 1.0, so it's probably a regression (unless the message changed and wasn't caught by the baseline anymore).

Code snippet that reproduces the problem

https://phpstan.org/r/1420808b-7d48-41c2-936e-8e17b0bdae94

Expected output

It shouldn't complain about ?? null.

Activity

  1. ondrejmirtes commented on Jan 31, 2022

    @ondrejmirtes
    Member

    @schlndh PHPStan is smart and I think that it knows what's going on in your example: https://phpstan.org/r/1b680ac5-e330-47e7-bc0e-132f83f31588 - the 'a' index always exists.

    @chaos0815 unrelated, please open a separate issue.

  2. schlndh commented on Feb 1, 2022

    @schlndh
    ContributorAuthor

    @ondrejmirtes Thanks, but I don't understand your explanation. Clearly, it can happen that $arr[$i]['a'] is not set even if $arr[$i] is set (which I now realize is the difference between the first and second ?? null). Therefore, it is valid to write $val['a'] ?? null to acknowledge that fact (and mute the undefined index warning from PHP). So PHPStan shouldn't complain about it.

    Of course, I understand that an array structure can be arbitrarily complex and PHPStan can never understand everything. In this case, I'd say that non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}> or non-empty-array<int<0, 99>, array{a: int<0, 99>}|array{b: int<0, 99>}> would be better (I assume that either of them would prevent PHPStan from complaining about the ?? null, but I haven't checked). But I'm not sure whether it can be inferred from the code in a reasonable way.

    Ultimately, it's probably best not to write code like this in the first place and use proper types instead. 😅

  3. ondrejmirtes commented on Feb 1, 2022

    @ondrejmirtes
    Member

    I'm sorry, I misread the example, I thought that in both if/else 'a' is set and not 'b'. So yeah, this is a bug :)

  4. added this to the Easy fixes milestone on Feb 1, 2022
  5. phpstan-bot commented on Feb 1, 2022

    @phpstan-bot
    Collaborator

    @schlndh After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

    @@ @@
    -18: Offset 'a' on array{a: int<min, 99>, b: int<min, 99>} on left side of ?? always exists and is not nullable.
    +18: Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.
    Full report
    Line Error
    18 Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.
  6. phpstan-bot commented on Apr 2, 2022

    @phpstan-bot
    Collaborator

    @schlndh After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -18: Offset 'a' on array{a: int<min, 99>, b: int<min, 99>} on left side of ?? always exists and is not nullable.
    +No errors
  7. phpstan-bot commented on Apr 2, 2022

    @phpstan-bot
    Collaborator

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

    @@ @@
    -17: Dumped type: non-empty-array<int<0, 99>, array{a: int<0, 99>, b: int<0, 99>}>
    -20: Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.
    +17: Dumped type: non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}&non-empty-array>
    Full report
    Line Error
    17 Dumped type: non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}&non-empty-array>
  8. github-actions commented on May 4, 2022

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

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