+ $total = \array_sum($times);
+ \rsort($times);
+ $median = $times[\round($num / 2)];
+
+ $output->writeln(\sprintf(self::AVG_RESULT_MSG, $total / $num, $median, $total));
+ }
+ }
+
+ /**
+ * Internal method for marking the start of timeit execution.
+ *
+ * A static call to this method will be injected at the start of the timeit
+ * input code to instrument the call. We will use the saved start time to
+ * more accurately calculate time elapsed during execution.
+ */
+ public static function markStart()
+ {
+ self::$start = \microtime(true);
+ }
+
+ /**
+ * Internal method for marking the end of timeit execution.
+ *
+ * A static call to this method is injected by TimeitVisitor at the end
+ * of the timeit input code to instrument the call.
+ *
+ * Note that this accepts an optional $ret parameter, which is used to pass
+ * the return value of the last statement back out of timeit. This saves us
+ * a bunch of code rewriting shenanigans.
+ *
+ * @param mixed $ret
+ *
+ * @return mixed it just passes $ret right back
+ */
+ public static function markEnd($ret = null)
+ {
+ self::$times[] = \microtime(true) - self::$start;
+ self::$start = null;
+
+ return $ret;
+ }
+
+ /**
+ * Ensure that the end of code execution was marked.
+ *
+ * The end *should* be marked in the instrumented code, but just in case
+ * we'll add a fallback here.
+ */
+ private function ensureEndMarked()
+ {
+ if (self::$start !== null) {
+ self::markEnd();
+ }
+ }
+
+ /**
+ * Instrument code for timeit execution.
+ *
+ * This inserts `markStart` and `markEnd` calls to ensure that (reasonably)
+ * accurate times are recorded for just the code being executed.
+ *
+ * @param string $code
+ *
+ * @return string
+ */
+ private function instrumentCode($code)
+ {
+ return $this->printer->prettyPrint($this->traverser->traverse($this->parse($code)));
+ }
+
+ /**
+ * Lex and parse a string of code into statements.
+ *
+ * @param string $code
+ *
+ * @return array Statements
+ */
+ private function parse($code)
+ {
+ $code = '<?php ' . $code;
+
+ try {
+ return $this->parser->parse($code);
+ } catch (\PhpParser\Error $e) {
+ if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
+ throw $e;
+ }