Skip to content
Permalink
Browse files

Fix #1989 - support fully-qualified params in @method annotations

  • Loading branch information...
muglug committed Aug 8, 2019
1 parent d5e2c32 commit bdac01582d6e6a85d94ec45a0576118e0e235030
@@ -715,8 +715,11 @@ private static function extractReturnType(
* @return ClassLikeDocblockComment
* @psalm-suppress MixedArrayAccess
*/
public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, PhpParser\Comment\Doc $comment)
{
public static function extractClassLikeDocblockInfo(
\PhpParser\Node $node,
PhpParser\Comment\Doc $comment,
Aliases $aliases
) {
$parsed_docblock = DocComment::parsePreservingLength($comment);
$info = new ClassLikeDocblockComment();
@@ -895,7 +898,13 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, PhpPa
$method_entry = preg_replace('/ (?!(\$|\.\.\.|&))/', '', trim($method_entry));
try {
$method_tree = ParseTree::createFromTokens(Type::tokenize($method_entry, false));
$method_tree = ParseTree::createFromTokens(
Type::fixUpLocalType(
$method_entry,
$aliases,
null
)
);
} catch (TypeParseTreeException $e) {
throw new DocblockParseException($method_entry . ' is not a valid method');
}
@@ -929,7 +938,7 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, PhpPa
if ($method_tree_child->children) {
$param_type = Type::getTypeFromTree($method_tree_child->children[0]);
$docblock_lines[] = '@param ' . $param_type . ' '
$docblock_lines[] = '@param \\' . $param_type . ' '
. ($method_tree_child->variadic ? '...' : '')
. $method_tree_child->name;
}
@@ -337,6 +337,11 @@ public static function createFromTokens(array $type_tokens)
$current_parent = $current_leaf->parent;
if ($current_parent instanceof ParseTree\CallableTree) {
++$i;
break;
}
while ($current_parent && !$current_parent instanceof ParseTree\MethodTree) {
$current_leaf = $current_parent;
$current_parent = $current_parent->parent;
@@ -1053,7 +1053,8 @@ private function registerClassLike(PhpParser\Node\Stmt\ClassLike $node)
try {
$docblock_info = CommentAnalyzer::extractClassLikeDocblockInfo(
$node,
$doc_comment
$doc_comment,
$this->aliases
);
} catch (DocblockParseException $e) {
if (IssueBuffer::accepts(
@@ -773,14 +773,20 @@ public static function tokenize($string_type, $ignore_space = true)
$rtc = 0;
$chars = str_split($string_type);
$was_space = false;
for ($i = 0, $c = count($chars); $i < $c; ++$i) {
$char = $chars[$i];
if (!$quote_char && $char === ' ' && $ignore_space) {
$was_space = true;
continue;
}
if ($was_char) {
if ($was_space && ($char === '$' || $char === '.')) {
$type_tokens[++$rtc] = [' ', $i - 1];
$type_tokens[++$rtc] = ['', $i];
} elseif ($was_char) {
$type_tokens[++$rtc] = ['', $i];
}
@@ -822,6 +828,8 @@ public static function tokenize($string_type, $ignore_space = true)
$quote_char = $char;
$was_char = false;
$was_space = false;
continue;
}
@@ -847,6 +855,7 @@ public static function tokenize($string_type, $ignore_space = true)
}
$was_char = true;
$was_space = false;
continue;
}
@@ -860,6 +869,7 @@ public static function tokenize($string_type, $ignore_space = true)
}
$was_char = true;
$was_space = false;
++$i;
@@ -873,6 +883,7 @@ public static function tokenize($string_type, $ignore_space = true)
}
$was_char = true;
$was_space = false;
continue;
}
@@ -885,6 +896,7 @@ public static function tokenize($string_type, $ignore_space = true)
) {
$type_tokens[$rtc][0] .= $char;
$was_char = false;
$was_space = false;
continue;
}
@@ -900,6 +912,7 @@ public static function tokenize($string_type, $ignore_space = true)
}
$was_char = true;
$was_space = false;
$i += 2;
@@ -908,6 +921,7 @@ public static function tokenize($string_type, $ignore_space = true)
$type_tokens[$rtc][0] .= $char;
$was_char = false;
$was_space = false;
}
self::$memoized_tokens[$string_type] = $type_tokens;
@@ -1003,7 +1017,11 @@ public static function fixUpLocalType(
}
}
if ($string_type_token[0][0] === '$') {
if ($string_type_token[0][0] === '$' || $string_type_token[0][0] === ' ') {
continue;
}
if (isset($type_tokens[$i + 1]) && $type_tokens[$i + 1][0] === '(') {
continue;
}
@@ -444,6 +444,8 @@ function sayHello() : string {
],
'inheritInterfacePseudoMethodsFromParent' => [
'<?php
namespace Foo;
interface ClassMetadata {}
interface ORMClassMetadata extends ClassMetadata {}
@@ -473,6 +475,22 @@ function test2(int $metadata): void {}
test2(em()->getOtherMetadata());
test2(concreteEm()->getOtherMetadata());',
],
'fullyQualifiedParam' => [
'<?php
namespace Foo {
/**
* @method void setInteger(\Closure $c)
*/
class Child {
public function __call(string $s, array $args) {}
}
}
namespace {
$child = new Foo\Child();
$child->setInteger(function() : void {});
}',
],
];
}

0 comments on commit bdac015

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