Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow @phpstan-use (and @use) in PHPdoc-block for classes #7486

@nagmat84

Description

@nagmat84

Feature request

PHPStan only recognizes annotations, if the annotation is inside a PHPdoc block (see PHPStan: PHPDocs Basics). But PHPdoc blocks are only allowed for structural elements of the PHP language as well as files, include and require statements (see phpDocumentor: What can be documented in your source code?).

However, if one uses a generic trait with a template parameter, one has to annotate the use-statement with the proper template parameter like this

/**
 * @template TDataType
 */
trait MyTrait {}

class MyClassA {}

class MyClassB {
  /** @phpstan-use MyTrait<MyClassA> */
  use MyTrait;
}

This is not 100% ideal as there are other tools (e.g. CS Fixer for PHP) which convert those wrongly placed PHPdoc comments into normal comments.

A solution would be to put this annotation into the PHPdoc block of the class (similar to @extends) like this

/**
 * @template TDataType
 */
trait MyTrait {}

class MyClassA {}

/**
 * @phpstan-use MyTrait<MyClassA>
 */
class MyClass {
  use MyTrait;
}

Did PHPStan help you today?

Currently, we are enabling PHPStan for Lychee as part of this PR LycheeOrg/Lychee#1336. While we are struggling with a lot of false positives due to the used framework, it already helped us to discover a lot of bugs which were dormant in less frequently used features of Lychee.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions