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