From e049d8983bdff7667ca462da4d86a757bc8f8b85 Mon Sep 17 00:00:00 2001 From: Luke Arms Date: Sun, 29 Dec 2024 15:20:43 +1100 Subject: [PATCH 1/2] Add `Extension::getData()`, `Formatter::getExtensionData()`, etc. --- src/App/PrettyPHPCommand.php | 30 +++++++++++++++++++++++++++--- src/Concern/ExtensionTrait.php | 8 ++++++++ src/Contract/Extension.php | 9 +++++++++ src/Formatter.php | 28 ++++++++++++++++++++++------ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/App/PrettyPHPCommand.php b/src/App/PrettyPHPCommand.php index adf3d41c..b1c8c1ce 100644 --- a/src/App/PrettyPHPCommand.php +++ b/src/App/PrettyPHPCommand.php @@ -1121,11 +1121,27 @@ protected function run(...$params) continue; } catch (FormatterException $ex) { Console::error('Unable to format:', $inputFile); - $this->maybeDumpDebugOutput($input, $ex->getOutput(), $ex->getTokens(), $ex->getLog(), $ex->getData()); + $this->maybeDumpDebugOutput( + $input, + $ex->getOutput(), + $ex->getTokens(), + $ex->getLog(), + $ex->getData(), + ); throw $ex; } catch (Throwable $ex) { Console::error('Unable to format:', $inputFile); - $this->maybeDumpDebugOutput($input, null, $formatter->getTokens(), $formatter->Log, (string) $ex); + $data = [get_class($ex) => (string) $ex]; + if ($this->Debug) { + $data += $formatter->getExtensionData(); + } + $this->maybeDumpDebugOutput( + $input, + null, + $formatter->getTokens(), + $formatter->Log, + $data, + ); throw $ex; } finally { Profile::stopTimer($inputFile, 'file'); @@ -1138,7 +1154,15 @@ protected function run(...$params) } if ($i === $count) { - $this->maybeDumpDebugOutput($input, $output, $formatter->getTokens(), $formatter->Log, null); + $this->maybeDumpDebugOutput( + $input, + $output, + $formatter->getTokens(), + $formatter->Log, + $this->Debug + ? $formatter->getExtensionData() + : null, + ); } if ($this->Diff !== null || $this->Check) { diff --git a/src/Concern/ExtensionTrait.php b/src/Concern/ExtensionTrait.php index 5a8b2c33..91395649 100644 --- a/src/Concern/ExtensionTrait.php +++ b/src/Concern/ExtensionTrait.php @@ -31,4 +31,12 @@ public function boot(): void {} * @inheritDoc */ public function reset(): void {} + + /** + * @inheritDoc + */ + public function getData(): array + { + return []; + } } diff --git a/src/Contract/Extension.php b/src/Contract/Extension.php index 335d3226..03e205fb 100644 --- a/src/Contract/Extension.php +++ b/src/Contract/Extension.php @@ -25,4 +25,13 @@ public function boot(): void; * Called once per document, before formatting begins. */ public function reset(): void; + + /** + * Get extension data as an associative array + * + * Called when generating debug output. + * + * @return array + */ + public function getData(): array; } diff --git a/src/Formatter.php b/src/Formatter.php index 0b6bb6d6..1933e8b6 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -1202,8 +1202,8 @@ public function format( null, $this->Debug ? $this->Tokens : null, $this->Log, - null, - $ex + $this->Debug ? $this->getExtensionData() : null, + $ex, ); // @codeCoverageIgnoreEnd } finally { @@ -1239,8 +1239,8 @@ public function format( $out, $this->Tokens ?? null, $this->Log, - null, - $ex + $this->Debug ? $this->getExtensionData() : null, + $ex, ); // @codeCoverageIgnoreEnd } finally { @@ -1263,7 +1263,7 @@ public function format( $out, $this->Tokens ?? null, $this->Log, - ['before' => $before, 'after' => $after] + $this->Debug ? $this->getExtensionData() : null, ); // @codeCoverageIgnoreEnd } @@ -1400,6 +1400,21 @@ public function registerProblem( ); } + /** + * Get extension data as an array of associative arrays + * + * @return array,non-empty-array> + */ + public function getExtensionData(): array + { + foreach ($this->Extensions ?? [] as $_ext => $ext) { + if ($_data = $ext->getData()) { + $data[$_ext] = $_data; + } + } + return $data ?? []; + } + /** * @template T of Extension * @@ -1561,7 +1576,8 @@ private function logProgress(string $rule, string $after): void null, $this->Tokens, $this->Log, - $ex + $this->getExtensionData(), + $ex, ); } finally { Profile::stopTimer(__METHOD__ . '#render'); From 51119731b4060481612619842c77344303a7a04b Mon Sep 17 00:00:00 2001 From: Luke Arms Date: Mon, 30 Dec 2024 10:45:24 +1100 Subject: [PATCH 2/2] Improve readability of serialized debug output --- src/App/PrettyPHPCommand.php | 4 ++-- src/Exception/FormatterException.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/App/PrettyPHPCommand.php b/src/App/PrettyPHPCommand.php index b1c8c1ce..39f0ca7a 100644 --- a/src/App/PrettyPHPCommand.php +++ b/src/App/PrettyPHPCommand.php @@ -1684,7 +1684,7 @@ private function maybeDumpDebugOutput( $files += [ 'input.php' => $input, 'output.php' => $output, - 'tokens.json' => $tokens, + 'tokens.json' => $tokens !== null ? (object) $tokens : null, 'data.out' => is_string($data) ? $data : null, 'data.json' => is_string($data) ? null : $data, ]; @@ -1698,7 +1698,7 @@ private function maybeDumpDebugOutput( if (!is_string($out)) { $out = Json::prettyPrint( $out, - \JSON_FORCE_OBJECT | \JSON_INVALID_UTF8_IGNORE + \JSON_INVALID_UTF8_IGNORE ) . \PHP_EOL; } File::writeContents($file, $out); diff --git a/src/Exception/FormatterException.php b/src/Exception/FormatterException.php index 3bfb0973..50b7c715 100644 --- a/src/Exception/FormatterException.php +++ b/src/Exception/FormatterException.php @@ -49,11 +49,11 @@ public function __construct( */ public function getMetadata(): array { - $flags = \JSON_FORCE_OBJECT | \JSON_INVALID_UTF8_IGNORE; + $flags = \JSON_INVALID_UTF8_IGNORE; return [ 'output' => $this->Output, - 'tokens' => Json::prettyPrint($this->Tokens, $flags), + 'tokens' => Json::prettyPrint($this->Tokens !== null ? (object) $this->Tokens : null, $flags), 'data' => Json::prettyPrint($this->Data, $flags), ]; }