diff --git a/src/PhpDocument.php b/src/PhpDocument.php index b2bae32..6049f0d 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -69,15 +69,20 @@ class PhpDocument public function parse() { $stmts = null; + $errors = []; try { $stmts = $this->parser->parse($this->content); } - catch(Error $e) { - // Parser still throws errors. e.g for unterminated comments + catch(\PhpParser\Error $e) { + // Lexer can throw errors. e.g for unterminated comments + // unfortunately we don't get a location back + $errors[] = $e; } + $errors = array_merge($this->parser->getErrors(), $errors); + $diagnostics = []; - foreach ($this->parser->getErrors() as $error) { + foreach ($errors as $error) { $diagnostic = new Diagnostic(); $diagnostic->range = new Range( new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($this->content) - 1 : 0), diff --git a/src/StopWatch.php b/src/StopWatch.php new file mode 100644 index 0000000..09e1099 --- /dev/null +++ b/src/StopWatch.php @@ -0,0 +1,96 @@ + self::$max[$name]) { + self::$max[$name] = $time; + self::$maxlabels[$name] = $label; + } + unset(self::$starts[$name]); + } + + public function getTimings(): string + { + $total = 0; + foreach(self::$timings as $timing) { + $total += $timing; + } + + $texts = ['', '--- StopWatch timings ----------------']; + foreach(self::$timings as $name => $time) { + $texts[] = $name . '*' . self::$counters[$name] + . ': tot. ' . self::formatTime($time) + . ', ' . self::formatPercent($time / $total) + . ', avg. '. self::formatTime($time / self::$counters[$name]) + . ', min. '. self::formatTime(self::$min[$name]) + . ', max. '. self::formatTime(self::$max[$name]) + . (empty(self::$maxlabels[$name]) ? '' : '(' . self::$maxlabels[$name] . ')'); + } + $texts[] = '--------------------------------------'; + return implode("\n", $texts); + } + + private function formatTime(float $seconds): string + { + if ($seconds >= 60) { + return (int)($seconds / 60).'min'; + } + else if ($seconds >= 1) { + return (int)($seconds).'s'; + } + else if ($seconds >= 0.001) { + return (int)($seconds * 1000).'ms'; + } + else if ($seconds >= 0.000001) { + return (int)($seconds * 1000000).'us'; + } + else { + return '0'; + } + } + + private function formatPercent(float $fraction): string + { + return number_format($fraction * 100, 1) . '%'; + } +} \ No newline at end of file