Skip to content

Commit

Permalink
Conditionally extend the old AnnotationDriver class (#9671)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Apr 22, 2022
1 parent d550364 commit 2e4a872
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 10 deletions.
5 changes: 0 additions & 5 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +31,7 @@
/**
* The AnnotationDriver reads the mapping metadata from docblock annotations.
*/
class AnnotationDriver implements MappingDriver
class AnnotationDriver extends CompatibilityAnnotationDriver
{
use ColocatedMappingDriver;

Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,7 @@

use const PHP_VERSION_ID;

class AttributeDriver implements MappingDriver
class AttributeDriver extends CompatibilityAnnotationDriver
{
use ColocatedMappingDriver;

Expand Down
26 changes: 26 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping\Driver;

use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as PersistenceAnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;

use function class_exists;

if (class_exists(PersistenceAnnotationDriver::class)) {
/**
* @internal This class will be removed in ORM 3.0.
*/
abstract class CompatibilityAnnotationDriver extends PersistenceAnnotationDriver
{
}
} else {
/**
* @internal This class will be removed in ORM 3.0.
*/
abstract class CompatibilityAnnotationDriver implements MappingDriver
{
}
}
3 changes: 2 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>lib/Doctrine/ORM/Mapping/Driver/CompatibilityAnnotationDriver.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ClassFileName.NoMatch">
Expand Down
11 changes: 11 additions & 0 deletions phpstan-persistence2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\<string, int\\> of property Doctrine\\\\ORM\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:\\$entityAnnotationClasses is not covariant with PHPDoc type array\\<class\\-string, bool\\|int\\> 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
13 changes: 13 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
/**
Expand Down Expand Up @@ -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));
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 2e4a872

Please sign in to comment.