Skip to content

Commit

Permalink
fix: resolve type aliases in docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
CHItA authored and williamdes committed Jan 7, 2021
1 parent 76dd2c8 commit 1724f16
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 97 deletions.
16 changes: 13 additions & 3 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3151,17 +3151,22 @@ parameters:
path: tests/Parser/DocBlockParserTest.php

-
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:testParse7dot1plus\\(\\) has parameter \\$expected with no value type specified in iterable type array\\.$#"
message: "#^Parameter \\#2 \\$context of method Doctum\\\\Parser\\\\DocBlockParser\\:\\:parse\\(\\) expects Doctum\\\\Parser\\\\ParserContext, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
count: 3
path: tests/Parser/DocBlockParserTest.php

-
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:testParseWithNamespace\\(\\) has parameter \\$expected with no value type specified in iterable type array\\.$#"
count: 1
path: tests/Parser/DocBlockParserTest.php

-
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:getParseTests\\(\\) return type has no value type specified in iterable type array\\.$#"
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:testParseWithAliases\\(\\) has parameter \\$expected with no value type specified in iterable type array\\.$#"
count: 1
path: tests/Parser/DocBlockParserTest.php

-
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:getParseTestsphp7dot1plus\\(\\) return type has no value type specified in iterable type array\\.$#"
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:getParseTests\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: tests/Parser/DocBlockParserTest.php

Expand All @@ -3170,6 +3175,11 @@ parameters:
count: 1
path: tests/Parser/DocBlockParserTest.php

-
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\DocBlockParserTest\\:\\:getContextMock\\(\\) has parameter \\$aliases with no value type specified in iterable type array\\.$#"
count: 1
path: tests/Parser/DocBlockParserTest.php

-
message: "#^Method Doctum\\\\Tests\\\\Parser\\\\NodeVisitorTest\\:\\:testMethodTypehints\\(\\) has parameter \\$expectedHints with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
9 changes: 7 additions & 2 deletions src/Parser/DocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\Types\Context;

class DocBlockParser
{
public function parse(?string $comment): DocBlockNode
public function parse(?string $comment, ParserContext $context): DocBlockNode
{
$docBlock = null;
$errorMessage = '';
Expand All @@ -38,7 +39,11 @@ public function parse(?string $comment): DocBlockNode

try {
$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($comment);
$docBlockContext = new Context(
$context->getNamespace() ?? '',
$context->getAliases() ?: []
);
$docBlock = $factory->create($comment, $docBlockContext);
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Parser/NodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public function leaveNode(AbstractNode $node)
protected function addAliases(UseNode $node)
{
foreach ($node->uses as $use) {
$this->context->addAlias($use->alias !== null ? $use->alias->__toString() : null, $use->name->__toString());
$alias = $use->getAlias()->toString();
$fullName = $use->name->__toString();
$this->context->addAlias($alias, $fullName);
}
}

Expand Down
Loading

0 comments on commit 1724f16

Please sign in to comment.