Issue #3156542 by alexpott, voleger, andypost, Charlie ChX Negyesi, Gábor Hojtsy, longwave: \ReflectionParameter::getClass() is deprecated in PHP 8.0
parent
d8a3cf98f5
commit
cb96a97be3
|
@ -69,7 +69,7 @@ class ArgumentsResolver implements ArgumentsResolverInterface {
|
|||
* Thrown when there is a missing parameter.
|
||||
*/
|
||||
protected function getArgument(\ReflectionParameter $parameter) {
|
||||
$parameter_type_hint = $parameter->getClass();
|
||||
$parameter_type_hint = Reflection::getParameterClassName($parameter);
|
||||
$parameter_name = $parameter->getName();
|
||||
|
||||
// If the argument exists and is NULL, return it, regardless of
|
||||
|
@ -79,6 +79,7 @@ class ArgumentsResolver implements ArgumentsResolverInterface {
|
|||
}
|
||||
|
||||
if ($parameter_type_hint) {
|
||||
$parameter_type_hint = new \ReflectionClass($parameter_type_hint);
|
||||
// If the argument exists and complies with the type hint, return it.
|
||||
if (isset($this->objects[$parameter_name]) && is_object($this->objects[$parameter_name]) && $parameter_type_hint->isInstance($this->objects[$parameter_name])) {
|
||||
return $this->objects[$parameter_name];
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Component\Utility;
|
||||
|
||||
/**
|
||||
* Provides helper methods for reflection.
|
||||
*/
|
||||
final class Reflection {
|
||||
|
||||
/**
|
||||
* Gets the parameter's class name.
|
||||
*
|
||||
* @param \ReflectionParameter $parameter
|
||||
* The parameter.
|
||||
*
|
||||
* @return string|null
|
||||
* The parameter's class name or NULL if the parameter is not a class.
|
||||
*/
|
||||
public static function getParameterClassName(\ReflectionParameter $parameter) : ?string {
|
||||
$name = NULL;
|
||||
if ($parameter->hasType() && !$parameter->getType()->isBuiltin()) {
|
||||
$name = $parameter->getType()->getName();
|
||||
$lc_name = strtolower($name);
|
||||
switch ($lc_name) {
|
||||
case 'self':
|
||||
return $parameter->getDeclaringClass()->getName();
|
||||
|
||||
case 'parent':
|
||||
return ($parent = $parameter->getDeclaringClass()->getParentClass()) ? $parent->name : NULL;
|
||||
}
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\Core\DependencyInjection\Compiler;
|
||||
|
||||
use Drupal\Component\Utility\Reflection;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\LogicException;
|
||||
|
@ -130,8 +131,9 @@ class TaggedHandlersPass implements CompilerPassInterface {
|
|||
$priority_pos = NULL;
|
||||
$extra_params = [];
|
||||
foreach ($params as $pos => $param) {
|
||||
if ($param->getClass()) {
|
||||
$interface = $param->getClass();
|
||||
$class = Reflection::getParameterClassName($param);
|
||||
if ($class !== NULL) {
|
||||
$interface = $class;
|
||||
}
|
||||
elseif ($param->getName() === 'id') {
|
||||
$id_pos = $pos;
|
||||
|
@ -152,7 +154,6 @@ class TaggedHandlersPass implements CompilerPassInterface {
|
|||
$method_name,
|
||||
]));
|
||||
}
|
||||
$interface = $interface->getName();
|
||||
|
||||
// Find all tagged handlers.
|
||||
$handlers = [];
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\Reflection;
|
||||
use Drupal\Core\DependencyInjection\ClassResolverInterface;
|
||||
use Symfony\Component\Routing\Route;
|
||||
|
||||
|
@ -139,7 +140,8 @@ class EntityResolverManager {
|
|||
if (isset($entity_types[$parameter_name])) {
|
||||
$entity_type = $entity_types[$parameter_name];
|
||||
$entity_class = $entity_type->getClass();
|
||||
if (($reflection_class = $parameter->getClass()) && (is_subclass_of($entity_class, $reflection_class->name) || $entity_class == $reflection_class->name)) {
|
||||
$reflection_class = Reflection::getParameterClassName($parameter);
|
||||
if ($reflection_class && (is_subclass_of($entity_class, $reflection_class) || $entity_class == $reflection_class)) {
|
||||
$parameter_definitions += [$parameter_name => []];
|
||||
$parameter_definitions[$parameter_name] += [
|
||||
'type' => 'entity:' . $parameter_name,
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Component\Utility;
|
||||
|
||||
use Drupal\Component\Utility\Reflection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Component\Utility\Reflection
|
||||
* @group Utility
|
||||
*/
|
||||
class ReflectionTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @param string|null $expected
|
||||
* @param \ReflectionParameter $parameter
|
||||
*
|
||||
* @covers ::getParameterClassName
|
||||
* @dataProvider providerGetParameterClassName
|
||||
*/
|
||||
public function testGetParameterClassName(?string $expected, \ReflectionParameter $parameter) {
|
||||
$this->assertEquals($expected, Reflection::getParameterClassName($parameter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for ::testGetParameterClassName().
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function providerGetParameterClassName() {
|
||||
$reflection_method = new \ReflectionMethod(static::class, 'existsForTesting');
|
||||
$parameters = $reflection_method->getParameters();
|
||||
return [
|
||||
'string' => [NULL, $parameters[0]],
|
||||
'array' => [NULL, $parameters[1]],
|
||||
'same class' => ['Drupal\Tests\Component\Utility\ReflectionTest', $parameters[2]],
|
||||
'class' => ['Drupal\Component\Utility\Reflection', $parameters[3]],
|
||||
'parent' => ['PHPUnit\Framework\TestCase', $parameters[4]],
|
||||
'self' => ['Drupal\Tests\Component\Utility\ReflectionTest', $parameters[5]],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* This method exists for reflection testing only.
|
||||
*
|
||||
* Note the capital P in Parent is intentional and for testing purposes.
|
||||
*/
|
||||
// phpcs:disable Generic.PHP.LowerCaseKeyword.Found
|
||||
protected function existsForTesting(string $string, array $array, ReflectionTest $test, Reflection $reflection, Parent $parent, self $self) {
|
||||
}
|
||||
|
||||
// phpcs:enable
|
||||
|
||||
}
|
Loading…
Reference in New Issue