annotate vendor/psy/psysh/src/Command/TimeitCommand/TimeitVisitor.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command\TimeitCommand;
Chris@0 13
Chris@0 14 use PhpParser\Node;
Chris@0 15 use PhpParser\Node\Arg;
Chris@0 16 use PhpParser\Node\Expr;
Chris@0 17 use PhpParser\Node\Expr\StaticCall;
Chris@0 18 use PhpParser\Node\FunctionLike;
Chris@0 19 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
Chris@0 20 use PhpParser\Node\Stmt\Expression;
Chris@0 21 use PhpParser\Node\Stmt\Return_;
Chris@0 22 use PhpParser\NodeVisitorAbstract;
Chris@0 23 use Psy\CodeCleaner\NoReturnValue;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * A node visitor for instrumenting code to be executed by the `timeit` command.
Chris@0 27 *
Chris@0 28 * Injects `TimeitCommand::markStart()` at the start of code to be executed, and
Chris@0 29 * `TimeitCommand::markEnd()` at the end, and on top-level return statements.
Chris@0 30 */
Chris@0 31 class TimeitVisitor extends NodeVisitorAbstract
Chris@0 32 {
Chris@0 33 private $functionDepth;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function beforeTraverse(array $nodes)
Chris@0 39 {
Chris@0 40 $this->functionDepth = 0;
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function enterNode(Node $node)
Chris@0 47 {
Chris@0 48 // keep track of nested function-like nodes, because they can have
Chris@0 49 // returns statements... and we don't want to call markEnd for those.
Chris@0 50 if ($node instanceof FunctionLike) {
Chris@0 51 $this->functionDepth++;
Chris@0 52
Chris@0 53 return;
Chris@0 54 }
Chris@0 55
Chris@0 56 // replace any top-level `return` statements with a `markEnd` call
Chris@0 57 if ($this->functionDepth === 0 && $node instanceof Return_) {
Chris@0 58 return new Return_($this->getEndCall($node->expr), $node->getAttributes());
Chris@0 59 }
Chris@0 60 }
Chris@0 61
Chris@0 62 /**
Chris@0 63 * {@inheritdoc}
Chris@0 64 */
Chris@0 65 public function leaveNode(Node $node)
Chris@0 66 {
Chris@0 67 if ($node instanceof FunctionLike) {
Chris@0 68 $this->functionDepth--;
Chris@0 69 }
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public function afterTraverse(array $nodes)
Chris@0 76 {
Chris@0 77 // prepend a `markStart` call
Chris@4 78 \array_unshift($nodes, $this->maybeExpression($this->getStartCall()));
Chris@0 79
Chris@0 80 // append a `markEnd` call (wrapping the final node, if it's an expression)
Chris@4 81 $last = $nodes[\count($nodes) - 1];
Chris@0 82 if ($last instanceof Expr) {
Chris@4 83 \array_pop($nodes);
Chris@0 84 $nodes[] = $this->getEndCall($last);
Chris@0 85 } elseif ($last instanceof Expression) {
Chris@4 86 \array_pop($nodes);
Chris@0 87 $nodes[] = new Expression($this->getEndCall($last->expr), $last->getAttributes());
Chris@0 88 } elseif ($last instanceof Return_) {
Chris@0 89 // nothing to do here, we're already ending with a return call
Chris@0 90 } else {
Chris@0 91 $nodes[] = $this->maybeExpression($this->getEndCall());
Chris@0 92 }
Chris@0 93
Chris@0 94 return $nodes;
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Get PhpParser AST nodes for a `markStart` call.
Chris@0 99 *
Chris@0 100 * @return PhpParser\Node\Expr\StaticCall
Chris@0 101 */
Chris@0 102 private function getStartCall()
Chris@0 103 {
Chris@0 104 return new StaticCall(new FullyQualifiedName('Psy\Command\TimeitCommand'), 'markStart');
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Get PhpParser AST nodes for a `markEnd` call.
Chris@0 109 *
Chris@0 110 * Optionally pass in a return value.
Chris@0 111 *
Chris@0 112 * @param Expr|null $arg
Chris@0 113 *
Chris@0 114 * @return PhpParser\Node\Expr\StaticCall
Chris@0 115 */
Chris@0 116 private function getEndCall(Expr $arg = null)
Chris@0 117 {
Chris@0 118 if ($arg === null) {
Chris@0 119 $arg = NoReturnValue::create();
Chris@0 120 }
Chris@0 121
Chris@0 122 return new StaticCall(new FullyQualifiedName('Psy\Command\TimeitCommand'), 'markEnd', [new Arg($arg)]);
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Compatibility shim for PHP Parser 3.x.
Chris@0 127 *
Chris@0 128 * Wrap $expr in a PhpParser\Node\Stmt\Expression if the class exists.
Chris@0 129 *
Chris@0 130 * @param PhpParser\Node $expr
Chris@0 131 * @param array $attrs
Chris@0 132 *
Chris@0 133 * @return PhpParser\Node\Expr|PhpParser\Node\Stmt\Expression
Chris@0 134 */
Chris@0 135 private function maybeExpression($expr, $attrs = [])
Chris@0 136 {
Chris@4 137 return \class_exists('PhpParser\Node\Stmt\Expression') ? new Expression($expr, $attrs) : $expr;
Chris@0 138 }
Chris@0 139 }