Skip to content

Commit

Permalink
Ensure compatibility with persistence 4
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 Nov 2, 2024
1 parent e9a8e78 commit 89d865c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"doctrine/collections": "^1.5 || ^2.0",
"doctrine/event-manager": "^1.0 || ^2.0",
"doctrine/instantiator": "^1.1 || ^2",
"doctrine/persistence": "^3.2",
"doctrine/persistence": "^3.2 || ^4",
"friendsofphp/proxy-manager-lts": "^1.0",
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
"mongodb/mongodb": "^1.17.0",
Expand Down
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 @@ -437,7 +438,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 @@ -457,7 +458,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 @@ -475,7 +476,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 @@ -496,7 +497,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 @@ -556,7 +557,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 @@ -572,7 +573,7 @@ public function getRepository($className)
* @throws MongoDBException
* @throws Throwable From event listeners.
*/
public function flush(array $options = [])
public function flush(array $options = []): void
{
$this->errorIfClosed();
$this->unitOfWork->commit($options);
Expand Down Expand Up @@ -680,7 +681,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 @@ -716,7 +717,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 @@ -2241,7 +2241,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 @@ -81,7 +81,7 @@ public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENS
}

// phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
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 @@ -2968,7 +2968,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
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 @@ -653,7 +653,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 89d865c

Please sign in to comment.