From a7b0f16a56c6d62af2e4a8f1461c617ad6246e5c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 23 Oct 2020 19:10:28 +0200 Subject: [PATCH] [DependencyInjection] Preload classes with union types correctly. --- .github/patch-types.php | 1 + .../DependencyInjection/Dumper/Preloader.php | 2 +- .../Tests/Dumper/PreloaderTest.php | 53 +++++++++++++++++++ .../Tests/Fixtures/Preload/A.php | 16 ++++++ .../Tests/Fixtures/Preload/B.php | 16 ++++++ .../Tests/Fixtures/Preload/C.php | 16 ++++++ .../Tests/Fixtures/Preload/D.php | 16 ++++++ .../Tests/Fixtures/Preload/Dummy.php | 30 +++++++++++ .../Tests/Fixtures/Preload/E.php | 16 ++++++ .../Tests/Fixtures/Preload/UnionDummy.php | 21 ++++++++ 10 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Dumper/PreloaderTest.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/A.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/B.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/C.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/D.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/Dummy.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/E.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/UnionDummy.php diff --git a/.github/patch-types.php b/.github/patch-types.php index 5c68a66c89..46c6980dab 100644 --- a/.github/patch-types.php +++ b/.github/patch-types.php @@ -33,6 +33,7 @@ foreach ($loader->getClassMap() as $class => $file) { case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php'): case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/uniontype_classes.php'): case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ParentNotExists.php'): + case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/'): case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/BadClasses/MissingParent.php'): case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/'): case false !== strpos($file, '/src/Symfony/Component/ErrorHandler/Tests/Fixtures/'): diff --git a/src/Symfony/Component/DependencyInjection/Dumper/Preloader.php b/src/Symfony/Component/DependencyInjection/Dumper/Preloader.php index fb39645022..1e0b6919fd 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/Preloader.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/Preloader.php @@ -94,7 +94,7 @@ class Preloader private static function preloadType(?\ReflectionType $t, array &$preloaded): void { - if (!$t || $t->isBuiltin()) { + if (!$t) { return; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PreloaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PreloaderTest.php new file mode 100644 index 0000000000..a9b3242031 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PreloaderTest.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Dumper; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Dumper\Preloader; + +class PreloaderTest extends TestCase +{ + /** + * @requires PHP 7.4 + */ + public function testPreload() + { + $r = new \ReflectionMethod(Preloader::class, 'doPreload'); + $r->setAccessible(true); + + $preloaded = []; + + $r->invokeArgs(null, ['Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\Dummy', &$preloaded]); + + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\Dummy', false)); + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\A', false)); + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\B', false)); + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\C', false)); + } + + /** + * @requires PHP 8 + */ + public function testPreloadUnion() + { + $r = new \ReflectionMethod(Preloader::class, 'doPreload'); + $r->setAccessible(true); + + $preloaded = []; + + $r->invokeArgs(null, ['Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\UnionDummy', &$preloaded]); + + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\UnionDummy', false)); + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\D', false)); + self::assertTrue(class_exists('Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\E', false)); + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/A.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/A.php new file mode 100644 index 0000000000..2802a577e8 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/A.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class A +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/B.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/B.php new file mode 100644 index 0000000000..66dda101ce --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/B.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class B +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/C.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/C.php new file mode 100644 index 0000000000..331e9fed19 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/C.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class C +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/D.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/D.php new file mode 100644 index 0000000000..52e10e9181 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/D.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class D +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/Dummy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/Dummy.php new file mode 100644 index 0000000000..35d61be5a5 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/Dummy.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class Dummy +{ + public A $a; + + public function doSomething(B $b): ?C + { + return null; + } + + public function noTypes($foo) + { + } + + public function builtinTypes(int $foo): ?string + { + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/E.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/E.php new file mode 100644 index 0000000000..b4ec3b2734 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/E.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class E +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/UnionDummy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/UnionDummy.php new file mode 100644 index 0000000000..24b709b0a2 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/UnionDummy.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload; + +final class UnionDummy +{ + public D|E $de; + + public function builtinTypes(int|float $foo): string|\Stringable|null + { + } +}