Skip to content

Commit 296b025

Browse files
authored
Ensure compatibility with persistence 4 (#2669)
This is mostly about adding return type declarations. In one instance though, it is more than that: ClassMetadataFactory redeclares $cacheSalt, a protected property inherited from the persistence package. Since it is not possible to widen or narrow the type, and since the redeclaration seems to be about setting a default value, let us set it in the constructor.
1 parent e40c8b1 commit 296b025

File tree

8 files changed

+34
-28
lines changed

8 files changed

+34
-28
lines changed

lib/Doctrine/ODM/MongoDB/DocumentManager.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\ODM\MongoDB\Repository\ViewRepository;
2222
use Doctrine\Persistence\Mapping\ProxyClassNameResolver;
2323
use Doctrine\Persistence\ObjectManager;
24+
use Doctrine\Persistence\ObjectRepository;
2425
use InvalidArgumentException;
2526
use Jean85\PrettyVersions;
2627
use MongoDB\Client;
@@ -218,12 +219,8 @@ public function getClient(): Client
218219
return $this->client;
219220
}
220221

221-
/**
222-
* Gets the metadata factory used to gather the metadata of classes.
223-
*
224-
* @return ClassMetadataFactoryInterface
225-
*/
226-
public function getMetadataFactory()
222+
/** Gets the metadata factory used to gather the metadata of classes. */
223+
public function getMetadataFactory(): ClassmetadataFactoryInterface
227224
{
228225
return $this->metadataFactory;
229226
}
@@ -235,16 +232,20 @@ public function getMetadataFactory()
235232
*
236233
* @param object $obj
237234
*/
238-
public function initializeObject($obj)
235+
public function initializeObject($obj): void
239236
{
240237
$this->unitOfWork->initializeObject($obj);
241238
}
242239

243240
/**
244241
* Helper method to check whether a lazy loading proxy or persistent collection has been initialized.
245242
*/
246-
public function isUninitializedObject(object $obj): bool
243+
public function isUninitializedObject(mixed $obj): bool
247244
{
245+
if (! is_object($obj)) {
246+
return false;
247+
}
248+
248249
return $this->unitOfWork->isUninitializedObject($obj);
249250
}
250251

