Skip to content

Commit

Permalink
Ensure compatibility with persistence 4 (#2669)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greg0ire committed Sep 9, 2024
1 parent e40c8b1 commit 296b025
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 28 deletions.
33 changes: 17 additions & 16 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\ODM\MongoDB\Repository\ViewRepository;
use Doctrine\Persistence\Mapping\ProxyClassNameResolver;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use Jean85\PrettyVersions;
use MongoDB\Client;
Expand Down Expand Up @@ -218,12 +219,8 @@ public function getClient(): Client
return $this->client;
}

/**
* Gets the metadata factory used to gather the metadata of classes.
*
* @return ClassMetadataFactoryInterface
*/
public function getMetadataFactory()
/** Gets the metadata factory used to gather the metadata of classes. */
public function getMetadataFactory(): ClassmetadataFactoryInterface
{
return $this->metadataFactory;
}
Expand All @@ -235,16 +232,20 @@ public function getMetadataFactory()
*
* @param object $obj
*/
public function initializeObject($obj)
public function initializeObject($obj): void
{
$this->unitOfWork->initializeObject($obj);
}

/**
* Helper method to check whether a lazy loading proxy or persistent collection has been initialized.
*/
public function isUninitializedObject(object $obj): bool
public function isUninitializedObject(mixed $obj): bool
{
if (! is_object($obj)) {
return false;
}

return $this->unitOfWork->isUninitializedObject($obj);
}

Expand Down Expand Up @@ -440,7 +441,7 @@ public function createAggregationBuilder(string $documentName): Aggregation\Buil
*
* @throws InvalidArgumentException When the given $object param is not an object.
*/
public function persist($object)
public function persist($object): void
{
if (! is_object($object)) {
throw new InvalidArgumentException(gettype($object));
Expand All @@ -460,7 +461,7 @@ public function persist($object)
*
* @throws InvalidArgumentException When the $object param is not an object.
*/
public function remove($object)
public function remove($object): void
{
if (! is_object($object)) {
throw new InvalidArgumentException(gettype($object));
Expand All @@ -478,7 +479,7 @@ public function remove($object)
*
* @throws InvalidArgumentException When the given $object param is not an object.
*/
public function refresh($object)
public function refresh($object): void
{
if (! is_object($object)) {
throw new InvalidArgumentException(gettype($object));
Expand All @@ -499,7 +500,7 @@ public function refresh($object)
*
* @throws InvalidArgumentException When the $object param is not an object.
*/
public function detach($object)
public function detach($object): void
{
if (! is_object($object)) {
throw new InvalidArgumentException(gettype($object));
Expand Down Expand Up @@ -561,7 +562,7 @@ public function unlock(object $document): void
*
* @template T of object
*/
public function getRepository($className)
public function getRepository($className): ObjectRepository
{
return $this->repositoryFactory->getRepository($this, $className);
}
Expand All @@ -576,7 +577,7 @@ public function getRepository($className)
*
* @throws MongoDBException
*/
public function flush(array $options = [])
public function flush(array $options = []): void
{
$this->errorIfClosed();
$this->unitOfWork->commit($options);
Expand Down Expand Up @@ -686,7 +687,7 @@ public function find($className, $id, $lockMode = LockMode::NONE, $lockVersion =
*
* @param string|null $objectName if given, only documents of this type will get detached
*/
public function clear($objectName = null)
public function clear($objectName = null): void
{
if ($objectName !== null) {
trigger_deprecation(
Expand Down Expand Up @@ -722,7 +723,7 @@ public function close()
*
* @throws InvalidArgumentException When the $object param is not an object.
*/
public function contains($object)
public function contains($object): bool
{
if (! is_object($object)) {
throw new InvalidArgumentException(gettype($object));
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ public function isAssociationInverseSide($assocName): bool
}

/** @param string $assocName */
public function getAssociationMappedByTargetField($assocName)
public function getAssociationMappedByTargetField($assocName): string
{
throw new BadMethodCallException(__METHOD__ . '() is not implemented yet.');
}
Expand Down
10 changes: 6 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
*/
final class ClassMetadataFactory extends AbstractClassMetadataFactory implements ClassMetadataFactoryInterface
{
/** @var string */
protected $cacheSalt = '$MONGODBODMCLASSMETADATA';

/** @var DocumentManager The DocumentManager instance */
private DocumentManager $dm;

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

public function __construct()
{
$this->cacheSalt = '$MONGODBODMCLASSMETADATA';
}

public function setDocumentManager(DocumentManager $dm): void
{
$this->dm = $dm;
Expand Down Expand Up @@ -82,7 +84,7 @@ protected function initialize(): void
}

/** @param string $className */
protected function onNotFoundMetadata($className)
protected function onNotFoundMetadata($className): ?ClassMetadata
{
if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct($paths = null, ?Reader $reader = null)
$this->addPaths((array) $paths);
}

public function isTransient($className)
public function isTransient($className): bool
{
$classAttributes = $this->getClassAttributes(new ReflectionClass($className));

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENS
parent::__construct($locator, $fileExtension);
}

public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata)
public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata): void
{
assert($metadata instanceof ClassMetadata);
$xmlRoot = $this->getElement($className);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ public function clearDocumentChangeSet(string $oid): void
* @param mixed $oldValue The old value of the property.
* @param mixed $newValue The new value of the property.
*/
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void
{
$oid = spl_object_hash($sender);
$class = $this->dm->getClassMetadata($sender::class);
Expand Down
9 changes: 6 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
</TypeDoesNotContainType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/DocumentManager.php">
<ImplementedReturnTypeMismatch>
<code><![CDATA[ClassMetadataFactoryInterface]]></code>
</ImplementedReturnTypeMismatch>
<InvalidReturnStatement>
<code><![CDATA[$this->metadataFactory]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[ClassmetadataFactoryInterface]]></code>
</InvalidReturnType>
<NullableReturnStatement>
<code><![CDATA[$mapping['targetDocument']]]></code>
</NullableReturnStatement>
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public function setTransient($value): void
$this->transient = $value;
}

public function addPropertyChangedListener(PropertyChangedListener $listener)
public function addPropertyChangedListener(PropertyChangedListener $listener): void
{
$this->_listeners[] = $listener;
}
Expand Down

0 comments on commit 296b025

Please sign in to comment.