Skip to content

Commit

Permalink
Merge branch '2.10.x' into proxy-var-exporter-2
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan authored Jan 22, 2025
2 parents 4d4c864 + d726f05 commit 3820d20
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 35 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",
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
"mongodb/mongodb": "^1.17.0",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
Expand Down
24 changes: 22 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\CachingIterator;
use Doctrine\ODM\MongoDB\Iterator\HydratingIterator;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Iterator as SPLIterator;
use IteratorAggregate;
use MongoDB\Collection;
use MongoDB\Driver\CursorInterface;

use function array_merge;
use function assert;

/** @phpstan-import-type PipelineExpression from Builder */
final class Aggregation implements IteratorAggregate
final class Aggregation implements IterableResult
{
/**
* @param array<string, mixed> $pipeline
Expand All @@ -30,6 +30,26 @@ public function __construct(private DocumentManager $dm, private ?ClassMetadata
{
}

public function execute(): Iterator
{
return $this->getIterator();
}

/**
* Execute the query and return the first result.
*
* @return array<string, mixed>|object|null
*/
public function getSingleResult(): mixed
{
$clone = clone $this;

// Limit the pipeline to a single result for efficiency
$this->pipeline[] = ['$limit' => 1];

return $clone->getIterator()->current() ?: null;
}

public function getIterator(): Iterator
{
// Force cursor to be used
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
Expand Down Expand Up @@ -246,8 +247,10 @@ public function geoNear($x, $y = null): Stage\GeoNear
* Returns an aggregation object for the current pipeline
*
* @param array<string, mixed> $options
*
* @return Aggregation
*/
public function getAggregation(array $options = []): Aggregation
public function getAggregation(array $options = []): IterableResult
{
$class = $this->hydrationClass ? $this->dm->getClassMetadata($this->hydrationClass) : null;

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 @@ -23,6 +23,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 @@ -224,12 +225,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 @@ -241,16 +238,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 @@ -443,7 +444,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 @@ -463,7 +464,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 @@ -481,7 +482,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 @@ -502,7 +503,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 @@ -562,7 +563,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 @@ -578,7 +579,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 @@ -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
32 changes: 32 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Iterator/IterableResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Iterator;

use BadMethodCallException;
use IteratorAggregate;

interface IterableResult extends IteratorAggregate
{
/**
* Executes the operation and returns a result if available; null otherwise
*/
public function execute(): mixed;

/**
* Executes the operation and returns a result iterator. If the operation
* did not yield an iterator, this method will throw
*
* @throws BadMethodCallException if the operation did not yield an iterator.
*/
public function getIterator(): Iterator;

/**
* Returns the first result only from the operation
*
* Note that the behaviour of fetching the first result is dependent on the
* implementation. A separate operation might be involved.
*/
public function getSingleResult(): mixed;
}
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 @@ -2246,7 +2246,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
5 changes: 4 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BadMethodCallException;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use GeoJson\Geometry\Geometry;
use GeoJson\Geometry\Point;
Expand Down Expand Up @@ -659,8 +660,10 @@ public function getNewObj(): array
* Gets the Query executable.
*
* @param array<string, mixed> $options
*
* @return Query
*/
public function getQuery(array $options = []): Query
public function getQuery(array $options = []): IterableResult
{
$documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);

Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/ODM/MongoDB/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\CachingIterator;
use Doctrine\ODM\MongoDB\Iterator\HydratingIterator;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Iterator\PrimingIterator;
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\UnitOfWork;
use InvalidArgumentException;
use IteratorAggregate;
use MongoDB\Collection;
use MongoDB\DeleteResult;
use MongoDB\Driver\ReadPreference;
Expand Down Expand Up @@ -63,7 +63,7 @@
* @phpstan-import-type Hints from UnitOfWork
* @phpstan-import-type SortMeta from Sort
*/
final class Query implements IteratorAggregate
final class Query implements IterableResult
{
public const TYPE_FIND = 1;
public const TYPE_FIND_AND_UPDATE = 2;
Expand Down Expand Up @@ -200,7 +200,7 @@ public function debug(?string $name = null)
*
* @throws MongoDBException
*/
public function execute()
public function execute(): mixed
{
$results = $this->runQuery();

Expand Down Expand Up @@ -296,7 +296,7 @@ public function getQuery(): array
*
* @return array<string, mixed>|object|null
*/
public function getSingleResult()
public function getSingleResult(): mixed
{
$clonedQuery = clone $this;
$clonedQuery->query['limit'] = 1;
Expand Down Expand Up @@ -353,7 +353,7 @@ public function setRewindable(bool $rewindable = true): void
/**
* Execute the query and return its results as an array.
*
* @see IteratorAggregate::toArray()
* @see Iterator::toArray()
*
* @return mixed[]
*/
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 @@ -2980,7 +2980,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 @@ -652,7 +652,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 3820d20

Please sign in to comment.