8000 use proper return types in ErrorHandler and ArgumentResolver by Tobion · Pull Request #32143 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use proper return types in ErrorHandler and ArgumentResolver
  • Loading branch information
Tobion committed Jun 24, 2019
commit 2f9121b74dcb9fbce8e1a15b55123341fa887952
9 changes: 2 additions & 7 deletions src/Symfony/Component/Debug/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,13 @@ private function reRegister($prev)
/**
* Handles errors by filtering then logging them according to the configured bit fields.
*
* @param int $type One of the E_* constants
* @param string $message
* @param string $file
* @param int $line
*
* @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
*
* @throws \ErrorException When $this->thrownErrors requests so
*
* @internal
*/
public function handleError($type, $message, $file, $line)
public function handleError(int $type, string $message, string $file, int $line): bool
{
// @deprecated to be removed in Symfony 5.0
if (\PHP_VERSION_ID >= 70300 && $message && '"' === $message[0] && 0 === strpos($message, '"continue') && preg_match('/^"continue(?: \d++)?" targeting switch is equivalent to "break(?: \d++)?"\. Did you mean to use "continue(?: \d++)?"\?$/', $message)) {
Expand Down Expand Up @@ -443,7 +438,7 @@ public function handleError($type, $message, $file, $line)
self::$silencedErrorCache[$id][$message] = $errorAsException;
}
if (null === $lightTrace) {
return;
return true;
}
} else {
$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Component/HttpKernel/Controller/ArgumentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}

$atLeastOne = false;
foreach ($resolved as $append) {
$atLeastOne = true;
$arguments[] = $append;
}

if (!$atLeastOne) {
throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', \get_class($resolver)));
}

// continue to the next controller argument
continue 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public function resolve(Request $request, ArgumentMetadata $argument)
throw new \InvalidArgumentException(sprintf('The action argument "...$%1$s" is required to be an array, the request attribute "%1$s" contains a type of "%2$s" instead.', $argument->getName(), \gettype($values)));
}

foreach ($values as $value) {
yield $value;
}
yield from $values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function supports(Request $request, ArgumentMetadata $argument);
* @param Request $request
* @param ArgumentMetadata $argument
*
* @return \Generator
* @return iterable
*/
public function resolve(Request $request, ArgumentMetadata $argument);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,24 @@ public function createArgumentMetadata($controller)

/**
* Returns an associated type to the given parameter if available.
*
* @param \ReflectionParameter $parameter
*
* @return string|null
*/
private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function)
private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function): ?string
{
if (!$type = $parameter->getType()) {
return;
return null;
}
$name = $type->getName();
$lcName = strtolower($name);

if ('self' !== $lcName && 'parent' !== $lcName) {
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.

}
if ('self' === $lcName) {
return $function->getDeclaringClass()->name;
}
if ($parent = $function->getDeclaringClass()->getParentClass()) {
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.

case 'self':
return $function->getDeclaringClass()->name;
case 'parent':
return ($parent = $function->getDeclaringClass()->getParentClass()) ? $parent->name : null;
}
}

return $name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function testGetArgumentWithoutArray()
$resolver = new ArgumentResolver($factory, [$valueResolver]);

$valueResolver->expects($this->any())->method('supports')->willReturn(true);
$valueResolver->expects($this->any())->method('resolve')->willReturn('foo');
$valueResolver->expects($this->any())->method('resolve')->willReturn([]);

$request = Request::create('/');
$request->attributes->set('foo', 'foo');
Expand Down
0