Skip to content

Commit b0f4ae5

Browse files
committed
Add explicit typing and remove unnecessary code due to type guarantees
1 parent c10d208 commit b0f4ae5

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
lines changed

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

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@
1717
*
1818
* @since n.e.x.t
1919
*
20-
* @return array<string,mixed> Returns an array containing response and blocking assets.
20+
* @return array{
21+
* response: WP_Error|array{
22+
* headers: WpOrg\Requests\Utility\CaseInsensitiveDictionary,
23+
* body: string,
24+
* response: array{
25+
* code: int|false,
26+
* message: string|false,
27+
* },
28+
* },
29+
* assets: array{
30+
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
31+
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
32+
* }
33+
* } An array containing response and blocking assets.
2134
*/
2235
function perflab_aea_audit_blocking_assets(): array {
2336
$response = wp_remote_get(
@@ -150,11 +163,10 @@ function perflab_aea_enqueued_blocking_assets_test(): array {
150163
);
151164

152165
$audit_result = perflab_aea_audit_blocking_assets();
153-
if ( is_wp_error( $audit_result['response'] ) || is_array( $audit_result['response'] ) ) {
154-
$retrieval_failure_result = perflab_aea_blocking_assets_retrieval_failure( $audit_result['response'] );
155-
if ( null !== $retrieval_failure_result ) {
156-
return array_merge( $result, $retrieval_failure_result );
157-
}
166+
167+
$retrieval_failure_result = perflab_aea_blocking_assets_retrieval_failure( $audit_result['response'] );
168+
if ( null !== $retrieval_failure_result ) {
169+
return array_merge( $result, $retrieval_failure_result );
158170
}
159171

160172
$scripts_result = perflab_aea_enqueued_blocking_scripts( $audit_result['assets'] );
@@ -199,6 +211,11 @@ function perflab_aea_enqueued_ajax_blocking_assets_test(): void {
199211
*
200212
* @since n.e.x.t
201213
*
214+
* @phpstan-param array{
215+
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
216+
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
217+
* } $blocking_assets
218+
*
202219
* @param array<string, mixed> $blocking_assets Array of blocking assets.
203220
* @return array{status: 'good'|'recommended', description: string}|null Result.
204221
*/
@@ -287,6 +304,11 @@ function perflab_aea_enqueued_blocking_scripts( array $blocking_assets ): ?array
287304
*
288305
* @since n.e.x.t
289306
*
307+
* @phpstan-param array{
308+
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
309+
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
310+
* } $blocking_assets
311+
*
290312
* @param array<string, mixed> $blocking_assets Array of blocking assets.
291313
* @return array{status: string, description: string}|null Result.
292314
*/
@@ -376,6 +398,15 @@ function perflab_aea_enqueued_blocking_styles( array $blocking_assets ): ?array
376398
*
377399
* @since n.e.x.t
378400
*
401+
* @phpstan-param WP_Error|array{
402+
* headers: WpOrg\Requests\Utility\CaseInsensitiveDictionary,
403+
* body: string,
404+
* response: array{
405+
* code: int|false,
406+
* message: string|false,
407+
* },
408+
* } $response
409+
*
379410
* @param WP_Error|array<string, mixed> $response The response from the home page retrieval.
380411
* @return array{status: 'recommended', description: string}|null Result, or null if there was no failure.
381412
*/
@@ -445,6 +476,11 @@ function perflab_aea_blocking_assets_retrieval_failure( $response ): ?array {
445476
*
446477
* @since n.e.x.t
447478
*
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+
*
448484
* @param array<string, mixed> $blocking_assets Array of blocking assets.
449485
* @param 'scripts'|'styles' $type Type of assets.
450486
* @return array<array{ src: string, size: int|null, error: WP_Error|null }>|null Blocking assets, or null if transient not set.
@@ -486,6 +522,11 @@ function perflab_aea_get_blocking_assets( array $blocking_assets, string $type )
486522
*
487523
* @since n.e.x.t
488524
*
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+
*
489530
* @param array<string, mixed> $blocking_assets Array of blocking assets.
490531
* @param 'scripts'|'styles' $type Type.
491532
* @return int|null Number of total scripts or null if transient hasn't been set.
@@ -503,6 +544,11 @@ function perflab_aea_get_total_enqueued_assets( array $blocking_assets, string $
503544
*
504545
* @since n.e.x.t
505546
*
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+
*
506552
* @param array<string, mixed> $blocking_assets Array of blocking assets.
507553
* @param 'scripts'|'styles' $type Type.
508554
* @return int|null Byte Total size or null if transient hasn't been set.
@@ -583,11 +629,16 @@ function perflab_get_http_basic_authorization_headers(): array {
583629
*
584630
* @since n.e.x.t
585631
*
586-
* @param array{scripts: array<string, mixed>, styles: array<string, mixed>} $blocking_assets Array of blocking assets.
632+
* @phpstan-param array{
633+
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
634+
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
635+
* } $blocking_assets
636+
*
637+
* @param array<string, mixed> $blocking_assets Array of blocking assets.
587638
* @return string HTML table of blocking assets.
588639
*/
589640
function perflab_aea_generate_blocking_assets_table( array $blocking_assets ): string {
590-
if ( 0 === count( $blocking_assets ) || ( isset( $blocking_assets['scripts'] ) && 0 === count( $blocking_assets['scripts'] ) && 0 === count( $blocking_assets['styles'] ) ) ) {
641+
if ( 0 === count( $blocking_assets['scripts'] ) && 0 === count( $blocking_assets['styles'] ) ) {
591642
return '';
592643
}
593644

@@ -605,12 +656,10 @@ function perflab_aea_generate_blocking_assets_table( array $blocking_assets ): s
605656
foreach ( $asset_types as $type => $label ) {
606657
if ( isset( $blocking_assets[ $type ] ) && is_array( $blocking_assets[ $type ] ) ) {
607658
foreach ( $blocking_assets[ $type ] as $asset ) {
608-
$has_error = is_wp_error( $asset['error'] );
609-
610-
$table .= $has_error ? '<tr style="background-color: #ffecec;">' : '<tr>';
659+
$table .= is_wp_error( $asset['error'] ) ? '<tr style="background-color: #ffecec;">' : '<tr>';
611660
$table .= '<td>' . esc_html( $label ) . '</td>';
612661
$table .= '<td>' . esc_url( $asset['src'] );
613-
if ( $has_error ) {
662+
if ( is_wp_error( $asset['error'] ) ) {
614663
$table .= '<p>' . wp_kses( $asset['error']->get_error_message(), array( 'code' => array() ) ) . '</p>';
615664
}
616665
$table .= '</td>';
@@ -622,7 +671,7 @@ function perflab_aea_generate_blocking_assets_table( array $blocking_assets ): s
622671
}
623672
$table .= '</td>';
624673
$table .= '<td>';
625-
if ( $has_error ) {
674+
if ( is_wp_error( $asset['error'] ) ) {
626675
$table .= esc_html__( 'Error', 'performance-lab' );
627676
} else {
628677
$table .= esc_html__( 'OK', 'performance-lab' );

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,15 @@ public function test_perflab_get_http_basic_authorization_headers(): void {
525525
* @covers ::perflab_aea_generate_blocking_assets_table
526526
*/
527527
public function test_perflab_aea_generate_blocking_assets_table_empty(): void {
528-
$this->assertSame( '', perflab_aea_generate_blocking_assets_table( array() ) );
528+
$this->assertSame(
529+
'',
530+
perflab_aea_generate_blocking_assets_table(
531+
array(
532+
'scripts' => array(),
533+
'styles' => array(),
534+
)
535+
)
536+
);
529537
}
530538

531539
/**
@@ -534,7 +542,12 @@ public function test_perflab_aea_generate_blocking_assets_table_empty(): void {
534542
* @covers ::perflab_aea_generate_blocking_assets_table
535543
*/
536544
public function test_perflab_aea_generate_blocking_assets_table_scripts(): void {
537-
$table = perflab_aea_generate_blocking_assets_table( Audit_Assets_Mock_Assets::mock_assets( 'scripts', 5 ) );
545+
$table = perflab_aea_generate_blocking_assets_table(
546+
array_merge(
547+
array( 'styles' => array() ),
548+
Audit_Assets_Mock_Assets::mock_assets( 'scripts', 5 )
549+
)
550+
);
538551
$processor = new WP_HTML_Tag_Processor( $table );
539552
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'TABLE' ) ) );
540553
}
@@ -545,7 +558,12 @@ public function test_perflab_aea_generate_blocking_assets_table_scripts(): void
545558
* @covers ::perflab_aea_generate_blocking_assets_table
546559
*/
547560
public function test_perflab_aea_generate_blocking_assets_table_css(): void {
548-
$table = perflab_aea_generate_blocking_assets_table( Audit_Assets_Mock_Assets::mock_assets( 'styles', 5 ) );
561+
$table = perflab_aea_generate_blocking_assets_table(
562+
array_merge(
563+
array( 'scripts' => array() ),
564+
Audit_Assets_Mock_Assets::mock_assets( 'styles', 5 )
565+
)
566+
);
549567
$processor = new WP_HTML_Tag_Processor( $table );
550568
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'TABLE' ) ) );
551569
}

0 commit comments

Comments
 (0)