Skip to content

@var tag doesn't detect variable in unreachable code #4505

@Riimu

Description

@Riimu

Bug report

It seems that due to the refactoring of @var handling, PHPStan fails to detect the variable referred in @var tag, if the variable is in code that PHPStan considers unreachable.

In a way this does make sense and may even be intended, but the problem is that unreachable code detection seems to come at higher level than the @var analysis.

Additionally, the error is produced even if the @var tag is also considered unreachable.

Code snippet that reproduces the problem

Running this at level 2:

<?php

(function (): void {
  if (true) {
    return;
  }
  
  /** @var int $foobar */
  $foobar = 1;
});

Produces the issue:

9 | Variable $foobar in PHPDoc tag @var does not exist.

https://phpstan.org/r/af4eaa32-2f93-4e44-a91d-d4380476d831

The error doesn't really seem to make sense alone. However, at level 4 you get the following errors, which highlight the error more:

4 | If condition is always true.
9 | Unreachable statement - code above always terminates.
9 | Variable $foobar in PHPDoc tag @var does not exist.

Expected output

No error should be produced at least at level 2, if ever.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions