Skip to content

Commit 263e4de

Browse files
committed
Remove unnecessary helper functions
1 parent 1f1a674 commit 263e4de

File tree

3 files changed

+36
-211
lines changed

3 files changed

+36
-211
lines changed

plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php

Lines changed: 32 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,12 @@ function perflab_aea_enqueued_blocking_assets_test(): array {
174174

175175
$result['description'] .= perflab_aea_generate_blocking_assets_table( $audit_result['assets'] );
176176

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'];
183179

184180
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']
187183
) {
188184
$result['status'] = 'recommended';
189185
$result['actions'] = sprintf(
@@ -217,14 +213,17 @@ function perflab_aea_enqueued_ajax_blocking_assets_test(): void {
217213
* } $blocking_assets
218214
*
219215
* @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.
221217
*/
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+
);
228227

229228
$result = array(
230229
'status' => 'good',
@@ -286,13 +285,10 @@ function perflab_aea_enqueued_blocking_scripts( array $blocking_assets ): ?array
286285
}
287286

288287
// 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;
296292
}
297293
}
298294

@@ -310,15 +306,17 @@ function perflab_aea_enqueued_blocking_scripts( array $blocking_assets ): ?array
310306
* } $blocking_assets
311307
*
312308
* @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.
314310
*/
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+
);
322320

323321
$result = array(
324322
'status' => 'good',
@@ -380,13 +378,10 @@ function perflab_aea_enqueued_blocking_styles( array $blocking_assets ): ?array
380378
}
381379

382380
// 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;
390385
}
391386
}
392387

