You can use the ternary operator (always used in Laravel for example):
return $res and $res->getBody() ? $res->getBody()->getContents() : null;
or in cases where you want to return the same think you check, the Elvis operator
:
return $res->getBody()?: $something_else ; // this check only if the getbody() call return null, and if not, return what that call has returns, otherwise $something_else
Just as note, you can use the default operator sometimes like this:
return $res->getBody() ?? $default_or_whatever; // if getBody returns something evaluated as false, it will return the $default_or_whatever
// same thing as return $res->getBody() ? $res->getBody() : $default_or_whatever
// and return $res->getBody() ?: $default_or_whatever