Skip to content

Commit

Permalink
Add iterable types
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Apr 30, 2022
1 parent ea51247 commit b39b2d9
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 56 deletions.
8 changes: 6 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB\Aggregation;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
Expand All @@ -26,6 +27,8 @@

/**
* Fluent interface for building aggregation pipelines.
*
* @psalm-import-type SortShape from Sort
*/
class Builder
{
Expand Down Expand Up @@ -533,8 +536,9 @@ public function skip(int $skip): Stage\Skip
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/sort/
*
* @param array<string, int|string>|string $fieldName Field name or array of field/order pairs
* @param int|string $order Field order (if one field is specified)
* @param array<string, int|string|array<string, string>>|string $fieldName Field name or array of field/order pairs
* @param int|string|null $order Field order (if one field is specified)
* @psalm-param SortShape|string $fieldName Field name or array of field/order pairs
*/
public function sort($fieldName, $order = null): Stage\Sort
{
Expand Down
12 changes: 8 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@

/**
* Fluent interface for adding a $sort stage to an aggregation pipeline.
*
* @psalm-type SortMeta = array{"$meta": 'textScore'}
* @psalm-type SortShape = array<string, int|SortMeta|string>
*/
class Sort extends Stage
{
/** @var array<string, -1|1|array{"$meta": string}> */
/** @var array<string, -1|1|SortMeta> */
private $sort = [];

/**
* @param array<string, int|string>|string $fieldName Field name or array of field/order pairs
* @param int|string $order Field order (if one field is specified)
* @param array<string, int|string|array<string, string>>|string $fieldName Field name or array of field/order pairs
* @param int|string $order Field order (if one field is specified)
* @psalm-param SortShape|string $fieldName
*/
public function __construct(Builder $builder, $fieldName, $order = null)
{
Expand All @@ -34,7 +38,7 @@ public function __construct(Builder $builder, $fieldName, $order = null)

foreach ($fields as $fieldName => $order) {
if (is_string($order)) {
if (in_array($order, $allowedMetaSort)) {
if (in_array($order, $allowedMetaSort, true)) {
$order = ['$meta' => $order];
} else {
$order = strtolower($order) === 'asc' ? 1 : -1;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* $dm = DocumentManager::create(new Connection(), $config);
*
* @psalm-import-type CommitOptions from UnitOfWork
* @psalm-import-type FieldMapping from ClassMetadata
*/
class DocumentManager implements ObjectManager
{
Expand Down Expand Up @@ -760,6 +761,8 @@ public function getConfiguration(): Configuration
/**
* Returns a reference to the supplied document.
*
* @psalm-param FieldMapping $referenceMapping
*
* @return mixed The reference for the document in question, according to the desired mapping
*
* @throws MappingException
Expand Down Expand Up @@ -816,8 +819,10 @@ public function createReference(object $document, array $referenceMapping)
*
* @param array $referenceMapping Mappings of reference for which discriminator data is created.
* @param ClassMetadata $class Metadata of reference document class.
* @psalm-param FieldMapping $referenceMapping
*
* @return array with next structure [{discriminator field} => {discriminator value}]
* @psalm-return array<string, class-string>
*
* @throws MappingException When discriminator map is present and reference class in not registered in it.
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@
*
* @see discriminatorField
*
* @var mixed
* @var string|null
* @psalm-var class-string|null
*/
public $discriminatorValue;

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/MongoDBException.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public static function invalidGridFSRepository(string $className): self
}

/**
* @param string|array $expected
* @param mixed $got
* @param string|string[] $expected
* @param mixed $got
*
* @return MongoDBException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

use Doctrine\Common\Collections\Collection as BaseCollection;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;

/**
* Interface for persistent collection classes factory.
*
* @psalm-import-type FieldMapping from ClassMetadata
*/
interface PersistentCollectionFactory
{
/**
* Creates specified persistent collection to work with given collection class.
*
* @psalm-param FieldMapping $mapping
*/
public function create(DocumentManager $dm, array $mapping, ?BaseCollection $coll = null): PersistentCollectionInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\UnitOfWork;
use Doctrine\Persistence\Mapping\ClassMetadata;

/**
Expand All @@ -15,6 +16,7 @@
* @internal
*
* @psalm-import-type FieldMapping from \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
* @psalm-import-type Hints from UnitOfWork
*
* @template TKey of array-key
* @template T of object
Expand All @@ -32,7 +34,7 @@ public function setDocumentManager(DocumentManager $dm);
/**
* Sets the array of raw mongo data that will be used to initialize this collection.
*
* @param array $mongoData
* @param mixed[] $mongoData
*
* @return void
*/
Expand All @@ -41,14 +43,14 @@ public function setMongoData(array $mongoData);
/**
* Gets the array of raw mongo data that will be used to initialize this collection.
*
* @return array $mongoData
* @return mixed[] $mongoData
*/
public function getMongoData();

/**
* Set hints to account for during reconstitution/lookup of the documents.
*
* @param array $hints
* @param Hints $hints
*
* @return void
*/
Expand All @@ -57,7 +59,7 @@ public function setHints(array $hints);
/**
* Get hints to account for during reconstitution/lookup of the documents.
*
* @return array $hints
* @return Hints $hints
*/
public function getHints();

Expand Down Expand Up @@ -117,31 +119,31 @@ public function clearSnapshot();
/**
* Returns the last snapshot of the elements in the collection.
*
* @return array The last snapshot of the elements.
* @return object[] The last snapshot of the elements.
*/
public function getSnapshot();

/**
* @return array
* @return array<string, object>
*/
public function getDeleteDiff();

/**
* Get objects that were removed, unlike getDeleteDiff this doesn't care about indices.
*
* @return array
* @return list<object>
*/
public function getDeletedDocuments();

/**
* @return array
* @return array<string, object>
*/
public function getInsertDiff();

/**
* Get objects that were added, unlike getInsertDiff this doesn't care about indices.
*
* @return array
* @return list<object>
*/
public function getInsertedDocuments();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
/**
* Trait with methods needed to implement PersistentCollectionInterface.
*
* @psalm-import-type Hints from UnitOfWork
* @psalm-import-type FieldMapping from ClassMetadata
* @template TKey of array-key
* @template T of object
Expand Down Expand Up @@ -92,14 +93,15 @@ trait PersistentCollectionTrait
/**
* The raw mongo data that will be used to initialize this collection.
*
* @var array
* @var mixed[]
*/
private $mongoData = [];

/**
* Any hints to account for during reconstitution/lookup of the documents.
*
* @var array
* @psalm-var Hints
*/
private $hints = [];

Expand Down
35 changes: 20 additions & 15 deletions lib/Doctrine/ODM/MongoDB/Persisters/CollectionPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(DocumentManager $dm, PersistenceBuilder $pb, UnitOfW
* Deletes a PersistentCollection instances completely from a document using $unset.
*
* @param PersistentCollectionInterface[] $collections
* @param array $options
* @param array<string, mixed> $options
*/
public function delete(object $parent, array $collections, array $options): void
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public function delete(object $parent, array $collections, array $options): void
* Updates a list PersistentCollection instances deleting removed rows and inserting new rows.
*
* @param PersistentCollectionInterface[] $collections
* @param array $options
* @param array<string, mixed> $options
*/
public function update(object $parent, array $collections, array $options): void
{
Expand Down Expand Up @@ -155,7 +155,7 @@ public function update(object $parent, array $collections, array $options): void
* reindexed numerically before storage.
*
* @param PersistentCollectionInterface[] $collections
* @param array $options
* @param array<string, mixed> $options
*/
private function setCollections(object $parent, array $collections, array $options): void
{
Expand Down Expand Up @@ -195,7 +195,7 @@ private function setCollections(object $parent, array $collections, array $optio
* This method is intended to be used with the "pushAll" and "addToSet" strategies.
*
* @param PersistentCollectionInterface[] $collections
* @param array $options
* @param array<string, mixed> $options
*/
private function deleteElements(object $parent, array $collections, array $options): void
{
Expand Down Expand Up @@ -258,7 +258,7 @@ private function deleteElements(object $parent, array $collections, array $optio
* This method is intended to be used with the "pushAll" and "addToSet" strategies.
*
* @param PersistentCollectionInterface[] $collections
* @param array $options
* @param array<string, mixed> $options
*/
private function insertElements(object $parent, array $collections, array $options): void
{
Expand Down Expand Up @@ -328,11 +328,11 @@ private function insertElements(object $parent, array $collections, array $optio
/**
* Perform collections update for 'pushAll' strategy.
*
* @param object $parent Parent object to which passed collections is belong.
* @param array $collsPaths Paths of collections that is passed.
* @param array $pathCollsMap List of collections indexed by their paths.
* @param array $diffsMap List of collection diffs indexed by collections paths.
* @param array $options
* @param object $parent Parent object to which passed collections is belong.
* @param string[] $collsPaths Paths of collections that is passed.
* @param array<string, PersistentCollectionInterface<array-key, object>> $pathCollsMap List of collections indexed by their paths.
* @param array<string, mixed[]> $diffsMap List of collection diffs indexed by collections paths.
* @param array<string, mixed> $options
*/
private function pushAllCollections(object $parent, array $collsPaths, array $pathCollsMap, array $diffsMap, array $options): void
{
Expand Down Expand Up @@ -362,11 +362,11 @@ private function pushAllCollections(object $parent, array $collsPaths, array $pa
/**
* Perform collections update by 'addToSet' strategy.
*
* @param object $parent Parent object to which passed collections is belong.
* @param array $collsPaths Paths of collections that is passed.
* @param array $pathCollsMap List of collections indexed by their paths.
* @param array $diffsMap List of collection diffs indexed by collections paths.
* @param array $options
* @param object $parent Parent object to which passed collections is belong.
* @param string[] $collsPaths Paths of collections that is passed.
* @param array<string, PersistentCollectionInterface<array-key, object>> $pathCollsMap List of collections indexed by their paths.
* @param array<string, mixed[]> $diffsMap List of collection diffs indexed by collections paths.
* @param array<string, mixed> $options
*/
private function addToSetCollections(object $parent, array $collsPaths, array $pathCollsMap, array $diffsMap, array $options): void
{
Expand Down Expand Up @@ -416,6 +416,8 @@ private function getValuePrepareCallback(PersistentCollectionInterface $coll): C
* <code>
* list($path, $parent) = $this->getPathAndParent($coll)
* </code>
*
* @return array{string, object|null}
*/
private function getPathAndParent(PersistentCollectionInterface $coll): array
{
Expand Down Expand Up @@ -443,6 +445,9 @@ private function getPathAndParent(PersistentCollectionInterface $coll): array

/**
* Executes a query updating the given document.
*
* @param array<string, mixed> $newObj
* @param array<string, mixed> $options
*/
private function executeQuery(object $document, array $newObj, array $options): void
{
Expand Down
Loading

0 comments on commit b39b2d9

Please sign in to comment.