@@ -471,101 +466,6 @@ function perflab_aea_blocking_assets_retrieval_failure( $response ): ?array {
471466
return $result;
472467
}
473468

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-
569469
/**
570470
* Gets the size of the asset in bytes.
571471
*

plugins/performance-lab/tests/data/class-site-health-mock-responses.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function return_aea_enqueued_js_assets_test_callback_less_than_thr
4848
'performance-lab'
4949
),
5050
$enqueued_scripts,
51-
size_format( perflab_aea_get_total_size_bytes_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'scripts', $enqueued_scripts ), 'scripts' ) )
51+
size_format( $enqueued_scripts * 1000 )
5252
)
5353
)
5454
),
@@ -76,7 +76,7 @@ public static function return_aea_enqueued_js_assets_test_callback_more_than_thr
7676
'performance-lab'
7777
),
7878
$enqueued_scripts,
79-
size_format( perflab_aea_get_total_size_bytes_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'scripts', $enqueued_scripts ), 'scripts' ) )
79+
size_format( $enqueued_scripts * 1000 )
8080
)
8181
)
8282
),
@@ -104,7 +104,7 @@ public static function return_aea_enqueued_css_assets_test_callback_less_than_th
104104
'performance-lab'
105105
),
106106
$enqueued_styles,
107-
size_format( perflab_aea_get_total_size_bytes_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'styles', $enqueued_styles ), 'styles' ) )
107+
size_format( $enqueued_styles * 1000 )
108108
)
109109
)
110110
),
@@ -132,7 +132,7 @@ public static function return_aea_enqueued_css_assets_test_callback_more_than_th
132132
'performance-lab'
133133
),
134134
$enqueued_styles,
135-
size_format( perflab_aea_get_total_size_bytes_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'styles', $enqueued_styles ), 'styles' ) )
135+
size_format( $enqueued_styles * 1000 )
136136
)
137137
)
138138
),

plugins/performance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets-helper.php

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,6 @@ public function test_perflab_aea_enqueued_blocking_assets_test_but_one_style_is_
269269
$this->assertStringContainsString( 'Not found', $test['description'] );
270270
}
271271

272-
/**
273-
* Test perflab_aea_enqueued_blocking_scripts() no blocking scripts.
274-
*
275-
* @covers ::perflab_aea_enqueued_blocking_scripts
276-
*/
277-
public function test_perflab_aea_enqueued_js_assets_test_no_blocking_scripts(): void {
278-
$this->assertNull( perflab_aea_enqueued_blocking_scripts( array() ) );
279-
}
280-
281272
/**
282273
* Test perflab_aea_enqueued_blocking_scripts() with scripts less than WARNING_SCRIPTS_threshold.
283274
*
@@ -298,15 +289,6 @@ public function test_perflab_aea_enqueued_js_assets_test_with_assets_more_than_t
298289
$this->assertEqualSets( $mocked_data, perflab_aea_enqueued_blocking_scripts( Audit_Assets_Mock_Assets::mock_assets( 'scripts', self::WARNING_SCRIPTS_THRESHOLD ) ) );
299290
}
300291

301-
/**
302-
* Test perflab_aea_enqueued_blocking_styles() no blocking styles.
303-
*
304-
* @covers ::perflab_aea_enqueued_blocking_styles
305-
*/
306-
public function test_perflab_aea_enqueued_css_assets_test_no_blocking_styles(): void {
307-
$this->assertNull( perflab_aea_enqueued_blocking_styles( array() ) );
308-
}
309-
310292
/**
311293
* Test perflab_aea_enqueued_blocking_styles() with styles less than WARNING_STYLES_threshold.
312294
*
@@ -400,63 +382,6 @@ public function test_perflab_aea_blocking_assets_retrieval_failure_wp_error(): v
400382
$this->assertSame( 'recommended', $result['status'] );
401383
}
402384

403-
/**
404-
* Tests perflab_aea_get_total_enqueued_assets() with no assets.
405-
*
406-
* @covers ::perflab_aea_get_blocking_assets
407-
* @covers ::perflab_aea_get_total_enqueued_assets
408-
*/
409-
public function test_perflab_aea_get_total_enqueued_assets_no_assets(): void {
410-
$this->assertNull( perflab_aea_get_blocking_assets( array(), 'scripts' ) );
411-
$this->assertNull( perflab_aea_get_total_enqueued_assets( array(), 'scripts' ) );
412-
$this->assertNull( perflab_aea_get_blocking_assets( array(), 'styles' ) );
413-
$this->assertNull( perflab_aea_get_total_enqueued_assets( array(), 'styles' ) );
414-
}
415-
416-
/**
417-
* Tests perflab_aea_get_total_enqueued_assets( 'scripts' ).
418-
*
419-
* @covers ::perflab_aea_get_blocking_assets
420-
* @covers ::perflab_aea_get_total_enqueued_assets
421-
*/
422-
public function test_perflab_aea_get_total_enqueued_scripts(): void {
423-
$this->assertNull( perflab_aea_get_total_enqueued_assets( array(), 'scripts' ) );
424-
$this->assertSame( 5, perflab_aea_get_total_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'scripts', 5 ), 'scripts' ) );
425-
}
426-
427-
/**
428-
* Tests perflab_aea_get_total_enqueued_assets( 'styles' ).
429-
*
430-
* @covers ::perflab_aea_get_blocking_assets
431-
* @covers ::perflab_aea_get_total_enqueued_assets
432-
*/
433-
public function test_perflab_aea_get_total_enqueued_styles(): void {
434-
$this->assertNull( perflab_aea_get_total_enqueued_assets( array(), 'styles' ) );
435-
$this->assertSame( 5, perflab_aea_get_total_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'styles', 5 ), 'styles' ) );
436-
}
437-
438-
/**
439-
* Tests perflab_aea_get_total_size_bytes_enqueued_assets( 'scripts' ).
440-
*
441-
* @covers ::perflab_aea_get_blocking_assets
442-
* @covers ::perflab_aea_get_total_size_bytes_enqueued_assets
443-
*/
444-
public function test_perflab_aea_get_total_size_bytes_enqueued_scripts(): void {
445-
$this->assertNull( perflab_aea_get_total_size_bytes_enqueued_assets( array(), 'scripts' ) );
446-
$this->assertSame( 5000, perflab_aea_get_total_size_bytes_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'scripts', 5 ), 'scripts' ) );
447-
}
448-
449-
/**
450-
* Tests perflab_aea_get_total_size_bytes_enqueued_assets( 'styles' ).
451-
*
452-
* @covers ::perflab_aea_get_blocking_assets
453-
* @covers ::perflab_aea_get_total_size_bytes_enqueued_assets
454-
*/
455-
public function test_perflab_aea_get_total_size_bytes_enqueued_styles(): void {
456-
$this->assertNull( perflab_aea_get_total_size_bytes_enqueued_assets( array(), 'styles' ) );
457-
$this->assertEquals( 5000, perflab_aea_get_total_size_bytes_enqueued_assets( Audit_Assets_Mock_Assets::mock_assets( 'styles', 5 ), 'styles' ) );
458-
}
459-
460385
/**
461386
* Tests perflab_aea_get_asset_size() with various scenarios.
462387
*

0 commit comments

Comments
 (0)