4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command\TimeitCommand;
15 use PhpParser\Node\Arg;
16 use PhpParser\Node\Expr;
17 use PhpParser\Node\Expr\StaticCall;
18 use PhpParser\Node\FunctionLike;
19 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
20 use PhpParser\Node\Stmt\Expression;
21 use PhpParser\Node\Stmt\Return_;
22 use PhpParser\NodeVisitorAbstract;
23 use Psy\CodeCleaner\NoReturnValue;
26 * A node visitor for instrumenting code to be executed by the `timeit` command.
28 * Injects `TimeitCommand::markStart()` at the start of code to be executed, and
29 * `TimeitCommand::markEnd()` at the end, and on top-level return statements.
31 class TimeitVisitor extends NodeVisitorAbstract
33 private $functionDepth;
38 public function beforeTraverse(array $nodes)
40 $this->functionDepth = 0;
46 public function enterNode(Node $node)
48 // keep track of nested function-like nodes, because they can have
49 // returns statements... and we don't want to call markEnd for those.
50 if ($node instanceof FunctionLike) {
51 $this->functionDepth++;
56 // replace any top-level `return` statements with a `markEnd` call
57 if ($this->functionDepth === 0 && $node instanceof Return_) {
58 return new Return_($this->getEndCall($node->expr), $node->getAttributes());
65 public function leaveNode(Node $node)
67 if ($node instanceof FunctionLike) {
68 $this->functionDepth--;
75 public function afterTraverse(array $nodes)
77 // prepend a `markStart` call
78 \array_unshift($nodes, $this->maybeExpression($this->getStartCall()));
80 // append a `markEnd` call (wrapping the final node, if it's an expression)
81 $last = $nodes[\count($nodes) - 1];
82 if ($last instanceof Expr) {
84 $nodes[] = $this->getEndCall($last);
85 } elseif ($last instanceof Expression) {
87 $nodes[] = new Expression($this->getEndCall($last->expr), $last->getAttributes());
88 } elseif ($last instanceof Return_) {
89 // nothing to do here, we're already ending with a return call
91 $nodes[] = $this->maybeExpression($this->getEndCall());
98 * Get PhpParser AST nodes for a `markStart` call.
100 * @return PhpParser\Node\Expr\StaticCall
102 private function getStartCall()
104 return new StaticCall(new FullyQualifiedName('Psy\Command\TimeitCommand'), 'markStart');
108 * Get PhpParser AST nodes for a `markEnd` call.
110 * Optionally pass in a return value.
112 * @param Expr|null $arg
114 * @return PhpParser\Node\Expr\StaticCall
116 private function getEndCall(Expr $arg = null)
119 $arg = NoReturnValue::create();
122 return new StaticCall(new FullyQualifiedName('Psy\Command\TimeitCommand'), 'markEnd', [new Arg($arg)]);
126 * Compatibility shim for PHP Parser 3.x.
128 * Wrap $expr in a PhpParser\Node\Stmt\Expression if the class exists.
130 * @param PhpParser\Node $expr
131 * @param array $attrs
133 * @return PhpParser\Node\Expr|PhpParser\Node\Stmt\Expression
135 private function maybeExpression($expr, $attrs = [])
137 return \class_exists('PhpParser\Node\Stmt\Expression') ? new Expression($expr, $attrs) : $expr;