Skip to content

Commit

Permalink
Replace deprecated calls
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Nov 26, 2020
1 parent 30e6db0 commit f576113
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/ControllerValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Brick\Http\Request;
use Brick\Di\ValueResolver;
use Brick\Reflection\ReflectionTools;
use ReflectionNamedType;

/**
* Resolves controller values in the application.
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Plugin/ObjectUnpackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [];
Expand Down
15 changes: 8 additions & 7 deletions src/Plugin/OnBeforeAfterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down
18 changes: 15 additions & 3 deletions src/Plugin/RequestParamPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 === '') {
Expand Down

0 comments on commit f576113

Please sign in to comment.