Skip to content
Permalink
Browse files

Fix #2488 - add support for @phpstan- annotations

  • Loading branch information
muglug committed Dec 20, 2019
1 parent a419117 commit 1b54d058aa0178e080b7209108afec4e14648f5c
Showing with 57 additions and 22 deletions.
  1. +51 −18 src/Psalm/Internal/Analyzer/CommentAnalyzer.php
  2. +6 −4 src/Psalm/Internal/Visitor/ReflectorVisitor.php
@@ -98,8 +98,9 @@ public static function arrayToDocblocks(
$var_line_number = $comment->getLine();

if ($parsed_docblock) {
$all_vars = (isset($parsed_docblock['specials']['var']) ? $parsed_docblock['specials']['var'] : [])
+ (isset($parsed_docblock['specials']['psalm-var']) ? $parsed_docblock['specials']['psalm-var'] : []);
$all_vars = ($parsed_docblock['specials']['var'] ?? [])
+ ($parsed_docblock['specials']['phpstan-var'] ?? [])
+ ($parsed_docblock['specials']['psalm-var'] ?? []);

foreach ($all_vars as $offset => $var_line) {
$var_line = trim($var_line);
@@ -337,10 +338,17 @@ public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $commen

$info = new FunctionDocblockComment();

if (isset($parsed_docblock['specials']['return']) || isset($parsed_docblock['specials']['psalm-return'])) {
$return_specials = isset($parsed_docblock['specials']['psalm-return'])
? $parsed_docblock['specials']['psalm-return']
: $parsed_docblock['specials']['return'];
if (isset($parsed_docblock['specials']['return'])
|| isset($parsed_docblock['specials']['psalm-return'])
|| isset($parsed_docblock['specials']['phpstan-return'])
) {
if (isset($parsed_docblock['specials']['psalm-return'])) {
$return_specials = $parsed_docblock['specials']['psalm-return'];
} elseif (isset($parsed_docblock['specials']['phpstan-return'])) {
$return_specials = $parsed_docblock['specials']['phpstan-return'];
} else {
$return_specials = $parsed_docblock['specials']['return'];
}

self::extractReturnType(
$comment,
@@ -349,11 +357,17 @@ public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $commen
);
}

if (isset($parsed_docblock['specials']['param']) || isset($parsed_docblock['specials']['psalm-param'])) {
if (isset($parsed_docblock['specials']['param'])
|| isset($parsed_docblock['specials']['psalm-param'])
|| isset($parsed_docblock['specials']['phpstan-param'])
) {
$all_params =
(isset($parsed_docblock['specials']['param'])
? $parsed_docblock['specials']['param']
: [])
+ (isset($parsed_docblock['specials']['phpstan-param'])
? $parsed_docblock['specials']['phpstan-param']
: [])
+ (isset($parsed_docblock['specials']['psalm-param'])
? $parsed_docblock['specials']['psalm-param']
: []);
@@ -545,13 +559,19 @@ public static function extractFunctionDocblockInfo(PhpParser\Comment\Doc $commen
$info->inheritdoc = true;
}

if (isset($parsed_docblock['specials']['template']) || isset($parsed_docblock['specials']['psalm-template'])) {
if (isset($parsed_docblock['specials']['template'])
|| isset($parsed_docblock['specials']['psalm-template'])
|| isset($parsed_docblock['specials']['phpstan-template'])
) {
$all_templates
= (isset($parsed_docblock['specials']['template'])
? $parsed_docblock['specials']['template']
: [])
+ (isset($parsed_docblock['specials']['psalm-template'])
? $parsed_docblock['specials']['psalm-template']
: [])
+ (isset($parsed_docblock['specials']['phpstan-template'])
? $parsed_docblock['specials']['phpstan-template']
: []);

foreach ($all_templates as $template_line) {
@@ -721,11 +741,16 @@ public static function extractClassLikeDocblockInfo(

$info = new ClassLikeDocblockComment();

if (isset($parsed_docblock['specials']['template']) || isset($parsed_docblock['specials']['psalm-template'])) {
if (isset($parsed_docblock['specials']['template'])
|| isset($parsed_docblock['specials']['psalm-template'])
) {
$all_templates
= (isset($parsed_docblock['specials']['template'])
? $parsed_docblock['specials']['template']
: [])
+ (isset($parsed_docblock['specials']['phpstan-template'])
? $parsed_docblock['specials']['phpstan-template']
: [])
+ (isset($parsed_docblock['specials']['psalm-template'])
? $parsed_docblock['specials']['psalm-template']
: []);
@@ -758,11 +783,15 @@ public static function extractClassLikeDocblockInfo(

if (isset($parsed_docblock['specials']['template-covariant'])
|| isset($parsed_docblock['specials']['psalm-template-covariant'])
|| isset($parsed_docblock['specials']['phpstan-template-covariant'])
) {
$all_templates =
(isset($parsed_docblock['specials']['template-covariant'])
? $parsed_docblock['specials']['template-covariant']
: [])
+ (isset($parsed_docblock['specials']['phpstan-template-covariant'])
? $parsed_docblock['specials']['phpstan-template-covariant']
: [])
+ (isset($parsed_docblock['specials']['psalm-template-covariant'])
? $parsed_docblock['specials']['psalm-template-covariant']
: []);
@@ -796,12 +825,14 @@ public static function extractClassLikeDocblockInfo(
if (isset($parsed_docblock['specials']['template-extends'])
|| isset($parsed_docblock['specials']['inherits'])
|| isset($parsed_docblock['specials']['extends'])
|| isset($parsed_docblock['specials']['psalm-extends'])
|| isset($parsed_docblock['specials']['phpstan-extends'])
) {
$all_inheritance = array_merge(
$parsed_docblock['specials']['template-extends'] ?? [],
$parsed_docblock['specials']['inherits'] ?? [],
$parsed_docblock['specials']['extends'] ?? []
);
$all_inheritance = ($parsed_docblock['specials']['template-extends'] ?? [])
+ ($parsed_docblock['specials']['inherits'] ?? [])
+ ($parsed_docblock['specials']['extends'] ?? [])
+ ($parsed_docblock['specials']['psalm-extends'] ?? [])
+ ($parsed_docblock['specials']['phpstan-extends'] ?? []);

foreach ($all_inheritance as $template_line) {
$info->template_extends[] = trim(preg_replace('@^[ \t]*\*@m', '', $template_line));
@@ -810,11 +841,13 @@ public static function extractClassLikeDocblockInfo(

if (isset($parsed_docblock['specials']['template-implements'])
|| isset($parsed_docblock['specials']['implements'])
|| isset($parsed_docblock['specials']['phpstan-implements'])
|| isset($parsed_docblock['specials']['psalm-implements'])
) {
$all_inheritance = array_merge(
$parsed_docblock['specials']['template-implements'] ?? [],
$parsed_docblock['specials']['implements'] ?? []
);
$all_inheritance = ($parsed_docblock['specials']['template-implements'] ?? [])
+ ($parsed_docblock['specials']['implements'] ?? [])
+ ($parsed_docblock['specials']['phpstan-implements'] ?? [])
+ ($parsed_docblock['specials']['psalm-implements'] ?? []);

foreach ($all_inheritance as $template_line) {
$info->template_implements[] = trim(preg_replace('@^[ \t]*\*@m', '', $template_line));
@@ -404,11 +404,13 @@ public function enterNode(PhpParser\Node $node)

if (isset($comments['specials']['template-use'])
|| isset($comments['specials']['use'])
|| isset($comments['specials']['phpstan-use'])
|| isset($comments['specials']['psalm-use'])
) {
$all_inheritance = array_merge(
$comments['specials']['template-use'] ?? [],
$comments['specials']['use'] ?? []
);
$all_inheritance = ($comments['specials']['template-use'] ?? [])
+ ($comments['specials']['use'] ?? [])
+ ($comments['specials']['phpstan-use'] ?? [])
+ ($comments['specials']['psalm-use'] ?? []);

foreach ($all_inheritance as $template_line) {
$this->useTemplatedType(

0 comments on commit 1b54d05

Please sign in to comment.
You can’t perform that action at this time.