diff --git a/src/ControllerValueResolver.php b/src/ControllerValueResolver.php index 7868e90..0f10b9c 100644 --- a/src/ControllerValueResolver.php +++ b/src/ControllerValueResolver.php @@ -7,6 +7,7 @@ use Brick\Http\Request; use Brick\Di\ValueResolver; use Brick\Reflection\ReflectionTools; +use ReflectionNamedType; /** * Resolves controller values in the application. @@ -54,8 +55,9 @@ public function setRequest(Request $request) : void */ public function getParameterValue(\ReflectionParameter $parameter) { - $class = $parameter->getClass(); - if ($class && $class->getName() === Request::class) { + $type = $parameter->getType(); + + if ($type instanceof ReflectionNamedType && ! $type->allowsNull() && $type->getName() === Request::class) { return $this->request; } diff --git a/src/Plugin/ObjectUnpackPlugin.php b/src/Plugin/ObjectUnpackPlugin.php index 285dde3..b874615 100644 --- a/src/Plugin/ObjectUnpackPlugin.php +++ b/src/Plugin/ObjectUnpackPlugin.php @@ -15,6 +15,7 @@ use Brick\Http\Exception\HttpNotFoundException; use Brick\Http\Exception\HttpBadRequestException; use Brick\Http\Exception\HttpInternalServerErrorException; +use ReflectionNamedType; /** * Automatically converts type-hinted objects in controller parameters, from their string or array representation. @@ -82,10 +83,10 @@ private function getParameters(ControllerReadyEvent $event) : array */ private function getParameterValue(\ReflectionParameter $parameter, $value) { - $class = $parameter->getClass(); + $type = $parameter->getType(); - if ($class) { - $className = $class->getName(); + if ($type instanceof ReflectionNamedType && ! $type->isBuiltin()) { + $className = $type->getName(); if ($parameter->isVariadic()) { $result = []; diff --git a/src/Plugin/OnBeforeAfterPlugin.php b/src/Plugin/OnBeforeAfterPlugin.php index 4f229e5..560358a 100644 --- a/src/Plugin/OnBeforeAfterPlugin.php +++ b/src/Plugin/OnBeforeAfterPlugin.php @@ -12,6 +12,7 @@ use Brick\Http\Request; use Brick\Http\Response; use Brick\Reflection\ReflectionTools; +use ReflectionNamedType; /** * Registers functions to be called before and after controller method invocation. @@ -134,10 +135,10 @@ private function getFunctionParameters(string $onEvent, callable $function, arra $reflectionFunction = $this->reflectionTools->getReflectionFunction($function); foreach ($reflectionFunction->getParameters() as $reflectionParameter) { - $parameterClass = $reflectionParameter->getClass(); + $parameterType = $reflectionParameter->getType(); - if ($parameterClass !== null) { - $parameterClassName = $parameterClass->getName(); + if ($parameterType instanceof ReflectionNamedType && ! $parameterType->isBuiltin()) { + $parameterClassName = $parameterType->getName(); if (isset($objects[$parameterClassName])) { $parameters[] = $objects[$parameterClassName]; @@ -163,12 +164,12 @@ private function cannotResolveParameter(string $onEvent, array $types, \Reflecti { $message = 'Cannot resolve ' . $onEvent . ' function parameter $' . $parameter->getName() . ': '; - $parameterClass = $parameter->getClass(); + $parameterType = $parameter->getType(); - if ($parameterClass === null) { + if ($parameterType === null) { $message .= 'parameter is not typed.'; - } else { - $message .= 'type ' . $parameterClass->getName() . ' is not in available types (' . implode(', ', $types) . ').'; + } elseif ($parameterType instanceof ReflectionNamedType) { + $message .= 'type ' . $parameterType->getName() . ' is not in available types (' . implode(', ', $types) . ').'; } return new HttpInternalServerErrorException($message); diff --git a/src/Plugin/RequestParamPlugin.php b/src/Plugin/RequestParamPlugin.php index 6f3d131..bf1473b 100644 --- a/src/Plugin/RequestParamPlugin.php +++ b/src/Plugin/RequestParamPlugin.php @@ -11,6 +11,7 @@ use Brick\Http\Request; use Brick\Http\Exception\HttpBadRequestException; use Brick\Http\Exception\HttpInternalServerErrorException; +use ReflectionNamedType; /** * Injects request parameters into controllers with the QueryParam and PostParam annotations. @@ -108,15 +109,21 @@ private function getParameter(RequestParam $annotation, \ReflectionFunctionAbstr $value = $requestParameters[$parameterName]; - if ($value === '' && $parameter->getClass() && $parameter->allowsNull()) { + $parameterType = $parameter->getType(); + + if ($value === '' && $parameterType instanceof ReflectionNamedType && ! $parameterType->isBuiltin() && $parameter->allowsNull()) { return null; } - if ($parameter->isArray() && ! is_array($value)) { + $isArray = $parameterType instanceof ReflectionNamedType && $parameterType->getName() === 'array'; + + if ($isArray && ! is_array($value)) { throw $this->invalidArrayParameterException($controller, $annotation); } - if ($parameter->isArray() || $parameter->getClass()) { + $hasClass = $parameterType instanceof ReflectionNamedType && ! $parameterType->isBuiltin(); + + if ($isArray || $hasClass) { return $value; } @@ -126,6 +133,11 @@ private function getParameter(RequestParam $annotation, \ReflectionFunctionAbstr return $value; } + if (! $type instanceof ReflectionNamedType) { + // @todo handle union types + return $value; + } + $type = $type->getName(); if ($value === '') {