Skip to content

use proper return types in ErrorHandler and ArgumentResolver #32143

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

Merged
merged 1 commit into from
Jun 26, 2019

Conversation

Tobion
Copy link
Contributor

@Tobion Tobion commented Jun 24, 2019

Q A
Branch? 4.4
Bug fix? tiny
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Found those things while reviewing #31996 which missed some return types due to using return instead of return null.
It's part of fixing #17201 (due to #10717). See also #30869 that somebody was stumbling over.

return $name;
}
if (!$function instanceof \ReflectionMethod) {
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

you cannot have a function using self or parent. so this case cannot happen. I refactored the code to make more sense.

@@ -55,14 +55,16 @@ public function getArguments(Request $request, $controller)

$resolved = $resolver->resolve($request, $metadata);

if (!$resolved instanceof \Generator) {
throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', \get_class($resolver)));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

checking for Generator does not achieve the intended goal: having at least one yielded value.
I can do return; yield $value; which would return a generator but not yield anything.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a return type on the interface, so this would allow non-iterable return types as well. Perhaps the interface should receive a return type to enforce this (which is \Generator at the moment, iterable after this PR) in 5.0? Could do with a typecheck on $resolved

Another solution would be to allow a return $value in the resolvers as generators are pretty much only required for an array structure being inserted as multiple arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are several different topics:

  1. adding real return types: I would also prefer this. But not sure if we will do that in SF because it's a hard break for all implementations (even with return type variance in PHP 7.4). So that is a different topic.
  2. Validating returned type: Could make sense. But normally we do not do that for every method as we would just need to check types everywhere. So if you return something non-iterable and violate the interface, the code will just break. No real need to validate that.
  3. Changing the return type: I also think instead of forcing a iterable/Generator everywhere, it should allow only returning a single value (which is the common need). For variadic arguments, there could just be an explicit VariadicArgumentValue class that you can return to resolve to several arguments. I think that's more clear and clean. Currently you can yield several values for non-variadic arguments which makes the ArgumentResolver behave very strange. But that's also a different topic. If you argree with something like this, feel free to create an issue/PR to keep track of that.

@Tobion Tobion force-pushed the fix-minor-return-types branch from 00fda63 to 2f9121b Compare June 24, 2019 01:05
@xabbuh xabbuh added this to the next milestone Jun 24, 2019
return $parent->name;
if ($function instanceof \ReflectionMethod) {
$lcName = strtolower($name);
switch ($lcName) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest to inline the strtolower call because we don't need that value anywhere else.

Suggested change
switch ($lcName) {
switch (strtolower($name)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes seems fine, I just kept the variable because it was there before making merging easier.

@fabpot
Copy link
Member

fabpot commented Jun 26, 2019

Thank you @Tobion.

@fabpot fabpot merged commit 2f9121b into symfony:4.4 Jun 26, 2019
fabpot added a commit that referenced this pull request Jun 26, 2019
…lver (Tobion)

This PR was merged into the 4.4 branch.

Discussion
----------

use proper return types in ErrorHandler and ArgumentResolver

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | tiny
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        |

Found those things while reviewing #31996 which missed some return types due to using `return` instead of `return null`.
It's part of fixing #17201 (due to #10717). See also #30869 that somebody was stumbling over.

Commits
-------

2f9121b use proper return types in ErrorHandler and ArgumentResolver
@Tobion Tobion deleted the fix-minor-return-types branch June 26, 2019 09:52
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants