@@ -174,16 +174,12 @@ function perflab_aea_enqueued_blocking_assets_test(): array {
174
174
175
175
$ result ['description ' ] .= perflab_aea_generate_blocking_assets_table ( $ audit_result ['assets ' ] );
176
176
177
- if ( isset ( $ scripts_result ) ) {
178
- $ result ['description ' ] .= $ scripts_result ['description ' ];
179
- }
180
- if ( isset ( $ styles_result ) ) {
181
- $ result ['description ' ] .= $ styles_result ['description ' ];
182
- }
177
+ $ result ['description ' ] .= $ scripts_result ['description ' ];
178
+ $ result ['description ' ] .= $ styles_result ['description ' ];
183
179
184
180
if (
185
- ( isset ( $ scripts_result ) && 'good ' !== $ scripts_result ['status ' ] ) ||
186
- ( isset ( $ styles_result ) && 'good ' !== $ styles_result ['status ' ] )
181
+ 'good ' !== $ scripts_result ['status ' ] ||
182
+ 'good ' !== $ styles_result ['status ' ]
187
183
) {
188
184
$ result ['status ' ] = 'recommended ' ;
189
185
$ result ['actions ' ] = sprintf (
@@ -217,14 +213,17 @@ function perflab_aea_enqueued_ajax_blocking_assets_test(): void {
217
213
* } $blocking_assets
218
214
*
219
215
* @param array<string, mixed> $blocking_assets Array of blocking assets.
220
- * @return array{status: 'good'|'recommended', description: string}|null Result.
216
+ * @return array{status: 'good'|'recommended', description: string} Result.
221
217
*/
222
- function perflab_aea_enqueued_blocking_scripts ( array $ blocking_assets ): ?array {
223
- $ enqueued_scripts = perflab_aea_get_total_enqueued_assets ( $ blocking_assets , 'scripts ' );
224
- $ bytes_enqueued = perflab_aea_get_total_size_bytes_enqueued_assets ( $ blocking_assets , 'scripts ' );
225
- if ( null === $ enqueued_scripts || null === $ bytes_enqueued ) {
226
- return null ;
227
- }
218
+ function perflab_aea_enqueued_blocking_scripts ( array $ blocking_assets ): array {
219
+ $ enqueued_scripts = count ( $ blocking_assets ['scripts ' ] );
220
+ $ bytes_enqueued = array_reduce (
221
+ $ blocking_assets ['scripts ' ],
222
+ static function ( $ carry , $ asset ): int {
223
+ return $ carry + ( $ asset ['size ' ] ?? 0 );
224
+ },
225
+ 0
226
+ );
228
227
229
228
$ result = array (
230
229
'status ' => 'good ' ,
@@ -286,13 +285,10 @@ function perflab_aea_enqueued_blocking_scripts( array $blocking_assets ): ?array
286
285
}
287
286
288
287
// If one of the assets had an error, then fail the test even if under the threshold.
289
- $ scripts = perflab_aea_get_blocking_assets ( $ blocking_assets , 'scripts ' );
290
- if ( null !== $ scripts ) {
291
- foreach ( $ scripts as $ script ) {
292
- if ( is_wp_error ( $ script ['error ' ] ) ) {
293
- $ result ['status ' ] = 'recommended ' ;
294
- break ;
295
- }
288
+ foreach ( $ blocking_assets ['scripts ' ] as $ script ) {
289
+ if ( is_wp_error ( $ script ['error ' ] ) ) {
290
+ $ result ['status ' ] = 'recommended ' ;
291
+ break ;
296
292
}
297
293
}
298
294
@@ -310,15 +306,17 @@ function perflab_aea_enqueued_blocking_scripts( array $blocking_assets ): ?array
310
306
* } $blocking_assets
311
307
*
312
308
* @param array<string, mixed> $blocking_assets Array of blocking assets.
313
- * @return array{status: string, description: string}|null Result.
309
+ * @return array{status: string, description: string} Result.
314
310
*/
315
- function perflab_aea_enqueued_blocking_styles ( array $ blocking_assets ): ?array {
316
- // Omit if the test didn't run yet, omit.
317
- $ enqueued_styles = perflab_aea_get_total_enqueued_assets ( $ blocking_assets , 'styles ' );
318
- $ bytes_enqueued = perflab_aea_get_total_size_bytes_enqueued_assets ( $ blocking_assets , 'styles ' );
319
- if ( null === $ enqueued_styles || null === $ bytes_enqueued ) {
320
- return null ;
321
- }
311
+ function perflab_aea_enqueued_blocking_styles ( array $ blocking_assets ): array {
312
+ $ enqueued_styles = count ( $ blocking_assets ['styles ' ] );
313
+ $ bytes_enqueued = array_reduce (
314
+ $ blocking_assets ['styles ' ],
315
+ static function ( $ carry , $ asset ): int {
316
+ return $ carry + ( $ asset ['size ' ] ?? 0 );
317
+ },
318
+ 0
319
+ );
322
320
323
321
$ result = array (
324
322
'status ' => 'good ' ,
@@ -380,13 +378,10 @@ function perflab_aea_enqueued_blocking_styles( array $blocking_assets ): ?array
380
378
}
381
379
382
380
// If one of the assets had an error, then fail the test even if under the threshold.
383
- $ styles = perflab_aea_get_blocking_assets ( $ blocking_assets , 'styles ' );
384
- if ( null !== $ styles ) {
385
- foreach ( $ styles as $ style ) {
386
- if ( is_wp_error ( $ style ['error ' ] ) ) {
387
- $ result ['status ' ] = 'recommended ' ;
388
- break ;
389
- }
381
+ foreach ( $ blocking_assets ['styles ' ] as $ style ) {
382
+ if ( is_wp_error ( $ style ['error ' ] ) ) {
383
+ $ result ['status ' ] = 'recommended ' ;
384
+ break ;
390
385
}
391
386
}
392
387
@@ -471,101 +466,6 @@ function perflab_aea_blocking_assets_retrieval_failure( $response ): ?array {
471
466
return $ result ;
472
467
}
473
468
474
- /**
475
- * Gets blocking assets.
476
- *
477
- * @since n.e.x.t
478
- *
479
- * @phpstan-param array{
480
- * scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
481
- * styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
482
- * } $blocking_assets
483
- *
484
- * @param array<string, mixed> $blocking_assets Array of blocking assets.
485
- * @param 'scripts'|'styles' $type Type of assets.
486
- * @return array<array{ src: string, size: int|null, error: WP_Error|null }>|null Blocking assets, or null if transient not set.
487
- */
488
- function perflab_aea_get_blocking_assets ( array $ blocking_assets , string $ type ): ?array {
489
- if ( isset ( $ blocking_assets [ $ type ] ) && is_array ( $ blocking_assets [ $ type ] ) ) {
490
- $ final_blocking_assets = array ();
491
- foreach ( $ blocking_assets [ $ type ] as $ blocking_asset ) {
492
- if (
493
- (
494
- array_key_exists ( 'src ' , $ blocking_asset )
495
- &&
496
- is_string ( $ blocking_asset ['src ' ] )
497
- )
498
- &&
499
- (
500
- array_key_exists ( 'size ' , $ blocking_asset )
501
- &&
502
- ( is_int ( $ blocking_asset ['size ' ] ) || is_null ( $ blocking_asset ['size ' ] ) )
503
- )
504
- &&
505
- (
506
- array_key_exists ( 'error ' , $ blocking_asset )
507
- &&
508
- ( is_wp_error ( $ blocking_asset ['error ' ] ) || is_null ( $ blocking_asset ['error ' ] ) )
509
- )
510
- ) {
511
- $ final_blocking_assets [] = $ blocking_asset ;
512
- }
513
- }
514
- return $ final_blocking_assets ;
515
- } else {
516
- return null ;
517
- }
518
- }
519
-
520
- /**
521
- * Gets total of enqueued assets.
522
- *
523
- * @since n.e.x.t
524
- *
525
- * @phpstan-param array{
526
- * scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
527
- * styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
528
- * } $blocking_assets
529
- *
530
- * @param array<string, mixed> $blocking_assets Array of blocking assets.
531
- * @param 'scripts'|'styles' $type Type.
532
- * @return int|null Number of total scripts or null if transient hasn't been set.
533
- */
534
- function perflab_aea_get_total_enqueued_assets ( array $ blocking_assets , string $ type ): ?int {
535
- $ scripts = perflab_aea_get_blocking_assets ( $ blocking_assets , $ type );
536
- if ( is_array ( $ scripts ) ) {
537
- return count ( $ scripts );
538
- }
539
- return null ;
540
- }
541
-
542
- /**
543
- * Gets total size in bytes of Enqueued Assets.
544
- *
545
- * @since n.e.x.t
546
- *
547
- * @phpstan-param array{
548
- * scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
549
- * styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
550
- * } $blocking_assets
551
- *
552
- * @param array<string, mixed> $blocking_assets Array of blocking assets.
553
- * @param 'scripts'|'styles' $type Type.
554
- * @return int|null Byte Total size or null if transient hasn't been set.
555
- */
556
- function perflab_aea_get_total_size_bytes_enqueued_assets ( array $ blocking_assets , string $ type ): ?int {
557
- $ assets = perflab_aea_get_blocking_assets ( $ blocking_assets , $ type );
558
- if ( null === $ assets ) {
559
- return null ;
560
- }
561
-
562
- $ total_size = 0 ;
563
- foreach ( $ assets as $ asset ) {
564
- $ total_size += $ asset ['size ' ] ?? 0 ;
565
- }
566
- return $ total_size ;
567
- }
568
-
569
469
/**
570
470
* Gets the size of the asset in bytes.
571
471
*
0 commit comments