Skip to content

Subsequent nullsafe method calls on nullable variable results in false positive #6922

Closed

Description

@egonolieux

Bug report

Subsequent nullsafe method calls on nullable variable results in false positive.

Code snippet that reproduces the problem

https://phpstan.org/r/6ec02114-a3fc-4815-b655-7c688e67c445

Expected output

No error expected; false positive.

Activity

  1. added this to the Easy fixes milestone on Mar 28, 2022
  2. janchaloupka commented on Sep 20, 2022

    @janchaloupka

    Hello I have also encountered this issue.

    I have tried to narrow down the problem and I found that PHPStan correctly assumes the type if the (possible null) bool value is compared to TRUE but throws an error if compared to FALSE. Since both comparisons are strict I believe this to be a bug.

    Here is the issue demonstrated using the PHPStan playground:

    Strict comparison with FALSE value (incorrect type assumed).
    https://phpstan.org/r/1f061693-59d3-4205-b910-1e2822f9d499

    Strict comparison with TRUE value (everything works as expected).
    https://phpstan.org/r/cc552cae-25f0-4a5e-8c01-80ef681a4278

  3. phpstan-bot commented on Oct 30, 2023

    @phpstan-bot
    Collaborator

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

    @@ @@
    -20: Using nullsafe method call on non-nullable type Configuration. Use -> instead.
    +No errors
  4. phpstan-bot commented on Oct 30, 2023

    @phpstan-bot
    Collaborator

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

    @@ @@
    -PHP 8.1 – 8.2 (1 error)
    +PHP 8.1 – 8.2
     ==========
     
    -19: Using nullsafe property access on non-nullable type Person. Use -> instead.
    +No errors
     
     PHP 7.1 – 8.0 (1 error)
     ==========
     
      6: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 6
    Full report

    PHP 8.1 – 8.2

    No errors

    PHP 7.1 – 8.0 (1 error)

    Line Error
    6 Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 6
  5. added a commit that references this issue on Nov 2, 2023
  6. github-actions commented on Dec 4, 2023

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

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