From 2e4a8722721b934149ff53b191522a6829b6d73b Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 22 Apr 2022 19:46:03 +0200 Subject: [PATCH] Conditionally extend the old AnnotationDriver class (#9671) --- UPGRADE.md | 5 ---- .../ORM/Mapping/Driver/AnnotationDriver.php | 3 +-- .../ORM/Mapping/Driver/AttributeDriver.php | 3 +-- .../Driver/CompatibilityAnnotationDriver.php | 26 +++++++++++++++++++ phpcs.xml.dist | 3 ++- phpstan-persistence2.neon | 11 ++++++++ .../ORM/Mapping/AnnotationDriverTest.php | 13 ++++++++++ .../Tests/ORM/Mapping/AttributeDriverTest.php | 13 ++++++++++ 8 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php diff --git a/UPGRADE.md b/UPGRADE.md index 3e453d7d78c..3be2adc094a 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -41,11 +41,6 @@ This is now deprecated. Please extend `EntityRepository` instead. +$entityManager->getRepository(CmsUser::class); ``` -## BC Break: `AttributeDriver` and `AnnotationDriver` no longer extends parent class from `doctrine/persistence` - -Both these classes used to extend an abstract `AnnotationDriver` class defined -in `doctrine/persistence`, and no longer do. - ## Deprecate `AttributeDriver::getReader()` and `AnnotationDriver::getReader()` That method was inherited from the abstract `AnnotationDriver` class of diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index bdd1784c7c8..d1605f34340 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -14,7 +14,6 @@ use Doctrine\ORM\Mapping\MappingException; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver; -use Doctrine\Persistence\Mapping\Driver\MappingDriver; use ReflectionClass; use ReflectionMethod; use ReflectionProperty; @@ -32,7 +31,7 @@ /** * The AnnotationDriver reads the mapping metadata from docblock annotations. */ -class AnnotationDriver implements MappingDriver +class AnnotationDriver extends CompatibilityAnnotationDriver { use ColocatedMappingDriver; diff --git a/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php index 99d92a0534a..b318474d93d 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php @@ -12,7 +12,6 @@ use Doctrine\ORM\Mapping\MappingException; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver; -use Doctrine\Persistence\Mapping\Driver\MappingDriver; use LogicException; use ReflectionClass; use ReflectionMethod; @@ -27,7 +26,7 @@ use const PHP_VERSION_ID; -class AttributeDriver implements MappingDriver +class AttributeDriver extends CompatibilityAnnotationDriver { use ColocatedMappingDriver; diff --git a/lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php new file mode 100644 index 00000000000..1d544c43744 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php @@ -0,0 +1,26 @@ + - */tests/* + lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php + tests/* diff --git a/phpstan-persistence2.neon b/phpstan-persistence2.neon index 6572a340e8d..cd3fb1c80f3 100644 --- a/phpstan-persistence2.neon +++ b/phpstan-persistence2.neon @@ -41,3 +41,14 @@ parameters: # Symfony cache supports passing a key prefix to the clear method. - '/^Method Psr\\Cache\\CacheItemPoolInterface\:\:clear\(\) invoked with 1 parameter, 0 required\.$/' + + # Compatibility layer for AttributeDriver + - + message: "#^PHPDoc type Doctrine\\\\ORM\\\\Mapping\\\\Driver\\\\AttributeReader of property Doctrine\\\\ORM\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:\\$reader is not covariant with PHPDoc type Doctrine\\\\Common\\\\Annotations\\\\Reader of overridden property Doctrine\\\\Persistence\\\\Mapping\\\\Driver\\\\AnnotationDriver\\:\\:\\$reader\\.$#" + path: lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php + - + message: "#^PHPDoc type array\\ of property Doctrine\\\\ORM\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:\\$entityAnnotationClasses is not covariant with PHPDoc type array\\ of overridden property Doctrine\\\\Persistence\\\\Mapping\\\\Driver\\\\AnnotationDriver\\:\\:\\$entityAnnotationClasses\\.$#" + path: lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php + - + message: "#^Return type \\(Doctrine\\\\ORM\\\\Mapping\\\\Driver\\\\AttributeReader\\) of method Doctrine\\\\ORM\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:getReader\\(\\) should be compatible with return type \\(Doctrine\\\\Common\\\\Annotations\\\\Reader\\) of method Doctrine\\\\Persistence\\\\Mapping\\\\Driver\\\\AnnotationDriver\\:\\:getReader\\(\\)$#" + path: lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php index 9357208a4a3..111fdeeb6bd 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php @@ -26,6 +26,7 @@ use Doctrine\ORM\Mapping\OneToMany; use Doctrine\ORM\Mapping\PostLoad; use Doctrine\ORM\Mapping\PreUpdate; +use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as PersistenceAnnotationDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\Mapping\RuntimeReflectionService; use Doctrine\Tests\Models\CMS\CmsUser; @@ -36,6 +37,9 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCart; use Generator; +use function class_exists; +use function is_subclass_of; + class AnnotationDriverTest extends AbstractMappingDriverTest { /** @@ -303,6 +307,15 @@ public function provideDiscriminatorColumnTestcases(): Generator yield [DiscriminatorColumnWithZeroLength::class, 0]; yield [DiscriminatorColumnWithNonZeroLength::class, 60]; } + + public function testLegacyInheritance(): void + { + if (! class_exists(PersistenceAnnotationDriver::class)) { + self::markTestSkipped('This test requires doctrine/persistence 2.'); + } + + self::assertTrue(is_subclass_of(AnnotationDriver::class, PersistenceAnnotationDriver::class)); + } } /** diff --git a/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php index 1ef6f60cc32..684ebd8ff32 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php @@ -8,9 +8,13 @@ use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\Annotation; use Doctrine\ORM\Mapping\Driver\AttributeDriver; +use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as PersistenceAnnotationDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use stdClass; +use function class_exists; +use function is_subclass_of; + use const PHP_VERSION_ID; class AttributeDriverTest extends AbstractMappingDriverTest @@ -110,6 +114,15 @@ public function testIsTransient(): void self::assertFalse($driver->isTransient(AttributeEntityStartingWithRepeatableAttributes::class)); } + + public function testLegacyInheritance(): void + { + if (! class_exists(PersistenceAnnotationDriver::class)) { + self::markTestSkipped('This test requires doctrine/persistence 2.'); + } + + self::assertTrue(is_subclass_of(AttributeDriver::class, PersistenceAnnotationDriver::class)); + } } #[ORM\Entity]