Skip to content

Commit

Permalink
Implement compatibility with Persistence 4
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Nov 4, 2024
1 parent 0f11a97 commit 81c0d59
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doctrine/inflector": "^1.4 || ^2.0",
"doctrine/instantiator": "^1.3 || ^2",
"doctrine/lexer": "^3",
"doctrine/persistence": "^3.3.1",
"doctrine/persistence": "^3.3.1 || ^4",
"psr/cache": "^1 || ^2 || ^3",
"symfony/console": "^5.4 || ^6.0 || ^7.0",
"symfony/var-exporter": "^6.3.9 || ^7.0"
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>src/Mapping/Driver/LoadMappingFileImplementation.php</exclude-pattern>
<exclude-pattern>src/Mapping/GetReflectionClassImplementation.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>

Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ parameters:
count: 1
path: src/EntityRepository.php

-
message: "#^If condition is always true\\.$#"
count: 1
path: src/Mapping/ClassMetadata.php

-
message: "#^If condition is always true\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions phpstan-dbal3.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ parameters:
-
message: '~deprecated class Doctrine\\DBAL\\Tools\\Console\\Command\\ReservedWordsCommand\:~'
path: src/Tools/Console/ConsoleRunner.php

# Compatibility with Persistence 3
-
message: '#Expression on left side of \?\? is not nullable.#'
path: src/Mapping/Driver/AttributeDriver.php
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ parameters:
message: '#Negated boolean expression is always false\.#'
paths:
- src/Mapping/Driver/AttributeDriver.php

# Compatibility with Persistence 3
-
message: '#Expression on left side of \?\? is not nullable.#'
path: src/Mapping/Driver/AttributeDriver.php
23 changes: 17 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,9 @@
<code><![CDATA[$mapping]]></code>
<code><![CDATA[$overrideMapping]]></code>
</InvalidArgument>
<InvalidNullableReturnType>
<code><![CDATA[ReflectionClass|null]]></code>
</InvalidNullableReturnType>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$definition]]></code>
</InvalidPropertyAssignmentValue>
<NullableReturnStatement>
<code><![CDATA[$this->reflClass]]></code>
</NullableReturnStatement>
<ParamNameMismatch>
<code><![CDATA[$entity]]></code>
</ParamNameMismatch>
Expand Down Expand Up @@ -340,6 +334,7 @@
<RedundantCondition>
<code><![CDATA[$mapping !== false]]></code>
<code><![CDATA[$mapping !== false]]></code>
<code><![CDATA[$this->reflClass]]></code>
</RedundantCondition>
<RedundantFunctionCall>
<code><![CDATA[array_values]]></code>
Expand Down Expand Up @@ -474,6 +469,14 @@
<code><![CDATA[getIndexes]]></code>
</PossiblyNullReference>
</file>
<file src="src/Mapping/Driver/LoadMappingFileImplementation.php">
<LessSpecificImplementedReturnType>
<code><![CDATA[array]]></code>
</LessSpecificImplementedReturnType>
<MissingParamType>
<code><![CDATA[$file]]></code>
</MissingParamType>
</file>
<file src="src/Mapping/Driver/SimplifiedXmlDriver.php">
<MissingParamType>
<code><![CDATA[$fileExtension]]></code>
Expand Down Expand Up @@ -526,6 +529,14 @@
<code><![CDATA[$metadata]]></code>
</MoreSpecificImplementedParamType>
</file>
<file src="src/Mapping/GetReflectionClassImplementation.php">
<ImplementedReturnTypeMismatch>
<code><![CDATA[ReflectionClass]]></code>
</ImplementedReturnTypeMismatch>
<InvalidNullableReturnType>
<code><![CDATA[ReflectionClass]]></code>
</InvalidNullableReturnType>
</file>
<file src="src/Mapping/ManyToManyInverseSideMapping.php">
<PropertyNotSetInConstructor>
<code><![CDATA[ManyToManyInverseSideMapping]]></code>
Expand Down
4 changes: 2 additions & 2 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,9 @@ public function initializeObject(object $obj): void
/**
* {@inheritDoc}
*/
public function isUninitializedObject($obj): bool
public function isUninitializedObject($value): bool

Check warning on line 568 in src/EntityManager.php

View check run for this annotation

Codecov / codecov/patch

src/EntityManager.php#L568

Added line #L568 was not covered by tests
{
return $this->unitOfWork->isUninitializedObject($obj);
return $this->unitOfWork->isUninitializedObject($value);

Check warning on line 570 in src/EntityManager.php

View check run for this annotation

Codecov / codecov/patch

src/EntityManager.php#L570

Added line #L570 was not covered by tests
}

