Skip to content

Commit 301cfd2

Browse files
Copilotswissspidy
andcommitted
Address code review feedback: add try-finally, use str_contains, clarify unused params
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent ce80b88 commit 301cfd2

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/Core_Command.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,10 +1463,12 @@ private function get_updates( $assoc_args ) {
14631463
// Hook into HTTP API debug to capture errors during version check
14641464
add_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10, 5 );
14651465

1466-
wp_version_check( [], $force_check );
1467-
1468-
// Remove the hook after version check
1469-
remove_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10 );
1466+
try {
1467+
wp_version_check( [], $force_check );
1468+
} finally {
1469+
// Ensure the hook is always removed, even if wp_version_check() throws an exception
1470+
remove_action( 'http_api_debug', [ $this, 'capture_version_check_error' ], 10 );
1471+
}
14701472

14711473
/**
14721474
* @var object{updates: array<object{version: string, locale: string, packages: object{partial?: string, full: string}}>}|false $from_api
@@ -1526,15 +1528,23 @@ private function get_updates( $assoc_args ) {
15261528
/**
15271529
* Handles the http_api_debug action to capture HTTP errors during version check.
15281530
*
1531+
* This method signature matches the http_api_debug action hook signature.
1532+
* Note: $class and $args parameters are not used but are required by the hook.
1533+
*
15291534
* @param array|WP_Error $response HTTP response or WP_Error object.
15301535
* @param string $context Context of the HTTP request.
1531-
* @param string $class HTTP transport class name.
1532-
* @param array $args HTTP request arguments.
1536+
* @param string $_class HTTP transport class name (unused).
1537+
* @param array $_args HTTP request arguments (unused).
15331538
* @param string $url URL being requested.
15341539
*/
1535-
private function capture_version_check_error( $response, $context, $class, $args, $url ) {
1536-
// Only capture errors for the version check API
1537-
if ( false === strpos( $url, 'api.wordpress.org/core/version-check' ) ) {
1540+
private function capture_version_check_error( $response, $context, $_class, $_args, $url ) {
1541+
// Only capture errors for the version check API using str_contains for PHP 8.0+
1542+
// or fallback to strpos for older versions
1543+
$is_version_check_url = function_exists( 'str_contains' )
1544+
? str_contains( $url, 'api.wordpress.org/core/version-check' )
1545+
: false !== strpos( $url, 'api.wordpress.org/core/version-check' );
1546+
1547+
if ( ! $is_version_check_url ) {
15381548
return;
15391549
}
15401550

0 commit comments

Comments
 (0)