use proper return types in ErrorHandler and ArgumentResolver

This commit is contained in:
Tobias Schultze 2019-06-24 01:24:36 +01:00
parent aa4385dc12
commit 2f9121b74d
6 changed files with 23 additions and 34 deletions

View File

@ -369,18 +369,13 @@ class ErrorHandler
/** /**
* Handles errors by filtering then logging them according to the configured bit fields. * 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 * @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 * @throws \ErrorException When $this->thrownErrors requests so
* *
* @internal * @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 // @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)) { 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)) {
@ -443,7 +438,7 @@ class ErrorHandler
self::$silencedErrorCache[$id][$message] = $errorAsException; self::$silencedErrorCache[$id][$message] = $errorAsException;
} }
if (null === $lightTrace) { if (null === $lightTrace) {
return; return true;
} }
} else { } else {
$errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line); $errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);

View File

@ -55,12 +55,14 @@ final class ArgumentResolver implements ArgumentResolverInterface
$resolved = $resolver->resolve($request, $metadata); $resolved = $resolver->resolve($request, $metadata);
if (!$resolved instanceof \Generator) { $atLeastOne = false;
throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', \get_class($resolver))); foreach ($resolved as $append) {
$atLeastOne = true;
$arguments[] = $append;
} }
foreach ($resolved as $append) { if (!$atLeastOne) {
$arguments[] = $append; throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', \get_class($resolver)));
} }
// continue to the next controller argument // continue to the next controller argument

View File

@ -41,8 +41,6 @@ final class VariadicValueResolver implements ArgumentValueResolverInterface
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))); 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 from $values;
yield $value;
}
} }
} }

View File

@ -37,7 +37,7 @@ interface ArgumentValueResolverInterface
* @param Request $request * @param Request $request
* @param ArgumentMetadata $argument * @param ArgumentMetadata $argument
* *
* @return \Generator * @return iterable
*/ */
public function resolve(Request $request, ArgumentMetadata $argument); public function resolve(Request $request, ArgumentMetadata $argument);
} }

View File

@ -42,30 +42,24 @@ final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
/** /**
* Returns an associated type to the given parameter if available. * 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()) { if (!$type = $parameter->getType()) {
return; return null;
} }
$name = $type->getName(); $name = $type->getName();
$lcName = strtolower($name);
if ('self' !== $lcName && 'parent' !== $lcName) { if ($function instanceof \ReflectionMethod) {
return $name; $lcName = strtolower($name);
} switch ($lcName) {
if (!$function instanceof \ReflectionMethod) { case 'self':
return; return $function->getDeclaringClass()->name;
} case 'parent':
if ('self' === $lcName) { return ($parent = $function->getDeclaringClass()->getParentClass()) ? $parent->name : null;
return $function->getDeclaringClass()->name; }
}
if ($parent = $function->getDeclaringClass()->getParentClass()) {
return $parent->name;
} }
return $name;
} }
} }

View File

@ -185,7 +185,7 @@ class ArgumentResolverTest extends TestCase
$resolver = new ArgumentResolver($factory, [$valueResolver]); $resolver = new ArgumentResolver($factory, [$valueResolver]);
$valueResolver->expects($this->any())->method('supports')->willReturn(true); $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 = Request::create('/');
$request->attributes->set('foo', 'foo'); $request->attributes->set('foo', 'foo');