• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / e69c2e79c97a57e26aa5c15ba3ca2a387f561b7d

02 Mar 2026 03:20PM UTC coverage: 53.204% (-0.7%) from 53.915%
e69c2e79c97a57e26aa5c15ba3ca2a387f561b7d

Pull #23031

github

web-flow
Merge 25ffa8e80 into 4ff1bc18e
Pull Request #23031: Refactor ai organization

5938 of 10669 branches covered (55.66%)

Branch coverage included in aggregate %.

718 of 759 new or added lines in 38 files covered. (94.6%)

718 existing lines in 33 files now uncovered.

21852 of 41564 relevant lines covered (52.57%)

71980.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ai-http-request/application/response-parser.php
1
<?php
2

3
namespace Yoast\WP\SEO\AI_HTTP_Request\Application;
4

5
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Response;
6

7
/**
8
 * Class Response_Parser
9
 * Parses the response from the AI API and creates a Response object.
10
 */
11
class Response_Parser implements Response_Parser_Interface {
12

13
        /**
14
         * Parses the response from the API.
15
         *
16
         * @param array<int|string|array<string>> $response The response from the API.
17
         *
18
         * @return Response The parsed response.
19
         */
UNCOV
20
        public function parse( $response ): Response {
×
UNCOV
21
                $response_code    = ( \wp_remote_retrieve_response_code( $response ) !== '' ) ? \wp_remote_retrieve_response_code( $response ) : 0;
×
UNCOV
22
                $response_message = \esc_html( \wp_remote_retrieve_response_message( $response ) );
×
UNCOV
23
                $error_code       = '';
×
UNCOV
24
                $missing_licenses = [];
×
25

UNCOV
26
                if ( $response_code !== 200 && $response_code !== 0 ) {
×
UNCOV
27
                        $json_body = \json_decode( \wp_remote_retrieve_body( $response ) );
×
UNCOV
28
                        if ( $json_body !== null ) {
×
UNCOV
29
                                $response_message = ( $json_body->message ?? $response_message );
×
UNCOV
30
                                $error_code       = ( $json_body->error_code ?? $this->map_message_to_code( $response_message ) );
×
UNCOV
31
                                if ( $response_code === 402 || $response_code === 429 ) {
×
UNCOV
32
                                        $missing_licenses = isset( $json_body->missing_licenses ) ? (array) $json_body->missing_licenses : [];
×
33
                                }
34
                        }
35
                }
36

UNCOV
37
                return new Response( $response['body'], $response_code, $response_message, $error_code, $missing_licenses );
×
38
        }
39

40
        /**
41
         * Maps the error message to a code.
42
         *
43
         * @param string $message The error message.
44
         *
45
         * @return string The mapped code.
46
         */
UNCOV
47
        private function map_message_to_code( string $message ): string {
×
UNCOV
48
                if ( \strpos( $message, 'must NOT have fewer than 1 characters' ) !== false ) {
×
UNCOV
49
                        return 'NOT_ENOUGH_CONTENT';
×
50
                }
UNCOV
51
                if ( \strpos( $message, 'Client timeout' ) !== false ) {
×
UNCOV
52
                        return 'CLIENT_TIMEOUT';
×
53
                }
UNCOV
54
                if ( \strpos( $message, 'Server timeout' ) !== false ) {
×
UNCOV
55
                        return 'SERVER_TIMEOUT';
×
56
                }
57

UNCOV
58
                return 'UNKNOWN';
×
59
        }
60
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc