mirror of
https://github.com/DanielnetoDotCom/YouPHPTube
synced 2025-10-06 03:50:04 +02:00
Add PHPUnit
This commit is contained in:
parent
1467777757
commit
5959dd49d7
1366 changed files with 118633 additions and 13161 deletions
|
@ -2,51 +2,40 @@
|
|||
|
||||
namespace PhpParser;
|
||||
|
||||
class NodeTraverser implements NodeTraverserInterface
|
||||
{
|
||||
class NodeTraverser implements NodeTraverserInterface {
|
||||
/**
|
||||
* If NodeVisitor::enterNode() returns DONT_TRAVERSE_CHILDREN, child nodes
|
||||
* of the current node will not be traversed for any visitors.
|
||||
*
|
||||
* For subsequent visitors enterNode() will still be called on the current
|
||||
* node and leaveNode() will also be invoked for the current node.
|
||||
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
|
||||
*/
|
||||
const DONT_TRAVERSE_CHILDREN = 1;
|
||||
public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;
|
||||
|
||||
/**
|
||||
* If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns
|
||||
* STOP_TRAVERSAL, traversal is aborted.
|
||||
*
|
||||
* The afterTraverse() method will still be invoked.
|
||||
* @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
|
||||
*/
|
||||
const STOP_TRAVERSAL = 2;
|
||||
public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;
|
||||
|
||||
/**
|
||||
* If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs
|
||||
* in an array, it will be removed from the array.
|
||||
*
|
||||
* For subsequent visitors leaveNode() will still be invoked for the
|
||||
* removed node.
|
||||
* @deprecated Use NodeVisitor::REMOVE_NODE instead.
|
||||
*/
|
||||
const REMOVE_NODE = 3;
|
||||
public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;
|
||||
|
||||
/**
|
||||
* If NodeVisitor::enterNode() returns DONT_TRAVERSE_CURRENT_AND_CHILDREN, child nodes
|
||||
* of the current node will not be traversed for any visitors.
|
||||
*
|
||||
* For subsequent visitors enterNode() will not be called as well.
|
||||
* leaveNode() will be invoked for visitors that has enterNode() method invoked.
|
||||
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
|
||||
*/
|
||||
const DONT_TRAVERSE_CURRENT_AND_CHILDREN = 4;
|
||||
public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
||||
|
||||
/** @var NodeVisitor[] Visitors */
|
||||
protected $visitors = [];
|
||||
/** @var list<NodeVisitor> Visitors */
|
||||
protected array $visitors = [];
|
||||
|
||||
/** @var bool Whether traversal should be stopped */
|
||||
protected $stopTraversal;
|
||||
protected bool $stopTraversal;
|
||||
|
||||
public function __construct() {
|
||||
// for BC
|
||||
/**
|
||||
* Create a traverser with the given visitors.
|
||||
*
|
||||
* @param NodeVisitor ...$visitors Node visitors
|
||||
*/
|
||||
public function __construct(NodeVisitor ...$visitors) {
|
||||
$this->visitors = $visitors;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,21 +43,17 @@ class NodeTraverser implements NodeTraverserInterface
|
|||
*
|
||||
* @param NodeVisitor $visitor Visitor to add
|
||||
*/
|
||||
public function addVisitor(NodeVisitor $visitor) {
|
||||
public function addVisitor(NodeVisitor $visitor): void {
|
||||
$this->visitors[] = $visitor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an added visitor.
|
||||
*
|
||||
* @param NodeVisitor $visitor
|
||||
*/
|
||||
public function removeVisitor(NodeVisitor $visitor) {
|
||||
foreach ($this->visitors as $index => $storedVisitor) {
|
||||
if ($storedVisitor === $visitor) {
|
||||
unset($this->visitors[$index]);
|
||||
break;
|
||||
}
|
||||
public function removeVisitor(NodeVisitor $visitor): void {
|
||||
$index = array_search($visitor, $this->visitors);
|
||||
if ($index !== false) {
|
||||
array_splice($this->visitors, $index, 1, []);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,7 +64,7 @@ class NodeTraverser implements NodeTraverserInterface
|
|||
*
|
||||
* @return Node[] Traversed array of nodes
|
||||
*/
|
||||
public function traverse(array $nodes) : array {
|
||||
public function traverse(array $nodes): array {
|
||||
$this->stopTraversal = false;
|
||||
|
||||
foreach ($this->visitors as $visitor) {
|
||||
|
@ -90,7 +75,8 @@ class NodeTraverser implements NodeTraverserInterface
|
|||
|
||||
$nodes = $this->traverseArray($nodes);
|
||||
|
||||
foreach ($this->visitors as $visitor) {
|
||||
for ($i = \count($this->visitors) - 1; $i >= 0; --$i) {
|
||||
$visitor = $this->visitors[$i];
|
||||
if (null !== $return = $visitor->afterTraverse($nodes)) {
|
||||
$nodes = $return;
|
||||
}
|
||||
|
@ -103,82 +89,86 @@ class NodeTraverser implements NodeTraverserInterface
|
|||
* Recursively traverse a node.
|
||||
*
|
||||
* @param Node $node Node to traverse.
|
||||
*
|
||||
* @return Node Result of traversal (may be original node or new one)
|
||||
*/
|
||||
protected function traverseNode(Node $node) : Node {
|
||||
protected function traverseNode(Node $node): void {
|
||||
foreach ($node->getSubNodeNames() as $name) {
|
||||
$subNode =& $node->$name;
|
||||
$subNode = $node->$name;
|
||||
|
||||
if (\is_array($subNode)) {
|
||||
$subNode = $this->traverseArray($subNode);
|
||||
$node->$name = $this->traverseArray($subNode);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
} elseif ($subNode instanceof Node) {
|
||||
$traverseChildren = true;
|
||||
$breakVisitorIndex = null;
|
||||
|
||||
foreach ($this->visitors as $visitorIndex => $visitor) {
|
||||
$return = $visitor->enterNode($subNode);
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($subNode, $return);
|
||||
$subNode = $return;
|
||||
} elseif (self::DONT_TRAVERSE_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
} elseif (self::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
$breakVisitorIndex = $visitorIndex;
|
||||
break;
|
||||
} elseif (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'enterNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$subNode instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$traverseChildren = true;
|
||||
$visitorIndex = -1;
|
||||
|
||||
foreach ($this->visitors as $visitorIndex => $visitor) {
|
||||
$return = $visitor->enterNode($subNode);
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($subNode, $return);
|
||||
$subNode = $node->$name = $return;
|
||||
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
break;
|
||||
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
|
||||
$node->$name = null;
|
||||
continue 2;
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'enterNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($traverseChildren) {
|
||||
$subNode = $this->traverseNode($subNode);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
if ($traverseChildren) {
|
||||
$this->traverseNode($subNode);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->visitors as $visitorIndex => $visitor) {
|
||||
$return = $visitor->leaveNode($subNode);
|
||||
for (; $visitorIndex >= 0; --$visitorIndex) {
|
||||
$visitor = $this->visitors[$visitorIndex];
|
||||
$return = $visitor->leaveNode($subNode);
|
||||
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($subNode, $return);
|
||||
$subNode = $return;
|
||||
} elseif (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (\is_array($return)) {
|
||||
throw new \LogicException(
|
||||
'leaveNode() may only return an array ' .
|
||||
'if the parent structure is an array'
|
||||
);
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'leaveNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($breakVisitorIndex === $visitorIndex) {
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($subNode, $return);
|
||||
$subNode = $node->$name = $return;
|
||||
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
|
||||
$node->$name = null;
|
||||
break;
|
||||
} elseif (\is_array($return)) {
|
||||
throw new \LogicException(
|
||||
'leaveNode() may only return an array ' .
|
||||
'if the parent structure is an array'
|
||||
);
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'leaveNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -188,78 +178,84 @@ class NodeTraverser implements NodeTraverserInterface
|
|||
*
|
||||
* @return array Result of traversal (may be original array or changed one)
|
||||
*/
|
||||
protected function traverseArray(array $nodes) : array {
|
||||
protected function traverseArray(array $nodes): array {
|
||||
$doNodes = [];
|
||||
|
||||
foreach ($nodes as $i => &$node) {
|
||||
if ($node instanceof Node) {
|
||||
$traverseChildren = true;
|
||||
$breakVisitorIndex = null;
|
||||
|
||||
foreach ($this->visitors as $visitorIndex => $visitor) {
|
||||
$return = $visitor->enterNode($node);
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($node, $return);
|
||||
$node = $return;
|
||||
} elseif (self::DONT_TRAVERSE_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
} elseif (self::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
$breakVisitorIndex = $visitorIndex;
|
||||
break;
|
||||
} elseif (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'enterNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach ($nodes as $i => $node) {
|
||||
if (!$node instanceof Node) {
|
||||
if (\is_array($node)) {
|
||||
throw new \LogicException('Invalid node structure: Contains nested arrays');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($traverseChildren) {
|
||||
$node = $this->traverseNode($node);
|
||||
if ($this->stopTraversal) {
|
||||
$traverseChildren = true;
|
||||
$visitorIndex = -1;
|
||||
|
||||
foreach ($this->visitors as $visitorIndex => $visitor) {
|
||||
$return = $visitor->enterNode($node);
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($node, $return);
|
||||
$nodes[$i] = $node = $return;
|
||||
} elseif (\is_array($return)) {
|
||||
$doNodes[] = [$i, $return];
|
||||
continue 2;
|
||||
} elseif (NodeVisitor::REMOVE_NODE === $return) {
|
||||
$doNodes[] = [$i, []];
|
||||
continue 2;
|
||||
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
|
||||
$traverseChildren = false;
|
||||
break;
|
||||
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
|
||||
throw new \LogicException(
|
||||
'REPLACE_WITH_NULL can not be used if the parent structure is an array');
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'enterNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->visitors as $visitorIndex => $visitor) {
|
||||
$return = $visitor->leaveNode($node);
|
||||
if ($traverseChildren) {
|
||||
$this->traverseNode($node);
|
||||
if ($this->stopTraversal) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($node, $return);
|
||||
$node = $return;
|
||||
} elseif (\is_array($return)) {
|
||||
$doNodes[] = [$i, $return];
|
||||
break;
|
||||
} elseif (self::REMOVE_NODE === $return) {
|
||||
$doNodes[] = [$i, []];
|
||||
break;
|
||||
} elseif (self::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (false === $return) {
|
||||
throw new \LogicException(
|
||||
'bool(false) return from leaveNode() no longer supported. ' .
|
||||
'Return NodeTraverser::REMOVE_NODE instead'
|
||||
);
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'leaveNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (; $visitorIndex >= 0; --$visitorIndex) {
|
||||
$visitor = $this->visitors[$visitorIndex];
|
||||
$return = $visitor->leaveNode($node);
|
||||
|
||||
if ($breakVisitorIndex === $visitorIndex) {
|
||||
if (null !== $return) {
|
||||
if ($return instanceof Node) {
|
||||
$this->ensureReplacementReasonable($node, $return);
|
||||
$nodes[$i] = $node = $return;
|
||||
} elseif (\is_array($return)) {
|
||||
$doNodes[] = [$i, $return];
|
||||
break;
|
||||
} elseif (NodeVisitor::REMOVE_NODE === $return) {
|
||||
$doNodes[] = [$i, []];
|
||||
break;
|
||||
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
|
||||
$this->stopTraversal = true;
|
||||
break 2;
|
||||
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
|
||||
throw new \LogicException(
|
||||
'REPLACE_WITH_NULL can not be used if the parent structure is an array');
|
||||
} else {
|
||||
throw new \LogicException(
|
||||
'leaveNode() returned invalid value of type ' . gettype($return)
|
||||
);
|
||||
}
|
||||
}
|
||||
} elseif (\is_array($node)) {
|
||||
throw new \LogicException('Invalid node structure: Contains nested arrays');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -272,7 +268,7 @@ class NodeTraverser implements NodeTraverserInterface
|
|||
return $nodes;
|
||||
}
|
||||
|
||||
private function ensureReplacementReasonable($old, $new) {
|
||||
private function ensureReplacementReasonable(Node $old, Node $new): void {
|
||||
if ($old instanceof Node\Stmt && $new instanceof Node\Expr) {
|
||||
throw new \LogicException(
|
||||
"Trying to replace statement ({$old->getType()}) " .
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue