Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extensions to add their own data to debug output #179

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/App/PrettyPHPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1121,11 +1121,27 @@
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(),
);

Check warning on line 1130 in src/App/PrettyPHPCommand.php

View check run for this annotation

Codecov / codecov/patch

src/App/PrettyPHPCommand.php#L1124-L1130

Added lines #L1124 - L1130 were not covered by tests
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();

Check warning on line 1136 in src/App/PrettyPHPCommand.php

View check run for this annotation

Codecov / codecov/patch

src/App/PrettyPHPCommand.php#L1134-L1136

Added lines #L1134 - L1136 were not covered by tests
}
$this->maybeDumpDebugOutput(
$input,
null,
$formatter->getTokens(),
$formatter->Log,
$data,
);

Check warning on line 1144 in src/App/PrettyPHPCommand.php

View check run for this annotation

Codecov / codecov/patch

src/App/PrettyPHPCommand.php#L1138-L1144

Added lines #L1138 - L1144 were not covered by tests
throw $ex;
} finally {
Profile::stopTimer($inputFile, 'file');
Expand All @@ -1138,7 +1154,15 @@
}

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) {
Expand Down Expand Up @@ -1660,7 +1684,7 @@
$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,
];
Expand All @@ -1674,7 +1698,7 @@
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);
Expand Down
8 changes: 8 additions & 0 deletions src/Concern/ExtensionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public function boot(): void {}
* @inheritDoc
*/
public function reset(): void {}

/**
* @inheritDoc
*/
public function getData(): array
{
return [];
}
}
9 changes: 9 additions & 0 deletions src/Contract/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed>
*/
public function getData(): array;
}
4 changes: 2 additions & 2 deletions src/Exception/FormatterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}
Expand Down
28 changes: 22 additions & 6 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,8 @@
null,
$this->Debug ? $this->Tokens : null,
$this->Log,
null,
$ex
$this->Debug ? $this->getExtensionData() : null,
$ex,
);
// @codeCoverageIgnoreEnd
} finally {
Expand Down Expand Up @@ -1239,8 +1239,8 @@
$out,
$this->Tokens ?? null,
$this->Log,
null,
$ex
$this->Debug ? $this->getExtensionData() : null,
$ex,
);
// @codeCoverageIgnoreEnd
} finally {
Expand All @@ -1263,7 +1263,7 @@
$out,
$this->Tokens ?? null,
$this->Log,
['before' => $before, 'after' => $after]
$this->Debug ? $this->getExtensionData() : null,
);
// @codeCoverageIgnoreEnd
}
Expand Down Expand Up @@ -1400,6 +1400,21 @@
);
}

/**
* Get extension data as an array of associative arrays
*
* @return array<class-string<Extension>,non-empty-array<string,mixed>>
*/
public function getExtensionData(): array
{
foreach ($this->Extensions ?? [] as $_ext => $ext) {
if ($_data = $ext->getData()) {
$data[$_ext] = $_data;

Check warning on line 1412 in src/Formatter.php

View check run for this annotation

Codecov / codecov/patch

src/Formatter.php#L1412

Added line #L1412 was not covered by tests
}
}
return $data ?? [];
}

/**
* @template T of Extension
*
Expand Down Expand Up @@ -1561,7 +1576,8 @@
null,
$this->Tokens,
$this->Log,
$ex
$this->getExtensionData(),
$ex,

Check warning on line 1580 in src/Formatter.php

View check run for this annotation

Codecov / codecov/patch

src/Formatter.php#L1579-L1580

Added lines #L1579 - L1580 were not covered by tests
);
} finally {
Profile::stopTimer(__METHOD__ . '#render');
Expand Down
Loading