🔧
This commit is contained in:
@@ -10,8 +10,7 @@ use PhpParser\NodeVisitorAbstract;
|
||||
*
|
||||
* This visitor is required to perform format-preserving pretty prints.
|
||||
*/
|
||||
class CloningVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
class CloningVisitor extends NodeVisitorAbstract {
|
||||
public function enterNode(Node $origNode) {
|
||||
$node = clone $origNode;
|
||||
$node->setAttribute('origNode', $origNode);
|
||||
|
||||
82
vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CommentAnnotatingVisitor.php
vendored
Normal file
82
vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/CommentAnnotatingVisitor.php
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\NodeVisitor;
|
||||
|
||||
use PhpParser\Comment;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use PhpParser\Token;
|
||||
|
||||
class CommentAnnotatingVisitor extends NodeVisitorAbstract {
|
||||
/** @var int Last seen token start position */
|
||||
private int $pos = 0;
|
||||
/** @var Token[] Token array */
|
||||
private array $tokens;
|
||||
/** @var list<int> Token positions of comments */
|
||||
private array $commentPositions = [];
|
||||
|
||||
/**
|
||||
* Create a comment annotation visitor.
|
||||
*
|
||||
* @param Token[] $tokens Token array
|
||||
*/
|
||||
public function __construct(array $tokens) {
|
||||
$this->tokens = $tokens;
|
||||
|
||||
// Collect positions of comments. We use this to avoid traversing parts of the AST where
|
||||
// there are no comments.
|
||||
foreach ($tokens as $i => $token) {
|
||||
if ($token->id === \T_COMMENT || $token->id === \T_DOC_COMMENT) {
|
||||
$this->commentPositions[] = $i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function enterNode(Node $node) {
|
||||
$nextCommentPos = current($this->commentPositions);
|
||||
if ($nextCommentPos === false) {
|
||||
// No more comments.
|
||||
return self::STOP_TRAVERSAL;
|
||||
}
|
||||
|
||||
$oldPos = $this->pos;
|
||||
$this->pos = $pos = $node->getStartTokenPos();
|
||||
if ($nextCommentPos > $oldPos && $nextCommentPos < $pos) {
|
||||
$comments = [];
|
||||
while (--$pos >= $oldPos) {
|
||||
$token = $this->tokens[$pos];
|
||||
if ($token->id === \T_DOC_COMMENT) {
|
||||
$comments[] = new Comment\Doc(
|
||||
$token->text, $token->line, $token->pos, $pos,
|
||||
$token->getEndLine(), $token->getEndPos() - 1, $pos);
|
||||
continue;
|
||||
}
|
||||
if ($token->id === \T_COMMENT) {
|
||||
$comments[] = new Comment(
|
||||
$token->text, $token->line, $token->pos, $pos,
|
||||
$token->getEndLine(), $token->getEndPos() - 1, $pos);
|
||||
continue;
|
||||
}
|
||||
if ($token->id !== \T_WHITESPACE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty($comments)) {
|
||||
$node->setAttribute('comments', array_reverse($comments));
|
||||
}
|
||||
|
||||
do {
|
||||
$nextCommentPos = next($this->commentPositions);
|
||||
} while ($nextCommentPos !== false && $nextCommentPos < $this->pos);
|
||||
}
|
||||
|
||||
$endPos = $node->getEndTokenPos();
|
||||
if ($nextCommentPos > $endPos) {
|
||||
// Skip children if there are no comments located inside this node.
|
||||
$this->pos = $endPos;
|
||||
return self::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,11 @@ use PhpParser\NodeVisitorAbstract;
|
||||
* This visitor can be used to find and collect all nodes satisfying some criterion determined by
|
||||
* a filter callback.
|
||||
*/
|
||||
class FindingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
class FindingVisitor extends NodeVisitorAbstract {
|
||||
/** @var callable Filter callback */
|
||||
protected $filterCallback;
|
||||
/** @var Node[] Found nodes */
|
||||
protected $foundNodes;
|
||||
protected array $foundNodes;
|
||||
|
||||
public function __construct(callable $filterCallback) {
|
||||
$this->filterCallback = $filterCallback;
|
||||
@@ -27,11 +26,11 @@ class FindingVisitor extends NodeVisitorAbstract
|
||||
*
|
||||
* @return Node[] Found nodes
|
||||
*/
|
||||
public function getFoundNodes() : array {
|
||||
public function getFoundNodes(): array {
|
||||
return $this->foundNodes;
|
||||
}
|
||||
|
||||
public function beforeTraverse(array $nodes) {
|
||||
public function beforeTraverse(array $nodes): ?array {
|
||||
$this->foundNodes = [];
|
||||
|
||||
return null;
|
||||
|
||||
@@ -3,19 +3,18 @@
|
||||
namespace PhpParser\NodeVisitor;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
/**
|
||||
* This visitor can be used to find the first node satisfying some criterion determined by
|
||||
* a filter callback.
|
||||
*/
|
||||
class FirstFindingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
class FirstFindingVisitor extends NodeVisitorAbstract {
|
||||
/** @var callable Filter callback */
|
||||
protected $filterCallback;
|
||||
/** @var null|Node Found node */
|
||||
protected $foundNode;
|
||||
protected ?Node $foundNode;
|
||||
|
||||
public function __construct(callable $filterCallback) {
|
||||
$this->filterCallback = $filterCallback;
|
||||
@@ -28,11 +27,11 @@ class FirstFindingVisitor extends NodeVisitorAbstract
|
||||
*
|
||||
* @return null|Node Found node (or null if not found)
|
||||
*/
|
||||
public function getFoundNode() {
|
||||
public function getFoundNode(): ?Node {
|
||||
return $this->foundNode;
|
||||
}
|
||||
|
||||
public function beforeTraverse(array $nodes) {
|
||||
public function beforeTraverse(array $nodes): ?array {
|
||||
$this->foundNode = null;
|
||||
|
||||
return null;
|
||||
@@ -42,7 +41,7 @@ class FirstFindingVisitor extends NodeVisitorAbstract
|
||||
$filterCallback = $this->filterCallback;
|
||||
if ($filterCallback($node)) {
|
||||
$this->foundNode = $node;
|
||||
return NodeTraverser::STOP_TRAVERSAL;
|
||||
return NodeVisitor::STOP_TRAVERSAL;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -11,16 +11,15 @@ use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
class NameResolver extends NodeVisitorAbstract
|
||||
{
|
||||
class NameResolver extends NodeVisitorAbstract {
|
||||
/** @var NameContext Naming context */
|
||||
protected $nameContext;
|
||||
protected NameContext $nameContext;
|
||||
|
||||
/** @var bool Whether to preserve original names */
|
||||
protected $preserveOriginalNames;
|
||||
protected bool $preserveOriginalNames;
|
||||
|
||||
/** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
|
||||
protected $replaceNodes;
|
||||
protected bool $replaceNodes;
|
||||
|
||||
/**
|
||||
* Constructs a name resolution visitor.
|
||||
@@ -33,24 +32,22 @@ class NameResolver extends NodeVisitorAbstract
|
||||
* namespacedName attribute, as usual.)
|
||||
*
|
||||
* @param ErrorHandler|null $errorHandler Error handler
|
||||
* @param array $options Options
|
||||
* @param array{preserveOriginalNames?: bool, replaceNodes?: bool} $options Options
|
||||
*/
|
||||
public function __construct(ErrorHandler $errorHandler = null, array $options = []) {
|
||||
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing);
|
||||
public function __construct(?ErrorHandler $errorHandler = null, array $options = []) {
|
||||
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing());
|
||||
$this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
|
||||
$this->replaceNodes = $options['replaceNodes'] ?? true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name resolution context.
|
||||
*
|
||||
* @return NameContext
|
||||
*/
|
||||
public function getNameContext() : NameContext {
|
||||
public function getNameContext(): NameContext {
|
||||
return $this->nameContext;
|
||||
}
|
||||
|
||||
public function beforeTraverse(array $nodes) {
|
||||
public function beforeTraverse(array $nodes): ?array {
|
||||
$this->nameContext->startNamespace();
|
||||
return null;
|
||||
}
|
||||
@@ -78,6 +75,8 @@ class NameResolver extends NodeVisitorAbstract
|
||||
$this->resolveAttrGroups($node);
|
||||
if (null !== $node->name) {
|
||||
$this->addNamespacedName($node);
|
||||
} else {
|
||||
$node->namespacedName = null;
|
||||
}
|
||||
} elseif ($node instanceof Stmt\Interface_) {
|
||||
foreach ($node->extends as &$interface) {
|
||||
@@ -86,15 +85,13 @@ class NameResolver extends NodeVisitorAbstract
|
||||
|
||||
$this->resolveAttrGroups($node);
|
||||
$this->addNamespacedName($node);
|
||||
} elseif ($node instanceof Stmt\Enum_) {
|
||||
} elseif ($node instanceof Stmt\Enum_) {
|
||||
foreach ($node->implements as &$interface) {
|
||||
$interface = $this->resolveClassName($interface);
|
||||
}
|
||||
|
||||
$this->resolveAttrGroups($node);
|
||||
if (null !== $node->name) {
|
||||
$this->addNamespacedName($node);
|
||||
}
|
||||
$this->addNamespacedName($node);
|
||||
} elseif ($node instanceof Stmt\Trait_) {
|
||||
$this->resolveAttrGroups($node);
|
||||
$this->addNamespacedName($node);
|
||||
@@ -117,12 +114,12 @@ class NameResolver extends NodeVisitorAbstract
|
||||
foreach ($node->consts as $const) {
|
||||
$this->addNamespacedName($const);
|
||||
}
|
||||
} else if ($node instanceof Stmt\ClassConst) {
|
||||
} elseif ($node instanceof Stmt\ClassConst) {
|
||||
if (null !== $node->type) {
|
||||
$node->type = $this->resolveType($node->type);
|
||||
}
|
||||
$this->resolveAttrGroups($node);
|
||||
} else if ($node instanceof Stmt\EnumCase) {
|
||||
} elseif ($node instanceof Stmt\EnumCase) {
|
||||
$this->resolveAttrGroups($node);
|
||||
} elseif ($node instanceof Expr\StaticCall
|
||||
|| $node instanceof Expr\StaticPropertyFetch
|
||||
@@ -164,7 +161,8 @@ class NameResolver extends NodeVisitorAbstract
|
||||
return null;
|
||||
}
|
||||
|
||||
private function addAlias(Stmt\UseUse $use, int $type, Name $prefix = null) {
|
||||
/** @param Stmt\Use_::TYPE_* $type */
|
||||
private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void {
|
||||
// Add prefix for group uses
|
||||
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
|
||||
// Type is determined either by individual element or whole use declaration
|
||||
@@ -175,8 +173,8 @@ class NameResolver extends NodeVisitorAbstract
|
||||
);
|
||||
}
|
||||
|
||||
/** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */
|
||||
private function resolveSignature($node) {
|
||||
/** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure|Expr\ArrowFunction $node */
|
||||
private function resolveSignature($node): void {
|
||||
foreach ($node->params as $param) {
|
||||
$param->type = $this->resolveType($param->type);
|
||||
$this->resolveAttrGroups($param);
|
||||
@@ -184,7 +182,12 @@ class NameResolver extends NodeVisitorAbstract
|
||||
$node->returnType = $this->resolveType($node->returnType);
|
||||
}
|
||||
|
||||
private function resolveType($node) {
|
||||
/**
|
||||
* @template T of Node\Identifier|Name|Node\ComplexType|null
|
||||
* @param T $node
|
||||
* @return T
|
||||
*/
|
||||
private function resolveType(?Node $node): ?Node {
|
||||
if ($node instanceof Name) {
|
||||
return $this->resolveClassName($node);
|
||||
}
|
||||
@@ -205,11 +208,11 @@ class NameResolver extends NodeVisitorAbstract
|
||||
* Resolve name, according to name resolver options.
|
||||
*
|
||||
* @param Name $name Function or constant name to resolve
|
||||
* @param int $type One of Stmt\Use_::TYPE_*
|
||||
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
|
||||
*
|
||||
* @return Name Resolved name, or original name with attribute
|
||||
*/
|
||||
protected function resolveName(Name $name, int $type) : Name {
|
||||
protected function resolveName(Name $name, int $type): Name {
|
||||
if (!$this->replaceNodes) {
|
||||
$resolvedName = $this->nameContext->getResolvedName($name, $type);
|
||||
if (null !== $resolvedName) {
|
||||
@@ -240,17 +243,16 @@ class NameResolver extends NodeVisitorAbstract
|
||||
return $name;
|
||||
}
|
||||
|
||||
protected function resolveClassName(Name $name) {
|
||||
protected function resolveClassName(Name $name): Name {
|
||||
return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
|
||||
}
|
||||
|
||||
protected function addNamespacedName(Node $node) {
|
||||
protected function addNamespacedName(Node $node): void {
|
||||
$node->namespacedName = Name::concat(
|
||||
$this->nameContext->getNamespace(), (string) $node->name);
|
||||
}
|
||||
|
||||
protected function resolveAttrGroups(Node $node)
|
||||
{
|
||||
protected function resolveAttrGroups(Node $node): void {
|
||||
foreach ($node->attrGroups as $attrGroup) {
|
||||
foreach ($attrGroup->attrs as $attr) {
|
||||
$attr->name = $this->resolveClassName($attr->name);
|
||||
|
||||
@@ -14,12 +14,11 @@ use PhpParser\NodeVisitorAbstract;
|
||||
* node can be accessed through <code>$node->getAttribute('previous')</code>,
|
||||
* and the next node can be accessed through <code>$node->getAttribute('next')</code>.
|
||||
*/
|
||||
final class NodeConnectingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
final class NodeConnectingVisitor extends NodeVisitorAbstract {
|
||||
/**
|
||||
* @var Node[]
|
||||
*/
|
||||
private $stack = [];
|
||||
private array $stack = [];
|
||||
|
||||
/**
|
||||
* @var ?Node
|
||||
|
||||
@@ -2,31 +2,29 @@
|
||||
|
||||
namespace PhpParser\NodeVisitor;
|
||||
|
||||
use function array_pop;
|
||||
use function count;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
use function array_pop;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* Visitor that connects a child node to its parent node.
|
||||
*
|
||||
* On the child node, the parent node can be accessed through
|
||||
* <code>$node->getAttribute('parent')</code>.
|
||||
*/
|
||||
final class ParentConnectingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
final class ParentConnectingVisitor extends NodeVisitorAbstract {
|
||||
/**
|
||||
* @var Node[]
|
||||
*/
|
||||
private $stack = [];
|
||||
private array $stack = [];
|
||||
|
||||
public function beforeTraverse(array $nodes)
|
||||
{
|
||||
public function beforeTraverse(array $nodes) {
|
||||
$this->stack = [];
|
||||
}
|
||||
|
||||
public function enterNode(Node $node)
|
||||
{
|
||||
public function enterNode(Node $node) {
|
||||
if (!empty($this->stack)) {
|
||||
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
|
||||
}
|
||||
@@ -34,8 +32,7 @@ final class ParentConnectingVisitor extends NodeVisitorAbstract
|
||||
$this->stack[] = $node;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node)
|
||||
{
|
||||
public function leaveNode(Node $node) {
|
||||
array_pop($this->stack);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user