-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticsCollector.php
More file actions
176 lines (163 loc) · 9.93 KB
/
Copy pathDiagnosticsCollector.php
File metadata and controls
176 lines (163 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\Diagnostics;
use Automattic\BlocksEngine\PhpTransformer\Contract\ConversionFindingContract;
use Automattic\BlocksEngine\PhpTransformer\WordPress\Runtime;
/**
* Projects already-computed conversion signals (fallbacks, runtime islands,
* static script metadata, block-validity findings, and semantic-parity
* findings) into the flat diagnostics array carried by TransformerResult.
*
* This is a behavior-preserving extraction of the inline projection that
* previously lived in HtmlTransformer::transform(). It performs no DOM work and
* no logic decisions — it only reshapes data the transformer already produced —
* so the diagnostics output is byte-identical to the inline implementation.
*/
final class DiagnosticsCollector
{
/**
* Build the success-path diagnostics array from the transformer's computed
* signals.
*
* @param string $transformerSource Source attribution for transformer-owned diagnostics (HtmlTransformer::class).
* @param array<int, array<string, mixed>> $scriptMetadata Static script metadata preserved as bounded data.
* @param array<int, array<string, mixed>> $fallbacks Fallback diagnostics emitted during conversion.
* @param array<int, array<string, mixed>> $runtimeIslands Preserved runtime islands.
* @param array<string, mixed> $blockValidityReport Block serialization validity report.
* @param array<string, mixed> $semanticParityReport Semantic parity report.
* @param array<string, mixed> $contentRoundTripReport Content round-trip (hallucination) report.
* @return array<int, array<string, mixed>>
*/
public function collect(
string $transformerSource,
array $scriptMetadata,
array $fallbacks,
array $runtimeIslands,
array $blockValidityReport,
array $semanticParityReport,
array $contentRoundTripReport = array()
): array {
$diagnostics = array(
array(
'code' => 'html_to_blocks_core_slice',
'message' => 'Converted supported core text, layout, media, gallery, embed, file, table, button, shortcode, spacer, definition-list, details, navigation, safe inline SVG images, and wrapper elements; unsupported elements are reported as fallbacks.',
'source' => $transformerSource,
),
);
foreach ( $scriptMetadata as $metadata ) {
$diagnostics[] = array(
'code' => 'html_static_script_metadata',
'message' => 'Static script data was preserved as bounded metadata and does not require client script execution.',
'source' => $transformerSource,
'reason' => 'script_static_metadata',
'tag' => 'script',
'selector' => $metadata['selector'] ?? null,
'script_role' => $metadata['script_role'] ?? null,
);
}
foreach ( $fallbacks as $fallback ) {
if ( ! empty($fallback['diagnostic_code']) ) {
$diagnostics[] = array(
'code' => $fallback['diagnostic_code'],
'message' => $fallback['message'] ?? 'HTML element preserved as fallback metadata.',
'source' => $transformerSource,
'reason' => $fallback['reason'] ?? null,
'severity' => $fallback['severity'] ?? null,
'conversion_classification' => $fallback['conversion_classification'] ?? null,
'loss_class' => $fallback['loss_class'] ?? null,
'diagnostic_class' => $fallback['diagnostic_class'] ?? null,
'preservation_strategy' => $fallback['preservation_strategy'] ?? null,
'runtime_requirement' => $fallback['runtime_requirement'] ?? null,
'recoverability' => $fallback['recoverability'] ?? null,
'actionability' => $fallback['actionability'] ?? null,
'suggested_repair_class' => $fallback['suggested_repair_class'] ?? null,
'suggested_primitive' => $fallback['suggested_primitive'] ?? null,
'materialization_hint' => $fallback['materialization_hint'] ?? null,
'tag' => $fallback['tag'] ?? null,
'selector' => $fallback['selector'] ?? null,
'pattern_family' => $fallback['pattern_family'] ?? null,
'pattern_family_detail' => $fallback['pattern_family_detail'] ?? null,
'source_selector' => $fallback['source_selector'] ?? null,
'source_selector_specificity' => $fallback['source_selector_specificity'] ?? null,
'parent_reason' => $fallback['parent_reason'] ?? null,
'ancestor_reason' => $fallback['ancestor_reason'] ?? null,
'suggested_generic_repair_class' => $fallback['suggested_generic_repair_class'] ?? null,
);
}
}
foreach ( $runtimeIslands as $island ) {
$diagnostics[] = array_filter(array(
'code' => 'preserved_runtime_island',
'message' => 'Runtime-dependent source markup was preserved as a bounded runtime island.',
'source' => $transformerSource,
'severity' => 'info',
'conversion_classification' => 'runtime_island_preserved',
'loss_class' => 'runtime_island_preserved',
'diagnostic_class' => 'runtime_island_preserved',
'suggested_repair_class' => 'preserve_runtime_island',
'preservation_strategy' => $island['preservation_strategy'] ?? 'bounded_raw_html_runtime_island',
'disposition' => $island['disposition'] ?? null,
'preservation_status' => $island['preservation_status'] ?? null,
'js_handling' => $island['js_handling'] ?? null,
'runtime_requirement' => $island['runtime_requirement'] ?? null,
'kind' => $island['kind'] ?? null,
'reason' => $island['preservation_reason'] ?? null,
'tag' => $island['tag'] ?? null,
'selector' => $island['selector'] ?? null,
'pattern_family' => $island['pattern_family'] ?? null,
'pattern_family_detail' => $island['pattern_family_detail'] ?? null,
'source_selector' => $island['source_selector'] ?? null,
'source_selector_specificity' => $island['source_selector_specificity'] ?? null,
'parent_reason' => $island['parent_reason'] ?? null,
'ancestor_reason' => $island['ancestor_reason'] ?? null,
'suggested_generic_repair_class' => $island['suggested_generic_repair_class'] ?? null,
), static fn (mixed $value): bool => null !== $value && '' !== $value);
}
foreach ( $blockValidityReport['findings'] ?? array() as $finding ) {
if ( ! is_array($finding) ) {
continue;
}
$diagnostics[] = array(
'code' => 'wp_block_validity_' . (string) ($finding['code'] ?? 'warning'),
'message' => (string) ($finding['summary'] ?? 'Generated block serialization may trigger WordPress block invalidity warnings.'),
'source' => Runtime::class,
'severity' => $finding['severity'] ?? 'warning',
'block_name' => $finding['block_name'] ?? null,
'path' => $finding['path'] ?? null,
);
}
foreach ( $semanticParityReport['findings'] ?? array() as $finding ) {
if ( ! is_array($finding) ) {
continue;
}
$diagnostics[] = array(
'code' => 'html_semantic_parity_' . (string) ($finding['code'] ?? 'warning'),
'message' => (string) ($finding['summary'] ?? 'Generated blocks differ from source semantic structure.'),
'source' => $transformerSource,
'severity' => $finding['severity'] ?? 'warning',
'selector' => $finding['selector'] ?? null,
);
}
foreach ( $contentRoundTripReport['findings'] ?? array() as $finding ) {
if ( ! is_array($finding) ) {
continue;
}
$diagnostics[] = array(
'code' => 'html_content_round_trip_' . (string) ($finding['code'] ?? 'warning'),
'message' => (string) ($finding['summary'] ?? 'Generated block text does not appear in the source content.'),
'source' => $transformerSource,
'severity' => $finding['severity'] ?? 'warning',
'text' => $finding['text'] ?? null,
);
}
// Stamp the canonical classification triplet on every flat diagnostic so
// the projection's block-validity and semantic-parity findings (which
// otherwise carry no repair/family signal) and the carried-through
// fallback/runtime-island findings all cluster by root cause downstream
// instead of falling into a generic catch-all.
return array_map(
static fn (array $finding): array => ConversionFindingContract::withClassification($finding),
$diagnostics
);
}
}