Skip to content

Commit 5d8c693

Browse files
Copilotswissspidy
andcommitted
Refactor: Extract common error handling logic into set_version_check_error helper method
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 8ae12b6 commit 5d8c693

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

src/Core_Command.php

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,22 +1544,11 @@ private function get_updates( $assoc_args ) {
15441544
}
15451545

15461546
/**
1547-
* Handles the pre_http_request filter to capture HTTP errors during version check.
1548-
*
1549-
* This method signature matches the pre_http_request filter signature.
1550-
* Uses PHP_INT_MAX priority to run after test mocking and plugin modifications.
1547+
* Sets or clears the version check error property based on an HTTP response.
15511548
*
1552-
* @param false|array|WP_Error $response A preemptive return value of an HTTP request.
1553-
* @param array $args HTTP request arguments.
1554-
* @param string $url The request URL.
1555-
* @return false|array|WP_Error The response, unmodified.
1549+
* @param mixed $response The HTTP response (WP_Error, array, or other).
15561550
*/
1557-
public function capture_version_check_error( $response, $args, $url ) {
1558-
if ( false === strpos( $url, 'api.wordpress.org/core/version-check' ) ) {
1559-
return $response;
1560-
}
1561-
1562-
// Store the error if the response is a WP_Error
1551+
private function set_version_check_error( $response ) {
15631552
if ( is_wp_error( $response ) ) {
15641553
$this->version_check_error = $response;
15651554
} elseif ( is_array( $response ) && isset( $response['response']['code'] ) && $response['response']['code'] >= 400 ) {
@@ -1572,10 +1561,34 @@ public function capture_version_check_error( $response, $args, $url ) {
15721561
isset( $response['response']['message'] ) ? $response['response']['message'] : 'Unknown error'
15731562
)
15741563
);
1575-
} elseif ( false !== $response ) {
1576-
// Clear any previous error if we got a successful mocked response
1564+
} else {
1565+
// Clear any previous error if we got a successful response.
15771566
$this->version_check_error = null;
15781567
}
1568+
}
1569+
1570+
/**
1571+
* Handles the pre_http_request filter to capture HTTP errors during version check.
1572+
*
1573+
* This method signature matches the pre_http_request filter signature.
1574+
* Uses PHP_INT_MAX priority to run after test mocking and plugin modifications.
1575+
*
1576+
* @param false|array|WP_Error $response A preemptive return value of an HTTP request.
1577+
* @param array $args HTTP request arguments.
1578+
* @param string $url The request URL.
1579+
* @return false|array|WP_Error The response, unmodified.
1580+
*/
1581+
public function capture_version_check_error( $response, $args, $url ) {
1582+
if ( false === strpos( $url, 'api.wordpress.org/core/version-check' ) ) {
1583+
return $response;
1584+
}
1585+
1586+
// A `false` response means the request is not being preempted.
1587+
// In this case, we don't want to change the error status, as the subsequent
1588+
// `http_api_debug` hook will handle the actual response.
1589+
if ( false !== $response ) {
1590+
$this->set_version_check_error( $response );
1591+
}
15791592

15801593
return $response;
15811594
}
@@ -1602,23 +1615,7 @@ public function capture_version_check_error_from_response( $response, $context,
16021615
return;
16031616
}
16041617

1605-
// Store the error if the response is a WP_Error
1606-
if ( is_wp_error( $response ) ) {
1607-
$this->version_check_error = $response;
1608-
} elseif ( is_array( $response ) && isset( $response['response']['code'] ) && $response['response']['code'] >= 400 ) {
1609-
// HTTP error status code (4xx or 5xx) - convert to WP_Error for consistency
1610-
$this->version_check_error = new \WP_Error(
1611-
'http_request_failed',
1612-
sprintf(
1613-
'HTTP request failed with status %d: %s',
1614-
$response['response']['code'],
1615-
isset( $response['response']['message'] ) ? $response['response']['message'] : 'Unknown error'
1616-
)
1617-
);
1618-
} else {
1619-
// Clear any previous error if we got a successful response
1620-
$this->version_check_error = null;
1621-
}
1618+
$this->set_version_check_error( $response );
16221619
}
16231620

16241621
/**

0 commit comments

Comments
 (0)