Skip to content

Address deprecation of ReflectionType::getClass() #36891

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
May 23, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,16 @@ private static function getResourceMetadataForMethod(\ReflectionMethod $method)
$methodArgumentsMetadata = [];
foreach ($method->getParameters() as $parameter) {
try {
$class = $parameter->getClass();
if (method_exists($parameter, 'getType')) {
$type = $parameter->getType();
if ($type && !$type->isBuiltin()) {
$class = new \ReflectionClass(method_exists($type, 'getName') ? $type->getName() : (string) $type);
} else {
$class = null;
}
} else {
$class = $parameter->getClass();
}
} catch (\ReflectionException $e) {
// type-hint is against a non-existent class
$class = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function doGetArguments(Request $request, $controller, array $paramete
} else {
$arguments[] = $attributes[$param->name];
}
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
} elseif ($this->typeMatchesRequestClass($param, $request)) {
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
$arguments[] = $param->getDefaultValue();
Expand Down Expand Up @@ -260,4 +260,22 @@ private function getControllerError($callable)

return $message;
}

/**
* @return bool
*/
private function typeMatchesRequestClass(\ReflectionParameter $param, Request $request)
{
if (!method_exists($param, 'getType')) {
return $param->getClass() && $param->getClass()->isInstance($request);
}

if (!($type = $param->getType()) || $type->isBuiltin()) {
return false;
}

$class = new \ReflectionClass(method_exists($type, 'getName') ? $type->getName() : (string) $type);

return $class && $class->isInstance($request);
}
}
18 changes: 17 additions & 1 deletion src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function setDefault($option, $value)
$reflClosure = new \ReflectionFunction($value);
$params = $reflClosure->getParameters();

if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && Options::class === $class->name) {
if (isset($params[0]) && Options::class === $this->getParameterClassName($params[0])) {
// Initialize the option if no previous value exists
if (!isset($this->defaults[$option])) {
$this->defaults[$option] = null;
Expand Down Expand Up @@ -1066,4 +1066,20 @@ private static function isValueValidType($type, $value)
{
return (\function_exists($isFunction = 'is_'.$type) && $isFunction($value)) || $value instanceof $type;
}

/**
* @return string|null
*/
private function getParameterClassName(\ReflectionParameter $parameter)
{
if (!method_exists($parameter, 'getType')) {
return ($class = $parameter->getClass()) ? $class->name : null;
}

if (!($type = $parameter->getType()) || $type->isBuiltin()) {
return null;
}

return method_exists($type, 'getName') ? $type->getName() : (string) $type;
}
}