public function getFilters(): FilterCollection
Expand Down
12 changes: 2 additions & 10 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
*/
class ClassMetadata implements PersistenceClassMetadata, Stringable
{
use GetReflectionClassImplementation;

/* The inheritance mapping types */
/**
* NONE means the class does not participate in an inheritance hierarchy
Expand Down Expand Up @@ -932,16 +934,6 @@ public function validateLifecycleCallbacks(ReflectionService $reflService): void
}
}

/**
* {@inheritDoc}
*
* Can return null when using static reflection, in violation of the LSP
*/
public function getReflectionClass(): ReflectionClass|null
{
return $this->reflClass;
}

/** @psalm-param array{usage?: mixed, region?: mixed} $cache */
public function enableCache(array $cache): void
{
Expand Down
35 changes: 35 additions & 0 deletions src/Mapping/Driver/LoadMappingFileImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping\Driver;

use Doctrine\Persistence\Mapping\StaticReflectionService;

use function class_exists;

if (! class_exists(StaticReflectionService::class)) {
/** @internal */
trait LoadMappingFileImplementation
{
/**
* {@inheritDoc}
*/
protected function loadMappingFile($file): array
{
return $this->doLoadMappingFile($file);
}
}
} else {
/** @internal */
trait LoadMappingFileImplementation
{
/**
* {@inheritDoc}
*/
protected function loadMappingFile($file)
{
return $this->doLoadMappingFile($file);
}
}
}
8 changes: 4 additions & 4 deletions src/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
*/
class XmlDriver extends FileDriver
{
use LoadMappingFileImplementation;

public const DEFAULT_FILE_EXTENSION = '.dcm.xml';

/**
Expand Down Expand Up @@ -878,10 +880,8 @@ private function getCascadeMappings(SimpleXMLElement $cascadeElement): array
return $cascades;
}

/**
* {@inheritDoc}
*/
protected function loadMappingFile($file)
/** @return array<class-string, SimpleXMLElement> */
private function doLoadMappingFile(string $file): array
{
$this->validateMapping($file);
$result = [];
Expand Down
33 changes: 33 additions & 0 deletions src/Mapping/GetReflectionClassImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping;

use Doctrine\Persistence\Mapping\StaticReflectionService;
use ReflectionClass;

use function class_exists;

if (! class_exists(StaticReflectionService::class)) {
trait GetReflectionClassImplementation
{
public function getReflectionClass(): ReflectionClass
{
return $this->reflClass;
}
}
} else {
trait GetReflectionClassImplementation
{
/**
* {@inheritDoc}
*
* Can return null when using static reflection, in violation of the LSP
*/
public function getReflectionClass(): ReflectionClass|null
{
return $this->reflClass;
}
}
}
8 changes: 8 additions & 0 deletions tests/Performance/Mock/NonProxyLoadingEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,12 @@ public function contains(object $object): bool
{
return $this->realEntityManager->contains($object);
}

/**
* {@inheritDoc}
*/
public function isUninitializedObject($value): bool
{
return $this->realEntityManager->isUninitializedObject($value);
}
}
6 changes: 3 additions & 3 deletions tests/Tests/Mocks/MetadataDriverMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ class MetadataDriverMock implements MappingDriver
/**
* {@inheritDoc}
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
public function loadMetadataForClass($className, ClassMetadata $metadata): void
{
}

/**
* {@inheritDoc}
*/
public function isTransient($className)
public function isTransient($className): bool
{
return false;
}

/**
* {@inheritDoc}
*/
public function getAllClassNames()
public function getAllClassNames(): array
{
return [];
}
Expand Down
14 changes: 12 additions & 2 deletions tests/Tests/ORM/Functional/Ticket/DDC3103Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embeddable;
use Doctrine\Persistence\Mapping\StaticReflectionService;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

use function class_exists;
use function serialize;
use function unserialize;

#[CoversClass(ClassMetadata::class)]
#[Group('DDC-3103')]
class DDC3103Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
if (! class_exists(StaticReflectionService::class)) {
self::markTestSkipped('This test is not supported by the current installed doctrine/persistence version');
}

parent::setUp();
}

public function testIssue(): void
{
$classMetadata = new ClassMetadata(DDC3103ArticleId::class);
Expand All @@ -39,7 +50,6 @@ public function testIssue(): void
#[Embeddable]
class DDC3103ArticleId
{
/** @var string */
#[Column(name: 'name', type: 'string', length: 255)]
protected $nameValue;
protected string $nameValue;
}
5 changes: 5 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use stdClass;

use function assert;
use function class_exists;
use function count;
use function serialize;
use function str_contains;
Expand Down Expand Up @@ -979,6 +980,10 @@ public function testCanInstantiateInternalPhpClassSubclassFromUnserializedMetada

public function testWakeupReflectionWithEmbeddableAndStaticReflectionService(): void
{
if (! class_exists(StaticReflectionService::class)) {
self::markTestSkipped('This test is not supported by the current installed doctrine/persistence version');
}

$classMetadata = new ClassMetadata(TestEntity1::class);

$classMetadata->mapEmbedded(
Expand Down

0 comments on commit 81c0d59

Please sign in to comment.