-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Correct and test none/null return types distinction #1080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct and test none/null return types distinction #1080
Conversation
0a178c5
to
635b61a
Compare
I'm wondering if we can do better messages for return type errors in general. Something like:
Instead of:
What do you think? |
Producing "but" seems weird to me for some reason. I wrote a unit testing library that used "but" in its assertion failure error messages, and it just sounded wrong. I can't put my finger on why, though. |
We could use "and" or "yet" there as well, if you like those better. |
maybe use the similar to the warning message like parse args? ""%s%s%s() expects parameter %d to be %s, %s given"" %s expects return type of array , null returned in %s on %d? |
Hi Levi, Change it as you wish (just update the tests accordingly). Thanks. Dmitry. On Fri, Feb 13, 2015 at 5:06 AM, Levi Morrison notifications@github.com
|
Instead of
perhaps it could be
so:
? |
As an interim solution, though, "no value" is fine, so I've updated the patch to use that instead of "none". |
Current travis failure is unrelated to changes in patch. |
It rather looks to me like first case is wrong. It's well-defined that no defined return value means null is returned. So please reflect this in the error message too. |
@bwoebi Surely, then, the lack of a return statement should do the same? |
Also, bear in mind that what you say is only true since PHP 7. |
@TazeTSchnitzel It always returned null. Lack of return statement should give null in the error message too, yes. As said, first error message is wrong to me. |
It always returned null, but there's a distinction between explicitly returning null, and implicitly receiving null if nothing is returned. |
Well, at the end of the op_array is always an implicit And by that logic (which also always was my view on that), it's always null, not "none". There's no such thing like no return value in PHP. Every function has its return value, and if it's null. |
That's not true. In PHP 5, functions could return either nothing, or null. |
Example of something returning nothing in PHP 5? |
@staabm Yes, I'm aware that if you try to get at the return value you'll see |
@TazeTSchnitzel then sorry, I don't get your point. |
It's an internal difference only, I think ( Even if they effectively do the same thing, there's a difference in intentions. While |
Another way to think of it is that magic is bad: where did that NULL come from? It's not useful to say NULL was returned, because it's a NULL PHP has created. The function hasn't explicitly returned a value, so don't act as if it has. It's returned no value, and PHP has filled in a NULL for it. |
A function implicitly returns a NULL and caller explicitly receives NULL. That's my view on it. Period. It's fine if you disagree, but don't merge this. |
If it's an implicit NULL return, then you shouldn't produce the same message as an explicit NULL return. Also, the patch and RFC already make this distinction. This just clears up a minor erratum ( |
The RFC doesn't. (If I didn't miss anything in my quick glance now) The patch does, but shouldn't. Should be fixed in the patch. |
Why? It'd be inconsistent with parameters... |
With which parameters? |
@bwoebi There's a distinction between missing and NULL parameters |
@TazeTSchnitzel That's completely unrelated and not an inconsistency. |
Not really... |
It's ultimately a question of user-friendly error messages. Talking about |
Currently:
function foo(): array {}
produces the "must be an instance of int, none returned" errorfunction foo(): array { return; }
produces the "must be an instance of int, null returned" errorfunction foo(): array { return null; }
produces the "must be an instance of int, null returned" errorI think the 2nd case is wrong.
return;
doesn't return an explicit value, neither does omitting a return statement altogether, so it should say "none" and not "null" for that case.This patch fixes the 2nd case to produce "none", and adds tests.