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
Propose never
type
#1191
Propose never
type
#1191
Conversation
This type should be used mainly as a return type to designate a function/method as a never returning back. It shows that the function/method will always throw an exception or will call `exit()` or will call `trigger_error()` or will stop program execution in any other available way. This return type is available in many languages/tools: 1. Kotlin — `Nothing` return type. See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-nothing.html 2. Hack lang — `noreturn` return type. See https://docs.hhvm.com/hack/built-in-types/noreturn (examples: https://github.com/facebookarchive/hack-langspec/blob/master/tests/Functions/noreturn.php) 3. Vimeo Psalm — `@return no-return` PhpDoc type. See vimeo/psalm#1155 (comment) + they have config option, but it's only for functions/static methods and is going to be deprecated/removed in the future. 4. Phpstan has @return never PhpDoc type. See phpstan/phpstan#1472 + they have config option and it's possible to specify dynamic method calls there too. 5. Phan doesn't have this — see phan/phan#2118, but they plan to add it. 6. PhpStorm doesn't have this feature, but they plan to add it and the PhpDoc tag is not decided yet. See https://youtrack.jetbrains.com/issue/WI-10673
|
I would personally prefer |
Update: upcoming PhpStorm 2020 version will have support for exit points via meta files: https://youtrack.jetbrains.com/issue/WI-10673#focus=streamItem-27-3775062.0-0 |
Technically, PHP's (and Java/C like langauges) |
Coming from a C/C++ standpoint, void functions cannot return anything. That part is correct. However, void functions are expected to exit normally. Hence and as was mentioned above, signifying this with "void" will be very confusing. |
Well, formally, they do. From a formal standpoint, the only way to return nothing is to non-terminate, or kill the process, or do something else funky with control, like throwing an exception. |
To clarify, since there seems to be some confusion on this point, I believe the proposal is for "this method goes into an infinite loop, by design"; That's different from "this method returns nothing", which is already fully covered by @b1rdex Is that correct? |
Yes, it is. Every link to other languages mentioned here has this statement: such return type indicates that the method will exit program or throw exception always, so it'll never return or continue normal execution of a program. |
I'd say
An alternative could be to instead add a new tag; e.g., "@never-returns". |
I like the idea of @JanTvrdik , I assume this is still valid in PHPStan? @muglug , would it be too burdensome to deprecate Then again, we could just leave it out of the spec, as a customization that's unstandardized in the tools. |
If this is included, I would recommend either:
or
I don't have a strong feeling on which one is better. |
I prefer |
|
|
@rob006 It's exactly as ambiguous as most other keywords. You can have class named |
@JanTvrdik Now you can't have class |
Why would you invent a new thing while there is already a way to annotate it that is supported/used by popular SCA tools? Also, |
There is now a RFC to get something like this into the core: php/php-src#6761 |
Let’s wait for the vote end. There’s a poll between noreturn and never.
Отправлено с iPhone
… 31 марта 2021 г., в 00:09, Ben Edmunds ***@***.***> написал(а):
@benedmunds commented on this pull request.
In proposed/phpdoc.md:
> @@ -659,6 +659,9 @@ The following keywords are recognized by this PSR:
This type is often used as return value for methods implementing the [Fluent Interface][FLUENT] design pattern.
+17. `no-return`: denotes that element isn't going to return anything and always throws exception or terminates
⬇️ Suggested change
-17. `no-return`: denotes that element isn't going to return anything and always throws exception or terminates
+17. `noreturn`: denotes that element isn't going to return anything and always throws exception or terminates
Rename to match the noreturn_type PHP RFC
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meanwhile, the vote ended in favor of the never
return type, and the implementation was merged in php/php-src@6cd0b48.
Accordingly, no-return
should be renamed to never
in this PR.
Co-authored-by: Claas Augner <github@caugner.de>
@b1rdex Would you mind renaming |
|
This type should be used mainly as a return type to designate a function/method as a never returning back.
It shows that the function/method will always throw an exception or will call
exit()
or will calltrigger_error()
or will stop program execution in any other available way.I'm not a native speaker, so it probably needs some description extension/correction.
This return type is available in many languages/tools:
Nothing
return type. See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-nothing.htmlnoreturn
return type. See https://docs.hhvm.com/hack/built-in-types/noreturn (examples: https://github.com/facebookarchive/hack-langspec/blob/master/tests/Functions/noreturn.php)@return no-return
PhpDoc type. See Add @return no-return to denote a function that never returns vimeo/psalm#1155 (comment)Also, there is a config option, but it's only for functions/static methods and is going to be deprecated/removed in the future.
@return never
PhpDoc type. See support type never in phpDoc phpstan/phpstan#1472Also, there is a config option and it's possible to specify dynamic method calls there too.