Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Try/catch statement with return on class method

So I have a method inside my class, that does a GET request and then there are error handlers and various other components, but then if the response is good, it will be returned inside // THE 200 RESPONSE IS HERE.

How would I be able to make the method return the try/catch $response_body outside the try/catch if it’s successful? So I have can return $response_body at the end of the method so that I have a valid return.

Update:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So I have the following method inside my class:

public function get_posts_via_rest_api(): array
    {
        $page = 1;
        try {
            $response = wp_remote_get(
                $this->global_endpoint . '?page=' . $page
            );
            if ((!is_wp_error($response)) && (200 === wp_remote_retrieve_response_code($response))) {
                $response_body = json_decode(
                    $response['body'],
                    false,
                    512,
                    JSON_THROW_ON_ERROR
                );
                if (empty($response_body)) {
                    return [];
                }
                if (json_last_error() === JSON_ERROR_NONE) {

                    // THE 200 RESPONSE IS HERE

                    $posts = $response_body;
                }
            } else {
                error_log(
                    print_r(
                        'Error: ' . $response->get_error_message()
                    )
                );

                return [];
            }
        } catch (Exception $e) {
            error_log(
                print_r(
                    'Error: ' . $e,
                    true
                )
            );

            return [];
        }

        // THIS SHOWS ERROR IN IDE
        return $posts;
    }

>Solution :

if (json_last_error() === JSON_ERROR_NONE) {

PHPStorm doesn’t know that this statement is redundant (you’ve already told json_decode() to throw exceptions) so all it sees is a potential logic path missing a return value because there’s no else to this if.

You can simplify your code to use the following

$response_body = json_decode(
    $response['body'],
    false,
    512,
    JSON_THROW_ON_ERROR
);

return empty($response_body) ? [] : $response_body;

Now all your paths are covered.


You could also consolidate all your unhappy path return values into one statement at the end of your method.

The above can become

if (!empty($response_body)) {
    return $response_body;
}

and remove all the return []; and place a single return at the end.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading