From 7669ba9c21f79c54f68be8371371982f8f5360d3 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Wed, 19 Oct 2016 01:24:51 +0200 Subject: [PATCH] Use context in DocBlockParser --- src/NodeVisitor/DocBlockParser.php | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/NodeVisitor/DocBlockParser.php b/src/NodeVisitor/DocBlockParser.php index aeb9b8a..abc3b05 100644 --- a/src/NodeVisitor/DocBlockParser.php +++ b/src/NodeVisitor/DocBlockParser.php @@ -5,6 +5,7 @@ namespace LanguageServer\NodeVisitor; use PhpParser\{NodeVisitorAbstract, Node}; use phpDocumentor\Reflection\DocBlockFactory; +use phpDocumentor\Reflection\Types\Context; /** * Decorates all nodes with a docBlock attribute that is an instance of phpDocumentor\Reflection\DocBlock @@ -16,18 +17,52 @@ class DocBlockParser extends NodeVisitorAbstract */ private $docBlockFactory; + /** + * The current namespace context + * + * @var string + */ + private $namespace; + + /** + * Namespace aliases in the current context + * + * @var string[] + */ + private $aliases; + public function __construct(DocBlockFactory $docBlockFactory) { $this->docBlockFactory = $docBlockFactory; } + public function beforeTraverse(array $nodes) + { + $this->namespace = ''; + $this->aliases = []; + } + public function enterNode(Node $node) { + if ($node instanceof Node\Stmt\Namespace_) { + $this->namespace = (string)$node->name; + } else if ($node instanceof Node\Stmt\UseUse) { + $this->aliases[(string)$node->name] = $node->alias; + } $docComment = $node->getDocComment(); if ($docComment === null) { return; } - $docBlock = $this->docBlockFactory->create($docComment->getText()); + $context = new Context($this->namespace, $this->aliases); + $docBlock = $this->docBlockFactory->create($docComment->getText(), $context); $node->setAttribute('docBlock', $docBlock); } + + public function leaveNode(Node $node) + { + if ($node instanceof Node\Stmt\Namespace_) { + $this->namespace = ''; + $this->aliases = []; + } + } }