Skip to content

Commit 12d0b71

Browse files
committed
Refactor to only return array to improve clarity, Simplify conditional logic
1 parent 49ca83f commit 12d0b71

File tree

1 file changed

+26
-17
lines changed
  • plugins/performance-lab/includes/site-health/far-future-headers

1 file changed

+26
-17
lines changed

plugins/performance-lab/includes/site-health/far-future-headers/helper.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ function perflab_ffh_check_assets( array $assets ): array {
121121
}
122122

123123
$check = perflab_ffh_check_headers( $headers );
124-
if ( isset( $check['passed'] ) && $check['passed'] ) {
124+
if ( $check['passed'] ) {
125125
// This asset passed far-future headers test, no action needed.
126126
continue;
127127
}
128128

129129
// If not passed, decide whether to try conditional request.
130-
if ( false === $check ) {
130+
if ( $check['missing_max_age'] ) {
131131
// Only if no far-future headers at all, we try conditional request.
132132
$conditional_pass = perflab_ffh_try_conditional_request( $asset, $headers );
133133
$final_status = 'recommended';
@@ -165,9 +165,9 @@ function perflab_ffh_check_assets( array $assets ): array {
165165
* @access private
166166
*
167167
* @param WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string|array<string>> $headers Response headers.
168-
* @return array{passed: bool, reason: string}|false Detailed result. If passed=false, reason explains why it failed and false if no headers found.
168+
* @return array{passed: bool, reason: string, missing_max_age: bool} Detailed result of the check.
169169
*/
170-
function perflab_ffh_check_headers( $headers ) {
170+
function perflab_ffh_check_headers( $headers ): array {
171171
/**
172172
* Filters the threshold for far-future headers.
173173
*
@@ -195,8 +195,9 @@ function perflab_ffh_check_headers( $headers ) {
195195
// If max-age meets or exceeds the threshold, we consider it good.
196196
if ( $max_age >= $threshold ) {
197197
return array(
198-
'passed' => true,
199-
'reason' => '',
198+
'passed' => true,
199+
'reason' => '',
200+
'missing_max_age' => false,
200201
);
201202
}
202203

@@ -207,47 +208,55 @@ function perflab_ffh_check_headers( $headers ) {
207208
if ( $remaining_time >= $threshold ) {
208209
// Good - Expires far in the future.
209210
return array(
210-
'passed' => true,
211-
'reason' => '',
211+
'passed' => true,
212+
'reason' => '',
213+
'missing_max_age' => false,
212214
);
213215
}
214216

215217
// Expires header exists but not far enough in the future.
216218
if ( $max_age > 0 ) {
217219
return array(
218-
'passed' => false,
219-
'reason' => sprintf(
220+
'passed' => false,
221+
'reason' => sprintf(
220222
/* translators: 1: actual max-age value in seconds, 2: threshold in seconds */
221223
__( 'max-age below threshold (actual: %1$s seconds, threshold: %2$s seconds)', 'performance-lab' ),
222224
number_format_i18n( $max_age ),
223225
number_format_i18n( $threshold )
224226
),
227+
'missing_max_age' => false,
225228
);
226229
}
227230
return array(
228-
'passed' => false,
229-
'reason' => sprintf(
231+
'passed' => false,
232+
'reason' => sprintf(
230233
/* translators: 1: actual Expires header value in seconds, 2: threshold in seconds */
231234
__( 'expires below threshold (actual: %1$s seconds, threshold: %2$s seconds)', 'performance-lab' ),
232235
number_format_i18n( $remaining_time ),
233236
number_format_i18n( $threshold )
234237
),
238+
'missing_max_age' => false,
235239
);
236240
}
237241

238242
// No max-age or expires found at all or max-age < threshold and no expires.
239243
if ( 0 === $max_age ) {
240-
return false;
244+
return array(
245+
'passed' => false,
246+
'reason' => '',
247+
'missing_max_age' => true,
248+
);
241249
} else {
242250
// max-age was present but below threshold and no expires.
243251
return array(
244-
'passed' => false,
245-
'reason' => sprintf(
252+
'passed' => false,
253+
'reason' => sprintf(
246254
/* translators: 1: actual max-age value in seconds, 2: threshold in seconds */
247255
__( 'max-age below threshold (actual: %1$s seconds, threshold: %2$s seconds)', 'performance-lab' ),
248256
number_format_i18n( $max_age ),
249257
number_format_i18n( $threshold )
250258
),
259+
'missing_max_age' => false,
251260
);
252261
}
253262
}
@@ -263,8 +272,8 @@ function perflab_ffh_check_headers( $headers ) {
263272
* @return bool True if a 304 response was received.
264273
*/
265274
function perflab_ffh_try_conditional_request( string $url, $headers ): bool {
266-
$etag = isset( $headers['etag'] ) ? $headers['etag'] : '';
267-
$last_modified = isset( $headers['last-modified'] ) ? $headers['last-modified'] : '';
275+
$etag = $headers['etag'] ?? '';
276+
$last_modified = $headers['last-modified'] ?? '';
268277

269278
$conditional_headers = array();
270279
if ( '' !== $etag ) {

0 commit comments

Comments
 (0)