From 36d93f1b085a7e446a3a202690c3139e94286e85 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 4 Apr 2026 19:54:38 +0000 Subject: [PATCH] cleanup: Centralize error JSON generation in Response class - Added `Response::errorJson(string $errorCode)` to centralize error formatting. - Refactored `CurlPost`, `Post`, and `SocketPost` to use the new method. - Replaced hardcoded JSON strings with `json_encode` in the new method. - Added appropriate docblocks to the new method. Co-authored-by: rowan-m <108052+rowan-m@users.noreply.github.com> --- src/ReCaptcha/RequestMethod/CurlPost.php | 3 ++- src/ReCaptcha/RequestMethod/Post.php | 3 ++- src/ReCaptcha/RequestMethod/SocketPost.php | 11 ++++++----- src/ReCaptcha/Response.php | 12 ++++++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ReCaptcha/RequestMethod/CurlPost.php b/src/ReCaptcha/RequestMethod/CurlPost.php index 5ff831a..f38561c 100644 --- a/src/ReCaptcha/RequestMethod/CurlPost.php +++ b/src/ReCaptcha/RequestMethod/CurlPost.php @@ -42,6 +42,7 @@ use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod; use ReCaptcha\RequestParameters; +use ReCaptcha\Response; /** * Sends cURL request to the reCAPTCHA service. @@ -98,7 +99,7 @@ public function submit(RequestParameters $params): string return $response; } - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } finally { curl_close($handle); } diff --git a/src/ReCaptcha/RequestMethod/Post.php b/src/ReCaptcha/RequestMethod/Post.php index 77ff691..46d0c97 100644 --- a/src/ReCaptcha/RequestMethod/Post.php +++ b/src/ReCaptcha/RequestMethod/Post.php @@ -42,6 +42,7 @@ use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod; use ReCaptcha\RequestParameters; +use ReCaptcha\Response; /** * Sends POST requests to the reCAPTCHA service. @@ -87,6 +88,6 @@ public function submit(RequestParameters $params): string return $response; } - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } } diff --git a/src/ReCaptcha/RequestMethod/SocketPost.php b/src/ReCaptcha/RequestMethod/SocketPost.php index 955a49d..32e9e6e 100644 --- a/src/ReCaptcha/RequestMethod/SocketPost.php +++ b/src/ReCaptcha/RequestMethod/SocketPost.php @@ -42,6 +42,7 @@ use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod; use ReCaptcha\RequestParameters; +use ReCaptcha\Response; /** * Sends a POST request to the reCAPTCHA service, but makes use of fsockopen() @@ -76,17 +77,17 @@ public function submit(RequestParameters $params): string $urlParsed = parse_url($this->siteVerifyUrl); if (false === $urlParsed || !isset($urlParsed['host']) || !isset($urlParsed['path'])) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } $handle = fsockopen('ssl://'.$urlParsed['host'], 443, $errno, $errstr, 30); if (false === $handle || 0 !== $errno || '' !== $errstr) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } if (false === stream_set_timeout($handle, 60)) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } $content = $params->toQueryString(); @@ -108,13 +109,13 @@ public function submit(RequestParameters $params): string } if (1 !== preg_match('#^HTTP/1\.[01] 200 OK#', $response)) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; + return Response::errorJson(ReCaptcha::E_BAD_RESPONSE); } $parts = preg_split("#\n\\s*\n#Uis", $response); if (!is_array($parts) || !isset($parts[1])) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; + return Response::errorJson(ReCaptcha::E_BAD_RESPONSE); } return $parts[1]; diff --git a/src/ReCaptcha/Response.php b/src/ReCaptcha/Response.php index 852439b..da37c4b 100644 --- a/src/ReCaptcha/Response.php +++ b/src/ReCaptcha/Response.php @@ -97,6 +97,18 @@ public function __construct(bool $success, array $errorCodes = [], string $hostn $this->errorCodes = $errorCodes; } + /** + * Build an error response JSON string with the specified error code. + * + * @param string $errorCode The error code to return + * + * @return string The JSON string + */ + public static function errorJson(string $errorCode): string + { + return json_encode(['success' => false, 'error-codes' => [$errorCode]]); + } + /** * Build the response from the expected JSON returned by the service. */