Skip to content

Commit

Permalink
Merge pull request #2428 from malarzm/doctrine-persistence-3-0
Browse files Browse the repository at this point in the history
Allow doctrine/persistence ^3.0
  • Loading branch information
malarzm authored Apr 16, 2022
2 parents cad2504 + 1dc61d4 commit 1cfb132
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 6 deletions.
11 changes: 11 additions & 0 deletions UPGRADE-2.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ private int $myProp;
This property will be stored in DB as `string` but casted back to `int`. Please note that at this
time, due to backward compatibility reasons, nullable type does not imply `nullable` mapping.

## doctrine/persistence ^2.4 || ^3.0

ODM now supports two major versions of `doctrine/persistence` and provides forward compatibility where possible.
We strongly recommend checking [package's upgrade notes](https://github.com/doctrine/persistence/blob/3.0.x/UPGRADE.md)
and [ODM's todo list](https://github.com/doctrine/mongodb-odm/issues/2419). Please require 2.x version of
`doctrine/persistence` in your composer.json should you need to use functionalities without a forward
compatibility layer. Most notable examples are:

* removed `LifecycleEventArgs::getEntity()`
* removed support for short namespace aliases

## BC Break: `AttributeDriver` and `AnnotationDriver` no longer extends parent class from `doctrine/persistence`

Both these classes used to extend an abstract `AnnotationDriver` class defined
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"doctrine/collections": "^1.5",
"doctrine/event-manager": "^1.0",
"doctrine/instantiator": "^1.1",
"doctrine/persistence": "^2.4",
"doctrine/persistence": "^2.4 || ^3.0",
"friendsofphp/proxy-manager-lts": "^1.0",
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
"mongodb/mongodb": "^1.2.0",
Expand Down
7 changes: 5 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Event/OnClearEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ final class OnClearEventArgs extends BaseOnClearEventArgs
*/
private $entityClass;

/**
* @param class-string|null $entityClass
*/
public function __construct($objectManager, $entityClass = null)
{
if (method_exists(parent::class, 'getEntityClass') && $entityClass !== null) {
Expand Down Expand Up @@ -90,7 +93,7 @@ public function clearsAllDocuments(): bool
/**
* @deprecated no replacement planned
*/
public function clearsAllEntities()
public function clearsAllEntities(): bool
{
if (method_exists(parent::class, 'clearsAllEntities')) {
return parent::clearsAllEntities();
Expand All @@ -111,7 +114,7 @@ public function clearsAllEntities()
*
* @return class-string|null
*/
public function getEntityClass()
public function getEntityClass(): ?string
{
if (method_exists(parent::class, 'getEntityClass')) {
return parent::getEntityClass();
Expand Down
28 changes: 28 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ public function getReflectionClass(): ReflectionClass
return $this->reflClass;
}

/**
* @param string $fieldName
*/
public function isIdentifier($fieldName): bool
{
return $this->identifier === $fieldName;
Expand All @@ -811,6 +814,8 @@ public function setIdentifier(?string $identifier): void
/**
* Since MongoDB only allows exactly one identifier field
* this will always return an array with only one value
*
* @return array<string|null>
*/
public function getIdentifier(): array
{
Expand All @@ -828,6 +833,9 @@ public function getIdentifierFieldNames(): array
return [$this->identifier];
}

/**
* @param string $fieldName
*/
public function hasField($fieldName): bool
{
return isset($this->fieldMappings[$fieldName]);
Expand Down Expand Up @@ -1631,6 +1639,8 @@ public function hasEmbed(string $fieldName): bool

/**
* Checks whether the class has a mapped association (embed or reference) with the given field name.
*
* @param string $fieldName
*/
public function hasAssociation($fieldName): bool
{
Expand All @@ -1640,6 +1650,8 @@ public function hasAssociation($fieldName): bool
/**
* Checks whether the class has a mapped reference or embed for the specified field and
* is a single valued association.
*
* @param string $fieldName
*/
public function isSingleValuedAssociation($fieldName): bool
{
Expand All @@ -1649,6 +1661,8 @@ public function isSingleValuedAssociation($fieldName): bool
/**
* Checks whether the class has a mapped reference or embed for the specified field and
* is a collection valued association.
*
* @param string $fieldName
*/
public function isCollectionValuedAssociation($fieldName): bool
{
Expand Down Expand Up @@ -1758,6 +1772,8 @@ public function getIdentifierValue(object $document)
* Since MongoDB only allows exactly one identifier field this is a proxy
* to {@see getIdentifierValue()} and returns an array with the identifier
* field as a key.
*
* @param object $object
*/
public function getIdentifierValues($object): array
{
Expand Down Expand Up @@ -2082,12 +2098,18 @@ public function getAssociationNames(): array
return array_keys($this->associationMappings);
}

/**
* @param string $fieldName
*/
public function getTypeOfField($fieldName): ?string
{
return isset($this->fieldMappings[$fieldName]) ?
$this->fieldMappings[$fieldName]['type'] : null;
}

/**
* @param string $assocName
*/
public function getAssociationTargetClass($assocName): ?string
{
if (! isset($this->associationMappings[$assocName])) {
Expand All @@ -2113,11 +2135,17 @@ public function getAssociationCollectionClass(string $assocName): string
return $this->associationMappings[$assocName]['collectionClass'];
}

/**
* @param string $assocName
*/
public function isAssociationInverseSide($assocName): bool
{
throw new BadMethodCallException(__METHOD__ . '() is not implemented yet.');
}

/**
* @param string $assocName
*/
public function getAssociationMappedByTargetField($assocName)
{
throw new BadMethodCallException(__METHOD__ . '() is not implemented yet.');
Expand Down
14 changes: 13 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ protected function initialize(): void
$this->initialized = true;
}

/**
* @param string $className
*/
protected function onNotFoundMetadata($className)
{
if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
Expand All @@ -96,12 +99,18 @@ protected function onNotFoundMetadata($className)
return $eventArgs->getFoundMetadata();
}

/**
* @deprecated
*
* @param string $namespaceAlias
* @param string $simpleClassName
*/
protected function getFqcnFromAlias($namespaceAlias, $simpleClassName): string
{
return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName;
}

protected function getDriver()
protected function getDriver(): MappingDriver
{
return $this->driver;
}
Expand All @@ -121,6 +130,9 @@ protected function isEntity(ClassMetadataInterface $class): bool
return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument && ! $class->isView();
}

/**
* @param bool $rootEntityFound
*/
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = []): void
{
assert($class instanceof ClassMetadata);
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class XmlDriver extends FileDriver
],
];

/**
* @param string|null $fileExtension
*/
public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
parent::__construct($locator, $fileExtension);
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public function findAll(): array

/**
* Finds documents by a set of criteria.
*
* @param int|null $limit
* @param int|null $offset
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ parameters:
count: 1
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php

# compatibility layer for doctrine/persistence ^2.4 || ^3.0
-
message: "#.*#"
count: 3
path: lib/Doctrine/ODM/MongoDB/Event/OnClearEventArgs

-
message: "#Property .+ is never written, only read.#"
path: tests
Expand Down
19 changes: 17 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.20.0@f82a70e7edfc6cf2705e9374c8a0b6a974a779ed">
<files psalm-version="4.22.0@fc2c6ab4d5fa5d644d8617089f012f3bb84b8703">
<file src="lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GeoNear.php">
<InvalidReturnStatement occurrences="1">
<code>$this-&gt;num($limit)</code>
Expand All @@ -8,6 +8,11 @@
<code>self</code>
</InvalidReturnType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Event/OnClearEventArgs.php">
<TooManyArguments occurrences="1">
<code>parent::__construct($objectManager, $entityClass)</code>
</TooManyArguments>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Iterator/CachingIterator.php">
<ImplementedReturnTypeMismatch occurrences="1">
<code>TValue|false</code>
Expand All @@ -19,12 +24,17 @@
</ImplementedReturnTypeMismatch>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php">
<ImplementedReturnTypeMismatch occurrences="1">
<ImplementedReturnTypeMismatch occurrences="2">
<code>array&lt;string|null&gt;</code>
<code>array&lt;string|null&gt;</code>
</ImplementedReturnTypeMismatch>
<InvalidArrayOffset occurrences="1">
<code>[$this-&gt;identifier =&gt; $this-&gt;getIdentifierValue($object)]</code>
</InvalidArrayOffset>
<LessSpecificImplementedReturnType occurrences="2">
<code>?string</code>
<code>array</code>
</LessSpecificImplementedReturnType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php">
<InvalidArgument occurrences="2">
Expand Down Expand Up @@ -87,6 +97,11 @@
<code>$options + ['metadata' =&gt; (object) $metadata]</code>
</InvalidReturnStatement>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php">
<LessSpecificImplementedReturnType occurrences="1">
<code>array</code>
</LessSpecificImplementedReturnType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php">
<RedundantCondition occurrences="1">
<code>$datetime instanceof DateTime</code>
Expand Down

0 comments on commit 1cfb132

Please sign in to comment.