1212 *
1313 * Measures render time and extracts cache configuration with strict type safety (PHPStan Level 8).
1414 *
15+ * @phpstan-type CacheInfo array{
16+ * cacheable: bool,
17+ * lifetime: int|null,
18+ * cacheKey: string,
19+ * cacheTags: array<int, string>,
20+ * pageCacheable: bool
21+ * }
22+ * @phpstan-type RenderMetrics array{
23+ * renderTimeMs: float,
24+ * startTime: int,
25+ * endTime: int
26+ * }
27+ * @phpstan-type PerformanceExport array{
28+ * renderTime: string,
29+ * timestamp: int
30+ * }
31+ * @phpstan-type CacheExport array{
32+ * cacheable: bool,
33+ * lifetime: int|null,
34+ * key: string,
35+ * tags: array<int, string>,
36+ * pageCacheable: bool
37+ * }
38+ * @phpstan-type FormattedMetrics array{
39+ * performance: PerformanceExport,
40+ * cache: CacheExport
41+ * }
1542 */
1643class BlockCacheCollector
1744{
@@ -22,14 +49,16 @@ public function __construct(
2249 private readonly LayoutInterface $ layout
2350 ) {
2451 }
52+
2553 /**
2654 * Get cache information from block
2755 *
2856 * Safely extracts cache lifetime, key, and tags with explicit type checking
2957 * to satisfy PHPStan Level 8 requirements.
3058 *
3159 * @param BlockInterface $block
32- * @return array{cacheable: bool, lifetime: int|null, cacheKey: string, cacheTags: array<int, string>, pageCacheable: bool}
60+ * @return array<string, mixed>
61+ * @phpstan-return CacheInfo
3362 */
3463 public function getCacheInfo (BlockInterface $ block ): array
3564 {
@@ -167,8 +196,8 @@ private function resolveCacheTags(BlockInterface $block): array
167196 /**
168197 * Check if current page is cacheable
169198 *
170- * Checks layout configuration to determine if page has cacheable="false" attribute.
171- * If ANY block on the page is marked as non-cacheable in layout XML, the entire page is non-cacheable.
199+ * Checks layout configuration to determine if page has cacheable="false" attribute.
200+ * If any block on the page is marked as non-cacheable in layout XML, the page is non-cacheable.
172201 *
173202 * @return bool True if page is cacheable, false otherwise
174203 */
@@ -207,16 +236,22 @@ private function isPageCacheable(): bool
207236 /**
208237 * Format metrics for JSON export to frontend
209238 *
210- * @param array{renderTimeMs: float, startTime: int, endTime: int} $renderMetrics
211- * @param array{cacheable: bool, lifetime: int|null, cacheKey: string, cacheTags: array<int, string>, pageCacheable: bool} $cacheMetrics
212- * @return array{performance: array{renderTime: string, timestamp: int}, cache: array{cacheable: bool, lifetime: int|null, key: string, tags: array<int, string>, pageCacheable: bool}}
239+ * @param array<string, mixed> $renderMetrics
240+ * @param array<string, mixed> $cacheMetrics
241+ * @return array<string, mixed>
242+ * @phpstan-param RenderMetrics $renderMetrics
243+ * @phpstan-param CacheInfo $cacheMetrics
244+ * @phpstan-return FormattedMetrics
213245 */
214246 public function formatMetricsForJson (array $ renderMetrics , array $ cacheMetrics ): array
215247 {
248+ $ timestamp = (int ) ($ renderMetrics ['startTime ' ] / 1_000_000_000 );
249+
216250 return [
217251 'performance ' => [
218252 'renderTime ' => number_format ($ renderMetrics ['renderTimeMs ' ], 2 ),
219- 'timestamp ' => (int )($ renderMetrics ['startTime ' ] / 1_000_000_000 ), // Convert ns to seconds
253+ // Convert ns to seconds.
254+ 'timestamp ' => $ timestamp ,
220255 ],
221256 'cache ' => [
222257 'cacheable ' => $ cacheMetrics ['cacheable ' ],
0 commit comments