From 49db9da2512d4ece2dbbcf8175ae78fe1ef2f8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Tue, 16 Jan 2024 15:11:46 -0500 Subject: [PATCH] [All] fix deprecations --- .../core/Reflection/ReflectionExtractor.php | 32 +++++++++++++++++++ .../Compiler/EmailWriterCompilerPass.php | 30 ++++------------- packages/mailer/EmailComposer.php | 10 ++++-- .../Symfony/RouterRootSchemaExtractor.php | 8 ++++- .../Handler/ObjectReferenceHandler.php | 2 +- packages/tester/Http/Cookie/Cookie.php | 2 +- phpunit.xml.dist | 1 + 7 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 packages/core/Reflection/ReflectionExtractor.php diff --git a/packages/core/Reflection/ReflectionExtractor.php b/packages/core/Reflection/ReflectionExtractor.php new file mode 100644 index 000000000..479382ee4 --- /dev/null +++ b/packages/core/Reflection/ReflectionExtractor.php @@ -0,0 +1,32 @@ +getName()]; + } + + if ($reflectionType instanceof \ReflectionUnionType) { + $classes = []; + foreach ($reflectionType->getTypes() as $type) { + $classes = array_merge($classes, static::getClasses($type)); + } + + return array_values(array_unique($classes)); + } + + throw new \InvalidArgumentException('Unknown type '.$reflectionType::class); + } +} diff --git a/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php b/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php index 44b0e646e..80488c2d0 100644 --- a/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php +++ b/packages/framework-extra-bundle/DependencyInjection/Compiler/EmailWriterCompilerPass.php @@ -3,6 +3,7 @@ namespace Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler; use Draw\Component\Core\Reflection\ReflectionAccessor; +use Draw\Component\Core\Reflection\ReflectionExtractor; use Draw\Component\Mailer\EmailComposer; use Draw\Component\Mailer\EmailWriter\EmailWriterInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; @@ -32,7 +33,11 @@ public function process(ContainerBuilder $container): void $priority = 0; } - foreach ($this->getClasses((new \ReflectionMethod($class, $methodName))->getParameters()[0]) as $emailType) { + $emailTypes = ReflectionExtractor::getClasses( + (new \ReflectionMethod($class, $methodName))->getParameters()[0]->getType() + ); + + foreach ($emailTypes as $emailType) { $emailWriterListenerDefinition ->addMethodCall('addWriter', [$emailType, $id, $methodName, $priority]); } @@ -45,27 +50,4 @@ public function process(ContainerBuilder $container): void ServiceLocatorTagPass::register($container, $writers) ); } - - /** - * Extract classes base on union and name type. - * - * @return array - */ - private function getClasses(\ReflectionParameter $reflectionParameter): array - { - $type = $reflectionParameter->getType(); - - if ($type instanceof \ReflectionNamedType) { - return [$type->getName()]; - } - - if ($type instanceof \ReflectionUnionType) { - return array_map( - fn (\ReflectionNamedType $type) => $type->getName(), - $type->getTypes() - ); - } - - throw new \InvalidArgumentException('Unable to extract classes from parameter. Only named type and union type are supported.'); - } } diff --git a/packages/mailer/EmailComposer.php b/packages/mailer/EmailComposer.php index a04c10d87..59ba11cd0 100644 --- a/packages/mailer/EmailComposer.php +++ b/packages/mailer/EmailComposer.php @@ -3,6 +3,7 @@ namespace Draw\Component\Mailer; use Draw\Component\Core\Reflection\ReflectionAccessor; +use Draw\Component\Core\Reflection\ReflectionExtractor; use Draw\Component\Mailer\Email\LocalizeEmailInterface; use Draw\Component\Mailer\EmailWriter\EmailWriterInterface; use Psr\Container\ContainerInterface; @@ -63,8 +64,13 @@ public function registerEmailWriter(EmailWriterInterface $emailWriter): void $priority = 0; } - $emailType = (new \ReflectionMethod($class, $methodName))->getParameters()[0]->getClass()->name; - $this->addWriter($emailType, $emailWriter, $methodName, $priority); + $emailTypes = ReflectionExtractor::getClasses( + (new \ReflectionMethod($class, $methodName))->getParameters()[0]->getType() + ); + + foreach ($emailTypes as $emailType) { + $this->addWriter($emailType, $emailWriter, $methodName, $priority); + } } } diff --git a/packages/open-api/Extraction/Extractor/Symfony/RouterRootSchemaExtractor.php b/packages/open-api/Extraction/Extractor/Symfony/RouterRootSchemaExtractor.php index 4cd0dfabf..b5240a016 100644 --- a/packages/open-api/Extraction/Extractor/Symfony/RouterRootSchemaExtractor.php +++ b/packages/open-api/Extraction/Extractor/Symfony/RouterRootSchemaExtractor.php @@ -61,7 +61,13 @@ public function extract($source, $target, ExtractionContextInterface $extraction continue; } - $controller = explode('::', $route->getDefault('_controller')); + $controller = $route->getDefault('_controller'); + + if (!\is_string($controller)) { + continue; + } + + $controller = explode('::', $controller); if (2 != \count($controller)) { continue; diff --git a/packages/open-api/Serializer/Handler/ObjectReferenceHandler.php b/packages/open-api/Serializer/Handler/ObjectReferenceHandler.php index 93fae6c8e..54597ecc2 100644 --- a/packages/open-api/Serializer/Handler/ObjectReferenceHandler.php +++ b/packages/open-api/Serializer/Handler/ObjectReferenceHandler.php @@ -74,6 +74,6 @@ private function getManagerForClass(string $class): ObjectManager { return $this->ormManagerRegistry?->getManagerForClass($class) ?? $this->odmManagerRegistry?->getManagerForClass($class) - ?? throw new \RuntimeException('No object manager found for class ' . $class); + ?? throw new \RuntimeException('No object manager found for class '.$class); } } diff --git a/packages/tester/Http/Cookie/Cookie.php b/packages/tester/Http/Cookie/Cookie.php index 97d6e3b41..ea877a65c 100644 --- a/packages/tester/Http/Cookie/Cookie.php +++ b/packages/tester/Http/Cookie/Cookie.php @@ -328,7 +328,7 @@ public function matchesDomain($domain) { // Remove the leading '.' as per spec in RFC 6265. // http://tools.ietf.org/html/rfc6265#section-5.2.3 - $cookieDomain = ltrim($this->getDomain(), '.'); + $cookieDomain = ltrim((string) $this->getDomain(), '.'); // Domain not set or exact match. if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) { return true; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 93daf2a53..f43eaffc0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,7 @@ colors="true" bootstrap="tests/bootstrap.php" cacheDirectory=".phpunit.cache" + displayDetailsOnTestsThatTriggerDeprecations="true" >