On 4/15/16 12:22 AM, Levi Morrison wrote:

My point is that `foo(bar(), $val)` won't die because bar may return
null. Bar is expected to return null sometimes.

For example, let's consider an administrator page where they look up
user information based on an identifier. The routine we'll use will
have this signature:

     function get_user(string $id): User | Null;

It is possible for an identifier to not exist and this is not an error
(database successfully returned no results). If there is no User data
to display then it makes sense for the UI to present that differently.
Thus it makes sense to pass that User | Null onto the code that will
present it:

     $user_data = get_user($id);
     // ...
     $user_html = render_user_data($user_data);

In fact this is a common operation that is encountered in many code
bases (I think every single one I've ever looked at).

This is a good example.

My opinion is that *because* get_user() can return null (a red flag) I prefer to see explicit handing of the null case before doing anything else.

If I would end up with `render_user_data(get_user($id))` I would consider it fair to not hint the param because I didn't earn it. I invented the faux docblock tag @sorry for this kind of thing.

Tom


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to