minor #18025 [2.7] Don't use reflection when possible (Ener-Getick)
This PR was merged into the 2.7 branch.
Discussion
----------
[2.7] Don't use reflection when possible
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
See https://github.com/symfony/symfony/pull/18021
Commits
-------
35be501
Don't use reflections when possible
This commit is contained in:
commit
a29e818a3f
@ -37,9 +37,8 @@ class FragmentRendererPass implements CompilerPassInterface
|
|||||||
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
||||||
$class = $container->getDefinition($id)->getClass();
|
$class = $container->getDefinition($id)->getClass();
|
||||||
|
|
||||||
$refClass = new \ReflectionClass($class);
|
|
||||||
$interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
|
$interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
|
||||||
if (!$refClass->implementsInterface($interface)) {
|
if (!is_subclass_of($class, $interface)) {
|
||||||
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +36,7 @@ class LoggingTranslatorPass implements CompilerPassInterface
|
|||||||
$definition = $container->getDefinition((string) $translatorAlias);
|
$definition = $container->getDefinition((string) $translatorAlias);
|
||||||
$class = $container->getParameterBag()->resolveValue($definition->getClass());
|
$class = $container->getParameterBag()->resolveValue($definition->getClass());
|
||||||
|
|
||||||
$refClass = new \ReflectionClass($class);
|
if (is_subclass_of($class, 'Symfony\Component\Translation\TranslatorInterface') && is_subclass_of($class, 'Symfony\Component\Translation\TranslatorBagInterface')) {
|
||||||
if ($refClass->implementsInterface('Symfony\Component\Translation\TranslatorInterface') && $refClass->implementsInterface('Symfony\Component\Translation\TranslatorBagInterface')) {
|
|
||||||
$container->getDefinition('translator.logging')->setDecoratedService('translator');
|
$container->getDefinition('translator.logging')->setDecoratedService('translator');
|
||||||
$container->getDefinition('translation.warmer')->replaceArgument(0, new Reference('translator.logging.inner'));
|
$container->getDefinition('translation.warmer')->replaceArgument(0, new Reference('translator.logging.inner'));
|
||||||
}
|
}
|
||||||
|
@ -193,16 +193,16 @@ class DebugClassLoader
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$parent = $refl->getParentClass();
|
$parent = get_parent_class($class);
|
||||||
|
|
||||||
if (!$parent || strncmp($ns, $parent->name, $len)) {
|
if (!$parent || strncmp($ns, $parent, $len)) {
|
||||||
if ($parent && isset(self::$deprecated[$parent->name]) && strncmp($ns, $parent->name, $len)) {
|
if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
|
||||||
@trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent->name, self::$deprecated[$parent->name]), E_USER_DEPRECATED);
|
@trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($refl->getInterfaceNames() as $interface) {
|
foreach (class_implements($class) as $interface) {
|
||||||
if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !($parent && $parent->implementsInterface($interface))) {
|
if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !is_subclass_of($parent, $interface)) {
|
||||||
@trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
|
@trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, interface_exists($class) ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,9 +98,8 @@ class RegisterListenersPass implements CompilerPassInterface
|
|||||||
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
||||||
$class = $container->getParameterBag()->resolveValue($def->getClass());
|
$class = $container->getParameterBag()->resolveValue($def->getClass());
|
||||||
|
|
||||||
$refClass = new \ReflectionClass($class);
|
|
||||||
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
|
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
|
||||||
if (!$refClass->implementsInterface($interface)) {
|
if (!is_subclass_of($class, $interface))) {
|
||||||
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,9 +52,9 @@ class FragmentRendererPass implements CompilerPassInterface
|
|||||||
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
|
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
$refClass = new \ReflectionClass($container->getParameterBag()->resolveValue($def->getClass()));
|
$class = $container->getParameterBag()->resolveValue($def->getClass());
|
||||||
$interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
|
$interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
|
||||||
if (!$refClass->implementsInterface($interface)) {
|
if (!is_subclass_of($class, $interface)) {
|
||||||
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user