Skip to content

False positive "Readonly property is already assigned" in if-else #6402

Closed

Description

@javer

Bug report

After updating PHPStan from 1.3.1 to 1.4.1 assigning to a readonly property in the different branches of the same condition (if-else) is started to false trigger the error "Readonly property is already assigned".

Code snippet that reproduces the problem

https://phpstan.org/r/2069a468-0e58-4991-a125-69f1ce64f7a3

Expected output

No error should be reported.

Activity

  1. ondrejmirtes commented on Jan 18, 2022

    @ondrejmirtes
    Member
  2. javer commented on Jan 18, 2022

    @javer
    Author

    Yes, ternary operator works okay, but of course it is very simplified example, in real world it is much more bigger if-else with several assignments in both branches, so it cannot be replaced with a single ternary operator.

  3. apfelbox commented on Mar 10, 2022

    @apfelbox

    Just ran into the same issue. I assign multiple values in the if branch, so proper support would be great.

  4. tarlepp commented on May 31, 2022

    @tarlepp

    I have the same issue, it would be really great if there is proper support for if/else blocks - using ternary operator is not always possible.

  5. peternormann commented on Nov 9, 2022

    @peternormann

    I ran into the same issue.

    As a temporary work-around, I did this:

    https://phpstan.org/r/5f37b20f-fe0b-4ec2-add0-cad3ec933032

  6. gndk commented on Dec 3, 2022

    @gndk

    I ran into the same issue.

    As a temporary work-around, I did this:

    https://phpstan.org/r/5f37b20f-fe0b-4ec2-add0-cad3ec933032

    I just ran into this, too. But my "work-around" is actually to add those errors to the baseline, since the fix is already in progress 🙂

  7. lukasojd commented on Apr 27, 2023

    @lukasojd

    +1

  8. akmandev commented on Jun 20, 2023

    @akmandev
    Contributor

    @ondrejmirtes I would like to fix this if you could give me some directions as I don't really know the phpstan codebase

  9. ondrejmirtes commented on Jun 20, 2023

    @ondrejmirtes
    Member

    @ozanakman I want to fix this using control flow graph which is gonna be a huge effort: #8192

    But I just realized there might be an easier way which I'm gonna try sooner.

  10. ondrejmirtes commented on Jul 2, 2023

    @ondrejmirtes
    Member
  11. apfelbox commented on Jul 2, 2023

    @apfelbox

    Hi @ondrejmirtes,

    thank you so much for this fix. Does the fix also cover try-catch?

  12. ondrejmirtes commented on Jul 2, 2023

    @ondrejmirtes
    Member

    @apfelbox Please try it out on phpstan.org/try, and in case of unexpected behaviour open a new bug report :)

  13. apfelbox commented on Jul 3, 2023

    @apfelbox
  14. github-actions commented on Aug 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.

  15. locked as resolved and limited conversation to collaborators on Aug 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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions