Skip to content

Allow top-level @phpdoc-type aliases not bound to classes for use in functions #9164

@westonruter

Description

@westonruter

Feature request

Consider this example code with a function (playground):

/**
 * @phpstan-type BlockAttributes array{
 *     language: string,
 *     highlightedLines: string,
 *     showLineNumbers: bool,
 *     wrapLines: bool
 * }
 */

/**
 * Render.
 *
 * @param BlockAttributes $attributes Block attributes.
 * @param string          $content    Content.
 * @return string Rendered block.
 */
function render_block( array $attributes, string $content ): string {
	/* ... */
	return $content;
}

PHPStan currently fails here with three errors:

  1. Function render_block() has parameter $attributes with no value type specified in iterable type array.
  2. PHPDoc tag @param for parameter $attributes with type BlockAttributes is incompatible with native type array.
  3. Parameter $attributes of function render_block() has invalid type BlockAttributes.

It seems that the @phpstan-type alias only works when it is attached to a class (playground).

It would be useful if the class association wasn't required.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions