Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve type aliases in docblocks #24

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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