@@ -440,7 +441,7 @@ public function createAggregationBuilder(string $documentName): Aggregation\Buil
440441
*
441442
* @throws InvalidArgumentException When the given $object param is not an object.
442443
*/
443-
public function persist($object)
444+
public function persist($object): void
444445
{
445446
if (! is_object($object)) {
446447
throw new InvalidArgumentException(gettype($object));
@@ -460,7 +461,7 @@ public function persist($object)
460461
*
461462
* @throws InvalidArgumentException When the $object param is not an object.
462463
*/
463-
public function remove($object)
464+
public function remove($object): void
464465
{
465466
if (! is_object($object)) {
466467
throw new InvalidArgumentException(gettype($object));
@@ -478,7 +479,7 @@ public function remove($object)
478479
*
479480
* @throws InvalidArgumentException When the given $object param is not an object.
480481
*/
481-
public function refresh($object)
482+
public function refresh($object): void
482483
{
483484
if (! is_object($object)) {
484485
throw new InvalidArgumentException(gettype($object));
@@ -499,7 +500,7 @@ public function refresh($object)
499500
*
500501
* @throws InvalidArgumentException When the $object param is not an object.
501502
*/
502-
public function detach($object)
503+
public function detach($object): void
503504
{
504505
if (! is_object($object)) {
505506
throw new InvalidArgumentException(gettype($object));
@@ -561,7 +562,7 @@ public function unlock(object $document): void
561562
*
562563
* @template T of object
563564
*/
564-
public function getRepository($className)
565+
public function getRepository($className): ObjectRepository
565566
{
566567
return $this->repositoryFactory->getRepository($this, $className);
567568
}
@@ -576,7 +577,7 @@ public function getRepository($className)
576577
*
577578
* @throws MongoDBException
578579
*/
579-
public function flush(array $options = [])
580+
public function flush(array $options = []): void
580581
{
581582
$this->errorIfClosed();
582583
$this->unitOfWork->commit($options);
@@ -686,7 +687,7 @@ public function find($className, $id, $lockMode = LockMode::NONE, $lockVersion =
686687
*
687688
* @param string|null $objectName if given, only documents of this type will get detached
688689
*/
689-
public function clear($objectName = null)
690+
public function clear($objectName = null): void
690691
{
691692
if ($objectName !== null) {
692693
trigger_deprecation(
@@ -722,7 +723,7 @@ public function close()
722723
*
723724
* @throws InvalidArgumentException When the $object param is not an object.
724725
*/
725-
public function contains($object)
726+
public function contains($object): bool
726727
{
727728
if (! is_object($object)) {
728729
throw new InvalidArgumentException(gettype($object));

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ public function isAssociationInverseSide($assocName): bool
22162216
}
22172217

22182218
/** @param string $assocName */
2219-
public function getAssociationMappedByTargetField($assocName)
2219+
public function getAssociationMappedByTargetField($assocName): string
22202220
{
22212221
throw new BadMethodCallException(__METHOD__ . '() is not implemented yet.');
22222222
}

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
*/
4141
final class ClassMetadataFactory extends AbstractClassMetadataFactory implements ClassMetadataFactoryInterface
4242
{
43-
/** @var string */
44-
protected $cacheSalt = '$MONGODBODMCLASSMETADATA';
45-
4643
/** @var DocumentManager The DocumentManager instance */
4744
private DocumentManager $dm;
4845

@@ -55,6 +52,11 @@ final class ClassMetadataFactory extends AbstractClassMetadataFactory implements
5552
/** @var EventManager The event manager instance */
5653
private EventManager $evm;
5754

55+
public function __construct()
56+
{
57+
$this->cacheSalt = '$MONGODBODMCLASSMETADATA';
58+
}
59+
5860
public function setDocumentManager(DocumentManager $dm): void
5961
{
6062
$this->dm = $dm;
@@ -82,7 +84,7 @@ protected function initialize(): void
8284
}
8385

8486
/** @param string $className */
85-
protected function onNotFoundMetadata($className)
87+
protected function onNotFoundMetadata($className): ?ClassMetadata
8688
{
8789
if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
8890
return null;

lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct($paths = null, ?Reader $reader = null)
6262
$this->addPaths((array) $paths);
6363
}
6464

65-
public function isTransient($className)
65+
public function isTransient($className): bool
6666
{
6767
$classAttributes = $this->getClassAttributes(new ReflectionClass($className));
6868

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENS
7979
parent::__construct($locator, $fileExtension);
8080
}
8181

82-
public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata)
82+
public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata): void
8383
{
8484
assert($metadata instanceof ClassMetadata);
8585
$xmlRoot = $this->getElement($className);

lib/Doctrine/ODM/MongoDB/UnitOfWork.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,7 @@ public function clearDocumentChangeSet(string $oid): void
29742974
* @param mixed $oldValue The old value of the property.
29752975
* @param mixed $newValue The new value of the property.
29762976
*/
2977-
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
2977+
public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void
29782978
{
29792979
$oid = spl_object_hash($sender);
29802980
$class = $this->dm->getClassMetadata($sender::class);

psalm-baseline.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
</TypeDoesNotContainType>
2626
</file>
2727
<file src="lib/Doctrine/ODM/MongoDB/DocumentManager.php">
28-
<ImplementedReturnTypeMismatch>
29-
<code><![CDATA[ClassMetadataFactoryInterface]]></code>
30-
</ImplementedReturnTypeMismatch>
28+
<InvalidReturnStatement>
29+
<code><![CDATA[$this->metadataFactory]]></code>
30+
</InvalidReturnStatement>
31+
<InvalidReturnType>
32+
<code><![CDATA[ClassmetadataFactoryInterface]]></code>
33+
</InvalidReturnType>
3134
<NullableReturnStatement>
3235
<code><![CDATA[$mapping['targetDocument']]]></code>
3336
</NullableReturnStatement>

tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ public function setTransient($value): void
654654
$this->transient = $value;
655655
}
656656

657-
public function addPropertyChangedListener(PropertyChangedListener $listener)
657+
public function addPropertyChangedListener(PropertyChangedListener $listener): void
658658
{
659659
$this->_listeners[] = $listener;
660660
}

0 commit comments

Comments
 (0)