NodeVisitorForTesting.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace PhpParser;
  3. class NodeVisitorForTesting implements NodeVisitor {
  4. public $trace = [];
  5. private $returns;
  6. private $returnsPos;
  7. public function __construct(array $returns = []) {
  8. $this->returns = $returns;
  9. $this->returnsPos = 0;
  10. }
  11. public function beforeTraverse(array $nodes) {
  12. return $this->traceEvent('beforeTraverse', $nodes);
  13. }
  14. public function enterNode(Node $node) {
  15. return $this->traceEvent('enterNode', $node);
  16. }
  17. public function leaveNode(Node $node) {
  18. return $this->traceEvent('leaveNode', $node);
  19. }
  20. public function afterTraverse(array $nodes) {
  21. return $this->traceEvent('afterTraverse', $nodes);
  22. }
  23. private function traceEvent(string $method, $param) {
  24. $this->trace[] = [$method, $param];
  25. if ($this->returnsPos < count($this->returns)) {
  26. $currentReturn = $this->returns[$this->returnsPos];
  27. if ($currentReturn[0] === $method && $currentReturn[1] === $param) {
  28. $this->returnsPos++;
  29. return $currentReturn[2];
  30. }
  31. }
  32. return null;
  33. }
  34. public function __destruct() {
  35. if ($this->returnsPos !== count($this->returns)) {
  36. throw new \Exception("Expected event did not occur");
  37. }
  38. }
  39. }