X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCommand%2FTimeitCommand%2FTimeitVisitor.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCommand%2FTimeitCommand%2FTimeitVisitor.php;h=841ba135d8d6140cd9edd7a42ad627806416b788;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/vendor/psy/psysh/src/Command/TimeitCommand/TimeitVisitor.php b/vendor/psy/psysh/src/Command/TimeitCommand/TimeitVisitor.php new file mode 100644 index 000000000..841ba135d --- /dev/null +++ b/vendor/psy/psysh/src/Command/TimeitCommand/TimeitVisitor.php @@ -0,0 +1,139 @@ +functionDepth = 0; + } + + /** + * {@inheritdoc} + */ + public function enterNode(Node $node) + { + // keep track of nested function-like nodes, because they can have + // returns statements... and we don't want to call markEnd for those. + if ($node instanceof FunctionLike) { + $this->functionDepth++; + + return; + } + + // replace any top-level `return` statements with a `markEnd` call + if ($this->functionDepth === 0 && $node instanceof Return_) { + return new Return_($this->getEndCall($node->expr), $node->getAttributes()); + } + } + + /** + * {@inheritdoc} + */ + public function leaveNode(Node $node) + { + if ($node instanceof FunctionLike) { + $this->functionDepth--; + } + } + + /** + * {@inheritdoc} + */ + public function afterTraverse(array $nodes) + { + // prepend a `markStart` call + \array_unshift($nodes, $this->maybeExpression($this->getStartCall())); + + // append a `markEnd` call (wrapping the final node, if it's an expression) + $last = $nodes[\count($nodes) - 1]; + if ($last instanceof Expr) { + \array_pop($nodes); + $nodes[] = $this->getEndCall($last); + } elseif ($last instanceof Expression) { + \array_pop($nodes); + $nodes[] = new Expression($this->getEndCall($last->expr), $last->getAttributes()); + } elseif ($last instanceof Return_) { + // nothing to do here, we're already ending with a return call + } else { + $nodes[] = $this->maybeExpression($this->getEndCall()); + } + + return $nodes; + } + + /** + * Get PhpParser AST nodes for a `markStart` call. + * + * @return PhpParser\Node\Expr\StaticCall + */ + private function getStartCall() + { + return new StaticCall(new FullyQualifiedName('Psy\Command\TimeitCommand'), 'markStart'); + } + + /** + * Get PhpParser AST nodes for a `markEnd` call. + * + * Optionally pass in a return value. + * + * @param Expr|null $arg + * + * @return PhpParser\Node\Expr\StaticCall + */ + private function getEndCall(Expr $arg = null) + { + if ($arg === null) { + $arg = NoReturnValue::create(); + } + + return new StaticCall(new FullyQualifiedName('Psy\Command\TimeitCommand'), 'markEnd', [new Arg($arg)]); + } + + /** + * Compatibility shim for PHP Parser 3.x. + * + * Wrap $expr in a PhpParser\Node\Stmt\Expression if the class exists. + * + * @param PhpParser\Node $expr + * @param array $attrs + * + * @return PhpParser\Node\Expr|PhpParser\Node\Stmt\Expression + */ + private function maybeExpression($expr, $attrs = []) + { + return \class_exists('PhpParser\Node\Stmt\Expression') ? new Expression($expr, $attrs) : $expr; + } +}