Repository navigation
Check unreachable return statements when fixing a missing return type - #7323
Merged
Merged
Conversation
PHP checks every return statement against the return type, even an unreachable one, so a function that always throws and has return 'value'; after the throw cannot declare never. Amp-Thread-ID: https://ampcode.com/threads/T-01a24185-697f-75a6-9876-362595ee2d65
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PHP checks every return statement against the return type, even an unreachable one. The missing return type fixer only looked at the reachable return statements, so it produced invalid PHP:
throw ...; return 'some value';was fixed with: never("A never-returning function must not return"), now not fixedthrow ...; return;was fixed with: never, now: voidreturn 1; return;was fixed with: int, now not fixedThe fixer now takes whether the function returns with or without a value from all return statements in its body, skipping nested closures and classes. The returned value types still come from the reachable ones.
Reproducer: https://phpstan.org/r/6a066856-7c67-4fa4-b486-bbeefbbdd435
Amp thread: https://ampcode.com/threads/T-01a24185-697f-75a6-9876-362595ee2d65