Skip to content

Check unreachable return statements when fixing a missing return type - #7323

Merged
ondrejmirtes merged 1 commit into
2.4.xfrom
missing-return-type-unreachable-returns
Dec 4, 2026
Merged

ondrejmirtes merged 1 commit into
2.4.xfrom
missing-return-type-unreachable-returns

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

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 fixed
  • throw ...; return; was fixed with : never, now : void
  • return 1; return; was fixed with : int, now not fixed

The 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

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
@ondrejmirtes
ondrejmirtes merged commit 65b9ac9 into 2.4.x Dec 4, 2026
131 of 141 checks passed
@ondrejmirtes
ondrejmirtes deleted the missing-return-type-unreachable-returns branch December 4, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant