0

in php I see myself doing often something like this:

$res->getBody()->getContents();

but if body returns null, the next call will throw a fatal.

In Eloquent this can be even worse.

Is there a cleaner solution instead of

if ($res and $res->getBody()) {
    return $res->getBody()->getContent();
} else {
    return null;
}
Nick
  • 2,593
  • 3
  • 30
  • 59
Toskan
  • 13,911
  • 14
  • 95
  • 185
  • Does this answer your question? [?: operator (the 'Elvis operator') in PHP](https://stackoverflow.com/questions/1993409/operator-the-elvis-operator-in-php) – Dai Jan 25 '20 at 23:19
  • @Dai not really no – Toskan Jan 26 '20 at 01:12

2 Answers2

0

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
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • only the first entry is an answer, right? but it's not very readable. Just imagine it's not `$res->getBody()->getContents()` but `$res->getBody()->getContents()->getFirstItem()->getName()` – Toskan Jan 26 '20 at 01:13
  • yes, but there is no other shortcut for that, if you don't want to use Exceptions, which are a lot less readable/elegant that a ternary operator @Toskan – Alberto Sinigaglia Jan 26 '20 at 01:49
  • check what you think about my answer... notice it uses the new arrow functions but can be do without as well – Toskan Jan 26 '20 at 04:01
  • @Toskan yes but you are basing your function to use errors to achieve your goal, which is not a very well seen practice (https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why) – Alberto Sinigaglia Jan 26 '20 at 13:18
  • well the main reason for not using is exceptions is that I might swallow a real exception, not just a nullpointer. The other arguments are a bit moot imho. I'd say if you could adjust the exception, to only catch the null pointer exception or whatever it is, it could be fine. Certainly better than over and over doing `if($a and $a->b() and $a->b()->c() and $a->b()->c() and $a->b()->c()->d() and $a0>b()->....)` notice as well, this code will run code for each single check. That means to avoid penalities you'd have to create variables as well otherwise you re and rerun code all over – Toskan Jan 26 '20 at 18:23
0

what are your thoughts about this approach:

function valueOrNull(callable $closure){
    try{
        return $closure();
    } catch (\Throwable $e){
        return null;
    }
}

//$rv always null or return value never matter what
$rv  = valueOrNull(fn()=> $res->getBody()->getContents()->getFirstItem()->getName());;
Toskan
  • 13,911
  • 14
  • 95
  • 185