Skip to content
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

[8.x] Fix the wrong of response::make() function's parameter type #39626

Merged
merged 3 commits into from
Nov 16, 2021
Merged

[8.x] Fix the wrong of response::make() function's parameter type #39626

merged 3 commits into from
Nov 16, 2021

Conversation

hmingv
Copy link
Contributor

@hmingv hmingv commented Nov 16, 2021

‘’‘
$content = [
'code' => 1
];
\Illuminate\Support\Facades\Response::make($content);
’‘’

The above syntax prompt is incorrect
Expected parameter of type 'string|void', 'array' provided

However, $content can actually pass array, but the annotation is wrong.

@@ -7,7 +7,7 @@ interface ResponseFactory
/**
* Create a new response instance.
*
* @param string $content
* @param mixed $content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. It's okay to widen a param type in the implementation. The facade change is okay though.

Copy link
Contributor Author

@hmingv hmingv Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to undo this change?
I think the scope of implementation classes should not exceed interfaces.

@driesvints
Copy link
Member

Actually, the facade change is incorrect as well as it's mixin the contract, not the concrete implementation. No change is needed here and this PR should be closed.

If you want to do what you want you should type-hint the concrete factory class or use new Response directly.

@hmingv
Copy link
Contributor Author

hmingv commented Nov 16, 2021

@driesvints

In fact, the $content parameter type of the make method of src/illuminate/routing/ResponseFactory.php is mixed

However, the method annotation in src/illuminate/support/facades/Response.php is of type string.

As you said, if the $content parameter type of src/illuminate/contracts/routing/ResponseFactory.php make method is string, but its implementation class is indeed mixed, don't you think this is wrong?

I think that if I call it successfully, the editor should not prompt an error.

So my point is to correct the wrong annotation.

@driesvints
Copy link
Member

In fact, the $content parameter type of the make method of src/illuminate/routing/ResponseFactory.php is mixed

However, the method annotation in src/illuminate/support/facades/Response.php is of type string.

Yes, you can widen parameter types in implementations. As I said before: the facade mixes the concrete, not the factory implementation so it should follow the types of the contract.

As you said, if the $content parameter type of src/illuminate/contracts/routing/ResponseFactory.php make method is string, but its implementation class is indeed mixed, don't you think this is wrong?

No, again: you can widen types in implementations: https://www.php.net/manual/en/migration72.new-features.php#migration72.new-features.param-type-widening

I think that if I call it successfully, the editor should not prompt an error.

That's because you're using the facade which is linked to the contract, not the factory class. If you need the factory class itself you should resolve that instead of using the facade.

We could update the Facade mixin to the concrete response class but that would be a breaking change and not something we can do in the current stable release.

@nunomaduro nunomaduro changed the title Fix the wrong of response::make() function's parameter type [8.x] Fix the wrong of response::make() function's parameter type Nov 16, 2021
@taylorotwell taylorotwell merged commit d68685c into laravel:8.x Nov 16, 2021
@taylorotwell
Copy link
Member

taylorotwell commented Nov 16, 2021

I don't see any practical harm in updating both of these to match the reality of how they are being used.

@hmingv hmingv deleted the repair branch November 17, 2021 03:10
@hmingv
Copy link
Contributor Author

hmingv commented Nov 17, 2021

I don't see any practical harm in updating both of these to match the reality of how they are being used.

Yes, this is just a Annotation.

Thanks, at the same time, thank @driesvints very much.

The information he mentioned was very helpful to me.

@driesvints
Copy link
Member

Yeah the merged solution is a good middle ground 👍

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.

3 participants