Skip to content

Variable might not be defined when using @var to define it #3515

@villermen

Description

@villermen

I originally hijacked #351 because I was testing the new @var type $this support present in v0.12.30. It may or may not be related to this change.

Defininig a variable besides $this using an @var PHPDoc annotation does not make PHPStan think the variable exists for the rest of the file:

https://phpstan.org/r/17f3c263-543f-44ae-9f96-45e2ff5221ee

However, once you define and use $this via an @var PHPDoc annotation this error will disappear entirely:

https://phpstan.org/r/337c0f49-8b85-4044-be0a-e014fbae63ea

I expect the variable definition to be honored regardless of whether I have defined and used $this beforehand. Note that trying to use the variable before using $this in the previous snippet will also yield the error:

https://phpstan.org/r/a99c334b-b0b3-4a6a-b0d4-7b175616b695

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