1
0
Fork 0

Use context in DocBlockParser

pull/50/head
Felix Becker 2016-10-19 01:24:51 +02:00
parent 954a81f427
commit 7669ba9c21
1 changed files with 36 additions and 1 deletions

View File

@ -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 = [];
}
}
}