Skip to content

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented Aug 18, 2025

Hello!

This PR adds native return types to the helper functions (where the return type would not be mixed).

Note

Functions cannot be extended so there should not be any breaks

Motivation

I've been using Rector to automatically upgrade / refactor my codebase---it has the ability to automatically add types, narrow too wide types, etc. However, in most cases it only uses native types to be completely safe during it's refactors---it skips over function calls like these that don't have native return types.

Adding actual return types like this will allow Rector (and other tools) to better understand the return type and be more safe.

Thanks!

@calebdw calebdw force-pushed the calebdw/push-xnproloxrpwm branch 3 times, most recently from bce1c6e to 95a2c2d Compare August 18, 2025 18:44
@calebdw calebdw force-pushed the calebdw/push-xnproloxrpwm branch from 95a2c2d to 2d9f068 Compare August 18, 2025 18:45
@taylorotwell taylorotwell merged commit 6d5dfad into laravel:12.x Aug 19, 2025
60 checks passed
@calebdw calebdw deleted the calebdw/push-xnproloxrpwm branch August 19, 2025 13:37
@rodrigopedra
Copy link
Contributor

Why did you change an unrelated test?

@calebdw
Copy link
Contributor Author

calebdw commented Aug 20, 2025

It's not an "unrelated" test---the test was failing with a type error because the helper function was returning a mocked stdClass instead of a mocked ViewFactory like it should have to begin with

@rodrigopedra
Copy link
Contributor

It is unrelated, as the PR title and description fail to mention it and only talk about fixing return types on helpers.

That should have been sent on a separate PR.

@calebdw
Copy link
Contributor Author

calebdw commented Aug 20, 2025

I disagree, but what's done is done

@browner12
Copy link
Contributor

could we also safely type the parameters here?

@calebdw
Copy link
Contributor Author

calebdw commented Aug 20, 2025

could we also safely type the parameters here?

That's less safe because the user could be passing in bad data (e.g., could result in Argument #1 ($foo) must be of type string, null given type errors)

@chris-ware
Copy link
Contributor

Interestingly, this seems to cause an issue with the csrf_token helper method. There appears to be some instances where it doesn't return a string, which now causes a fatal error due to it not being type matched.

@calebdw
Copy link
Contributor Author

calebdw commented Aug 26, 2025

let me take a look

browner12 added a commit to browner12/framework that referenced this pull request Aug 26, 2025
this fully reverts the change from laravel#56684 that was partially reverted in laravel#56773 by adding back the `@return` docblock.

clearly this docblock isn't the whole story, but I think it's good to have it back until we fully figure out what the accurate answer is here.
taylorotwell pushed a commit that referenced this pull request Aug 26, 2025
this fully reverts the change from #56684 that was partially reverted in #56773 by adding back the `@return` docblock.

clearly this docblock isn't the whole story, but I think it's good to have it back until we fully figure out what the accurate answer is here.
@shaedrich
Copy link
Contributor

shaedrich commented Sep 29, 2025

Functions cannot be extended so there should not be any breaks

Unfortunately, this creates a bug when you do, for example:

return response(null, Response::HTTP_INTERNAL_SERVER_ERROR);

which results in "Expected type 'Symfony\Component\HttpFoundation\Response'. Found 'Illuminate\Contracts\Routing\ResponseFactory'" which is not true, since the condition is not

* @return ($content is null ? \Illuminate\Contracts\Routing\ResponseFactory : \Illuminate\Http\Response)

as hinted in the PHPStan conditional return types, but

if (func_num_args() === 0) {

in runtime code, which is not the same when you pass null for the first parameter but something as the second.

@calebdw
Copy link
Contributor Author

calebdw commented Sep 29, 2025

That is a phpstan limitation, not a runtime error, can just ignore

@shaedrich
Copy link
Contributor

Yeah, it's not a runtime error, sure, but it's not PHPStan's fault. Granted, PHPStan can't do func_num_args() === 0 in conditional returns, but the conditional return that this PR introduced is factually wrong. It doesn't reflect the runtime code accurately.

@calebdw
Copy link
Contributor Author

calebdw commented Sep 29, 2025

See phpstan/phpstan#11245 for reference, this is "good enough" to detect the correct return type when you call response(). You can just as call response([], Response::HTTP_INTERNAL_SERVER_ERROR) or even abort(500) instead to prevent the warning

@shaedrich
Copy link
Contributor

If you insist on the PHPDoc, you should change the runtime code as it allows for something you would not.

@calebdw
Copy link
Contributor Author

calebdw commented Sep 29, 2025

No, this PR did not touch the conditional return and the conditional return is good enough for most cases.

Larastan already has an extension for this helper. The fact that you're getting this warning indicates that you're not using it.

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.

6 participants