Completed
Pull Request — master (#1984)
by Maciej
22:01
created

DocumentPersister::hasQueryOperators()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7.5375

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 8.8333
c 0
b 0
f 0
cc 7
nc 7
nop 1
crap 7.5375
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Persisters;
6
7
use BadMethodCallException;
8
use DateTime;
9
use Doctrine\Common\Persistence\Mapping\MappingException;
10
use Doctrine\ODM\MongoDB\DocumentManager;
11
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
12
use Doctrine\ODM\MongoDB\Iterator\CachingIterator;
13
use Doctrine\ODM\MongoDB\Iterator\HydratingIterator;
14
use Doctrine\ODM\MongoDB\Iterator\Iterator;
15
use Doctrine\ODM\MongoDB\Iterator\PrimingIterator;
16
use Doctrine\ODM\MongoDB\LockException;
17
use Doctrine\ODM\MongoDB\LockMode;
18
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
19
use Doctrine\ODM\MongoDB\MongoDBException;
20
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionException;
21
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
22
use Doctrine\ODM\MongoDB\Query\CriteriaMerger;
23
use Doctrine\ODM\MongoDB\Query\Query;
24
use Doctrine\ODM\MongoDB\Query\ReferencePrimer;
25
use Doctrine\ODM\MongoDB\Types\Type;
26
use Doctrine\ODM\MongoDB\UnitOfWork;
27
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
28
use InvalidArgumentException;
29
use MongoDB\BSON\ObjectId;
30
use MongoDB\Collection;
31
use MongoDB\Driver\Cursor;
32
use MongoDB\Driver\Exception\Exception as DriverException;
33
use MongoDB\Driver\Exception\WriteException;
34
use MongoDB\GridFS\Bucket;
35
use ProxyManager\Proxy\GhostObjectInterface;
36
use stdClass;
37
use function array_combine;
38
use function array_fill;
39
use function array_intersect_key;
40
use function array_keys;
41
use function array_map;
42
use function array_merge;
43
use function array_search;
44
use function array_slice;
45
use function array_values;
46
use function assert;
47
use function count;
48
use function explode;
49
use function get_class;
50
use function get_object_vars;
51
use function implode;
52
use function in_array;
53
use function is_array;
54
use function is_object;
55
use function is_scalar;
56
use function is_string;
57
use function max;
58
use function spl_object_hash;
59
use function sprintf;
60
use function strpos;
61
use function strtolower;
62
63
/**
64
 * The DocumentPersister is responsible for persisting documents.
65
 *
66
 * @internal
67
 */
68
final class DocumentPersister
69
{
70
    /** @var PersistenceBuilder */
71
    private $pb;
72
73
    /** @var DocumentManager */
74
    private $dm;
75
76
    /** @var UnitOfWork */
77
    private $uow;
78
79
    /** @var ClassMetadata */
80
    private $class;
81
82
    /** @var Collection|null */
83
    private $collection;
84
85
    /** @var Bucket|null */
86
    private $bucket;
87
88
    /**
89
     * Array of queued inserts for the persister to insert.
90
     *
91
     * @var array
92
     */
93
    private $queuedInserts = [];
94
95
    /**
96
     * Array of queued inserts for the persister to insert.
97
     *
98
     * @var array
99
     */
100
    private $queuedUpserts = [];
101
102
    /** @var CriteriaMerger */
103
    private $cm;
104
105
    /** @var CollectionPersister */
106
    private $cp;
107
108
    /** @var HydratorFactory */
109
    private $hydratorFactory;
110
111 1197
    public function __construct(
112
        PersistenceBuilder $pb,
113
        DocumentManager $dm,
114
        UnitOfWork $uow,
115
        HydratorFactory $hydratorFactory,
116
        ClassMetadata $class,
117
        ?CriteriaMerger $cm = null
118
    ) {
119 1197
        $this->pb              = $pb;
120 1197
        $this->dm              = $dm;
121 1197
        $this->cm              = $cm ?: new CriteriaMerger();
122 1197
        $this->uow             = $uow;
123 1197
        $this->hydratorFactory = $hydratorFactory;
124 1197
        $this->class           = $class;
125 1197
        $this->cp              = $this->uow->getCollectionPersister();
126
127 1197
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
128 93
            return;
129
        }
130
131 1194
        $this->collection = $dm->getDocumentCollection($class->name);
132
133 1194
        if (! $class->isFile) {
134 1183
            return;
135
        }
136
137 18
        $this->bucket = $dm->getDocumentBucket($class->name);
138 18
    }
139
140
    public function getInserts() : array
141
    {
142
        return $this->queuedInserts;
143
    }
144
145
    public function isQueuedForInsert(object $document) : bool
146
    {
147
        return isset($this->queuedInserts[spl_object_hash($document)]);
148
    }
149
150
    /**
151
     * Adds a document to the queued insertions.
152
     * The document remains queued until {@link executeInserts} is invoked.
153
     */
154 531
    public function addInsert(object $document) : void
155
    {
156 531
        $this->queuedInserts[spl_object_hash($document)] = $document;
157 531
    }
158
159
    public function getUpserts() : array
160
    {
161
        return $this->queuedUpserts;
162
    }
163
164
    public function isQueuedForUpsert(object $document) : bool
165
    {
166
        return isset($this->queuedUpserts[spl_object_hash($document)]);
167
    }
168
169
    /**
170
     * Adds a document to the queued upserts.
171
     * The document remains queued until {@link executeUpserts} is invoked.
172
     */
173 85
    public function addUpsert(object $document) : void
174
    {
175 85
        $this->queuedUpserts[spl_object_hash($document)] = $document;
176 85
    }
177
178
    /**
179
     * Gets the ClassMetadata instance of the document class this persister is
180
     * used for.
181
     */
182
    public function getClassMetadata() : ClassMetadata
183
    {
184
        return $this->class;
185
    }
186
187
    /**
188
     * Executes all queued document insertions.
189
     *
190
     * Queued documents without an ID will inserted in a batch and queued
191
     * documents with an ID will be upserted individually.
192
     *
193
     * If no inserts are queued, invoking this method is a NOOP.
194
     *
195
     * @throws DriverException
196
     */
197 531
    public function executeInserts(array $options = []) : void
198
    {
199 531
        if (! $this->queuedInserts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->queuedInserts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
200
            return;
201
        }
202
203 531
        $inserts = [];
204 531
        $options = $this->getWriteOptions($options);
205 531
        foreach ($this->queuedInserts as $oid => $document) {
206 531
            $data = $this->pb->prepareInsertData($document);
207
208
            // Set the initial version for each insert
209 520
            if ($this->class->isVersioned) {
210 40
                $versionMapping = $this->class->fieldMappings[$this->class->versionField];
211 40
                $nextVersion    = null;
212 40
                if ($versionMapping['type'] === 'int') {
213 38
                    $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document));
214 38
                    $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
215 2
                } elseif ($versionMapping['type'] === 'date') {
216 2
                    $nextVersionDateTime = new DateTime();
217 2
                    $nextVersion         = Type::convertPHPToDatabaseValue($nextVersionDateTime);
218 2
                    $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime);
219
                }
220 40
                $data[$versionMapping['name']] = $nextVersion;
221
            }
222
223 520
            $inserts[] = $data;
224
        }
225
226 520
        if ($inserts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inserts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
227
            try {
228 520
                assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
229 520
                $this->collection->insertMany($inserts, $options);
230 6
            } catch (DriverException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
231 6
                $this->queuedInserts = [];
232 6
                throw $e;
233
            }
234
        }
235
236
        /* All collections except for ones using addToSet have already been
237
         * saved. We have left these to be handled separately to avoid checking
238
         * collection for uniqueness on PHP side.
239
         */
240 520
        foreach ($this->queuedInserts as $document) {
241 520
            $this->handleCollections($document, $options);
242
        }
243
244 520
        $this->queuedInserts = [];
245 520
    }
246
247
    /**
248
     * Executes all queued document upserts.
249
     *
250
     * Queued documents with an ID are upserted individually.
251
     *
252
     * If no upserts are queued, invoking this method is a NOOP.
253
     */
254 85
    public function executeUpserts(array $options = []) : void
255
    {
256 85
        if (! $this->queuedUpserts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->queuedUpserts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
257
            return;
258
        }
259
260 85
        $options = $this->getWriteOptions($options);
261 85
        foreach ($this->queuedUpserts as $oid => $document) {
262
            try {
263 85
                $this->executeUpsert($document, $options);
264 85
                $this->handleCollections($document, $options);
265 85
                unset($this->queuedUpserts[$oid]);
266
            } catch (WriteException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\WriteException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
267
                unset($this->queuedUpserts[$oid]);
268
                throw $e;
269
            }
270
        }
271 85
    }
272
273
    /**
274
     * Executes a single upsert in {@link executeUpserts}
275
     */
276 85
    private function executeUpsert(object $document, array $options) : void
277
    {
278 85
        $options['upsert'] = true;
279 85
        $criteria          = $this->getQueryForDocument($document);
280
281 85
        $data = $this->pb->prepareUpsertData($document);
282
283
        // Set the initial version for each upsert
284 85
        if ($this->class->isVersioned) {
285 3
            $versionMapping = $this->class->fieldMappings[$this->class->versionField];
286 3
            $nextVersion    = null;
287 3
            if ($versionMapping['type'] === 'int') {
288 2
                $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document));
289 2
                $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
290 1
            } elseif ($versionMapping['type'] === 'date') {
291 1
                $nextVersionDateTime = new DateTime();
292 1
                $nextVersion         = Type::convertPHPToDatabaseValue($nextVersionDateTime);
293 1
                $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime);
294
            }
295 3
            $data['$set'][$versionMapping['name']] = $nextVersion;
296
        }
297
298 85
        foreach (array_keys($criteria) as $field) {
299 85
            unset($data['$set'][$field]);
300 85
            unset($data['$inc'][$field]);
301 85
            unset($data['$setOnInsert'][$field]);
302
        }
303
304
        // Do not send empty update operators
305 85
        foreach (['$set', '$inc', '$setOnInsert'] as $operator) {
306 85
            if (! empty($data[$operator])) {
307 70
                continue;
308
            }
309
310 85
            unset($data[$operator]);
311
        }
312
313
        /* If there are no modifiers remaining, we're upserting a document with
314
         * an identifier as its only field. Since a document with the identifier
315
         * may already exist, the desired behavior is "insert if not exists" and
316
         * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set
317
         * the identifier to the same value in our criteria.
318
         *
319
         * This will fail for versions before MongoDB 2.6, which require an
320
         * empty $set modifier. The best we can do (without attempting to check
321
         * server versions in advance) is attempt the 2.6+ behavior and retry
322
         * after the relevant exception.
323
         *
324
         * See: https://jira.mongodb.org/browse/SERVER-12266
325
         */
326 85
        if (empty($data)) {
327 16
            $retry = true;
328 16
            $data  = ['$set' => ['_id' => $criteria['_id']]];
329
        }
330
331
        try {
332 85
            assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
333 85
            $this->collection->updateOne($criteria, $data, $options);
334 85
            return;
335
        } catch (WriteException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\WriteException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
336
            if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) {
337
                throw $e;
338
            }
339
        }
340
341
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
342
        $this->collection->updateOne($criteria, ['$set' => new stdClass()], $options);
343
    }
344
345
    /**
346
     * Updates the already persisted document if it has any new changesets.
347
     *
348
     * @throws LockException
349
     */
350 234
    public function update(object $document, array $options = []) : void
351
    {
352 234
        $update = $this->pb->prepareUpdateData($document);
353
354 234
        $query = $this->getQueryForDocument($document);
355
356 232
        foreach (array_keys($query) as $field) {
357 232
            unset($update['$set'][$field]);
358
        }
359
360 232
        if (empty($update['$set'])) {
361 101
            unset($update['$set']);
362
        }
363
364
        // Include versioning logic to set the new version value in the database
365
        // and to ensure the version has not changed since this document object instance
366
        // was fetched from the database
367 232
        $nextVersion = null;
368 232
        if ($this->class->isVersioned) {
369 33
            $versionMapping = $this->class->fieldMappings[$this->class->versionField];
370 33
            $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document);
371 33
            if ($versionMapping['type'] === 'int') {
372 30
                $nextVersion                             = $currentVersion + 1;
373 30
                $update['$inc'][$versionMapping['name']] = 1;
374 30
                $query[$versionMapping['name']]          = $currentVersion;
375 3
            } elseif ($versionMapping['type'] === 'date') {
376 3
                $nextVersion                             = new DateTime();
377 3
                $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion);
378 3
                $query[$versionMapping['name']]          = Type::convertPHPToDatabaseValue($currentVersion);
379
            }
380
        }
381
382 232
        if (! empty($update)) {
383
            // Include locking logic so that if the document object in memory is currently
384
            // locked then it will remove it, otherwise it ensures the document is not locked.
385 158
            if ($this->class->isLockable) {
386 11
                $isLocked    = $this->class->reflFields[$this->class->lockField]->getValue($document);
387 11
                $lockMapping = $this->class->fieldMappings[$this->class->lockField];
388 11
                if ($isLocked) {
389 2
                    $update['$unset'] = [$lockMapping['name'] => true];
390
                } else {
391 9
                    $query[$lockMapping['name']] = ['$exists' => false];
392
                }
393
            }
394
395 158
            $options = $this->getWriteOptions($options);
396
397 158
            assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
398 158
            $result = $this->collection->updateOne($query, $update, $options);
399
400 158
            if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) {
401 6
                throw LockException::lockFailed($document);
402 153
            } elseif ($this->class->isVersioned) {
403 28
                $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
404
            }
405
        }
406
407 227
        $this->handleCollections($document, $options);
408 227
    }
409
410
    /**
411
     * Removes document from mongo
412
     *
413
     * @throws LockException
414
     */
415 36
    public function delete(object $document, array $options = []) : void
416
    {
417 36
        if ($this->bucket instanceof Bucket) {
0 ignored issues
show
Bug introduced by
The class MongoDB\GridFS\Bucket does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
418 1
            $documentIdentifier = $this->uow->getDocumentIdentifier($document);
419 1
            $databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier);
420
421 1
            $this->bucket->delete($databaseIdentifier);
422
423 1
            return;
424
        }
425
426 35
        $query = $this->getQueryForDocument($document);
427
428 35
        if ($this->class->isLockable) {
429 2
            $query[$this->class->lockField] = ['$exists' => false];
430
        }
431
432 35
        $options = $this->getWriteOptions($options);
433
434 35
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
435 35
        $result = $this->collection->deleteOne($query, $options);
436
437 35
        if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) {
438 2
            throw LockException::lockFailed($document);
439
        }
440 33
    }
441
442
    /**
443
     * Refreshes a managed document.
444
     */
445 23
    public function refresh(object $document) : void
446
    {
447 23
        $query = $this->getQueryForDocument($document);
448 23
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
449 23
        $data  = $this->collection->findOne($query);
450 23
        if ($data === null) {
451
            throw MongoDBException::cannotRefreshDocument();
452
        }
453 23
        $data = $this->hydratorFactory->hydrate($document, (array) $data);
454 23
        $this->uow->setOriginalDocumentData($document, $data);
455 23
    }
456
457
    /**
458
     * Finds a document by a set of criteria.
459
     *
460
     * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will
461
     * be used to match an _id value.
462
     *
463
     * @param mixed $criteria Query criteria
464
     *
465
     * @throws LockException
466
     *
467
     * @todo Check identity map? loadById method? Try to guess whether
468
     *     $criteria is the id?
469
     */
470 366
    public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null) : ?object
0 ignored issues
show
Unused Code introduced by
The parameter $lockMode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
471
    {
472
        // TODO: remove this
473 366
        if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\ObjectId does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
474
            $criteria = ['_id' => $criteria];
475
        }
476
477 366
        $criteria = $this->prepareQueryOrNewObj($criteria);
0 ignored issues
show
Bug introduced by
It seems like $criteria can also be of type object; however, Doctrine\ODM\MongoDB\Per...:prepareQueryOrNewObj() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
478 366
        $criteria = $this->addDiscriminatorToPreparedQuery($criteria);
479 366
        $criteria = $this->addFilterToPreparedQuery($criteria);
480
481 366
        $options = [];
482 366
        if ($sort !== null) {
483 95
            $options['sort'] = $this->prepareSort($sort);
484
        }
485 366
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
486 366
        $result = $this->collection->findOne($criteria, $options);
487 366
        $result = $result !== null ? (array) $result : null;
488
489 366
        if ($this->class->isLockable) {
490 1
            $lockMapping = $this->class->fieldMappings[$this->class->lockField];
491 1
            if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) {
492 1
                throw LockException::lockFailed($document);
493
            }
494
        }
495
496 365
        if ($result === null) {
497 115
            return null;
498
        }
499
500 321
        return $this->createDocument($result, $document, $hints);
501
    }
502
503
    /**
504
     * Finds documents by a set of criteria.
505
     */
506 22
    public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit = null, ?int $skip = null) : Iterator
507
    {
508 22
        $criteria = $this->prepareQueryOrNewObj($criteria);
509 22
        $criteria = $this->addDiscriminatorToPreparedQuery($criteria);
510 22
        $criteria = $this->addFilterToPreparedQuery($criteria);
511
512 22
        $options = [];
513 22
        if ($sort !== null) {
514 11
            $options['sort'] = $this->prepareSort($sort);
515
        }
516
517 22
        if ($limit !== null) {
518 10
            $options['limit'] = $limit;
519
        }
520
521 22
        if ($skip !== null) {
522 1
            $options['skip'] = $skip;
523
        }
524
525 22
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
526 22
        $baseCursor = $this->collection->find($criteria, $options);
527 22
        return $this->wrapCursor($baseCursor);
528
    }
529
530
    /**
531
     * @throws MongoDBException
532
     */
533 314
    private function getShardKeyQuery(object $document) : array
534
    {
535 314
        if (! $this->class->isSharded()) {
536 304
            return [];
537
        }
538
539 10
        $shardKey = $this->class->getShardKey();
540 10
        $keys     = array_keys($shardKey['keys']);
541 10
        $data     = $this->uow->getDocumentActualData($document);
542
543 10
        $shardKeyQueryPart = [];
544 10
        foreach ($keys as $key) {
545 10
            assert(is_string($key));
546 10
            $mapping = $this->class->getFieldMappingByDbFieldName($key);
547 10
            $this->guardMissingShardKey($document, $key, $data);
548
549 8
            if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
550 1
                $reference = $this->prepareReference(
551 1
                    $key,
552 1
                    $data[$mapping['fieldName']],
553 1
                    $mapping,
554 1
                    false
555
                );
556 1
                foreach ($reference as $keyValue) {
557 1
                    $shardKeyQueryPart[$keyValue[0]] = $keyValue[1];
558
                }
559
            } else {
560 7
                $value                   = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]);
561 7
                $shardKeyQueryPart[$key] = $value;
562
            }
563
        }
564
565 8
        return $shardKeyQueryPart;
566
    }
567
568
    /**
569
     * Wraps the supplied base cursor in the corresponding ODM class.
570
     */
571 22
    private function wrapCursor(Cursor $baseCursor) : Iterator
572
    {
573 22
        return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class));
574
    }
575
576
    /**
577
     * Checks whether the given managed document exists in the database.
578
     */
579 3
    public function exists(object $document) : bool
580
    {
581 3
        $id = $this->class->getIdentifierObject($document);
582 3
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
583 3
        return (bool) $this->collection->findOne(['_id' => $id], ['_id']);
584
    }
585
586
    /**
587
     * Locks document by storing the lock mode on the mapped lock field.
588
     */
589 5
    public function lock(object $document, int $lockMode) : void
590
    {
591 5
        $id          = $this->uow->getDocumentIdentifier($document);
592 5
        $criteria    = ['_id' => $this->class->getDatabaseIdentifierValue($id)];
593 5
        $lockMapping = $this->class->fieldMappings[$this->class->lockField];
594 5
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
595 5
        $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]);
596 5
        $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode);
597 5
    }
598
599
    /**
600
     * Releases any lock that exists on this document.
601
     */
602 1
    public function unlock(object $document) : void
603
    {
604 1
        $id          = $this->uow->getDocumentIdentifier($document);
605 1
        $criteria    = ['_id' => $this->class->getDatabaseIdentifierValue($id)];
606 1
        $lockMapping = $this->class->fieldMappings[$this->class->lockField];
607 1
        assert($this->collection instanceof Collection);
0 ignored issues
show
Bug introduced by
The class MongoDB\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
608 1
        $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]);
609 1
        $this->class->reflFields[$this->class->lockField]->setValue($document, null);
610 1
    }
611
612
    /**
613
     * Creates or fills a single document object from an query result.
614
     *
615
     * @param array  $result   The query result.
616
     * @param object $document The document object to fill, if any.
617
     * @param array  $hints    Hints for document creation.
618
     *
619
     * @return object|null The filled and managed document object or NULL, if the query result is empty.
620
     */
621 321
    private function createDocument(array $result, ?object $document = null, array $hints = []) : ?object
622
    {
623 321
        if ($document !== null) {
624 26
            $hints[Query::HINT_REFRESH] = true;
625 26
            $id                         = $this->class->getPHPIdentifierValue($result['_id']);
626 26
            $this->uow->registerManaged($document, $id, $result);
627
        }
628
629 321
        return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document);
630
    }
631
632
    /**
633
     * Loads a PersistentCollection data. Used in the initialize() method.
634
     */
635 178
    public function loadCollection(PersistentCollectionInterface $collection) : void
636
    {
637 178
        $mapping = $collection->getMapping();
638 178
        switch ($mapping['association']) {
639
            case ClassMetadata::EMBED_MANY:
640 126
                $this->loadEmbedManyCollection($collection);
641 126
                break;
642
643
            case ClassMetadata::REFERENCE_MANY:
644 75
                if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) {
645 5
                    $this->loadReferenceManyWithRepositoryMethod($collection);
646
                } else {
647 71
                    if ($mapping['isOwningSide']) {
648 59
                        $this->loadReferenceManyCollectionOwningSide($collection);
649
                    } else {
650 17
                        $this->loadReferenceManyCollectionInverseSide($collection);
651
                    }
652
                }
653 75
                break;
654
        }
655 178
    }
656
657 126
    private function loadEmbedManyCollection(PersistentCollectionInterface $collection) : void
658
    {
659 126
        $embeddedDocuments = $collection->getMongoData();
660 126
        $mapping           = $collection->getMapping();
661 126
        $owner             = $collection->getOwner();
662 126
        if (! $embeddedDocuments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $embeddedDocuments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
663 75
            return;
664
        }
665
666 97
        foreach ($embeddedDocuments as $key => $embeddedDocument) {
667 97
            $className              = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument);
668 97
            $embeddedMetadata       = $this->dm->getClassMetadata($className);
669 97
            $embeddedDocumentObject = $embeddedMetadata->newInstance();
670
671 97
            $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key);
672
673 97
            $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints());
674 97
            $id   = $data[$embeddedMetadata->identifier] ?? null;
675
676 97
            if (empty($collection->getHints()[Query::HINT_READ_ONLY])) {
677 96
                $this->uow->registerManaged($embeddedDocumentObject, $id, $data);
678
            }
679 97
            if (CollectionHelper::isHash($mapping['strategy'])) {
680 25
                $collection->set($key, $embeddedDocumentObject);
681
            } else {
682 80
                $collection->add($embeddedDocumentObject);
683
            }
684
        }
685 97
    }
686
687 59
    private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) : void
688
    {
689 59
        $hints      = $collection->getHints();
690 59
        $mapping    = $collection->getMapping();
691 59
        $groupedIds = [];
692
693 59
        $sorted = isset($mapping['sort']) && $mapping['sort'];
694
695 59
        foreach ($collection->getMongoData() as $key => $reference) {
696 53
            $className  = $this->uow->getClassNameForAssociation($mapping, $reference);
697 53
            $identifier = ClassMetadata::getReferenceId($reference, $mapping['storeAs']);
698 53
            $id         = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($identifier);
699
700
            // create a reference to the class and id
701 53
            $reference = $this->dm->getReference($className, $id);
702
703
            // no custom sort so add the references right now in the order they are embedded
704 53
            if (! $sorted) {
705 52
                if (CollectionHelper::isHash($mapping['strategy'])) {
706 2
                    $collection->set($key, $reference);
707
                } else {
708 50
                    $collection->add($reference);
709
                }
710
            }
711
712
            // only query for the referenced object if it is not already initialized or the collection is sorted
713 53
            if (! (($reference instanceof GhostObjectInterface && ! $reference->isProxyInitialized())) && ! $sorted) {
0 ignored issues
show
Bug introduced by
The class ProxyManager\Proxy\GhostObjectInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
714 22
                continue;
715
            }
716
717 38
            $groupedIds[$className][] = $identifier;
718
        }
719 59
        foreach ($groupedIds as $className => $ids) {
720 38
            $class           = $this->dm->getClassMetadata($className);
721 38
            $mongoCollection = $this->dm->getDocumentCollection($className);
722 38
            $criteria        = $this->cm->merge(
723 38
                ['_id' => ['$in' => array_values($ids)]],
724 38
                $this->dm->getFilterCollection()->getFilterCriteria($class),
725 38
                $mapping['criteria'] ?? []
726
            );
727 38
            $criteria        = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria);
728
729 38
            $options = [];
730 38
            if (isset($mapping['sort'])) {
731 38
                $options['sort'] = $this->prepareSort($mapping['sort']);
732
            }
733 38
            if (isset($mapping['limit'])) {
734
                $options['limit'] = $mapping['limit'];
735
            }
736 38
            if (isset($mapping['skip'])) {
737
                $options['skip'] = $mapping['skip'];
738
            }
739 38
            if (! empty($hints[Query::HINT_READ_PREFERENCE])) {
740
                $options['readPreference'] = $hints[Query::HINT_READ_PREFERENCE];
741
            }
742
743 38
            $cursor    = $mongoCollection->find($criteria, $options);
744 38
            $documents = $cursor->toArray();
745 38
            foreach ($documents as $documentData) {
746 37
                $document = $this->uow->getById($documentData['_id'], $class);
747 37
                if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) {
0 ignored issues
show
Bug introduced by
The class ProxyManager\Proxy\GhostObjectInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
748 37
                    $data = $this->hydratorFactory->hydrate($document, $documentData);
749 37
                    $this->uow->setOriginalDocumentData($document, $data);
750
                }
751
752 37
                if (! $sorted) {
753 36
                    continue;
754
                }
755
756 1
                $collection->add($document);
757
            }
758
        }
759 59
    }
760
761 17
    private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) : void
762
    {
763 17
        $query    = $this->createReferenceManyInverseSideQuery($collection);
764 17
        $iterator = $query->execute();
765 17
        assert($iterator instanceof Iterator);
766 17
        $documents = $iterator->toArray();
767 17
        foreach ($documents as $key => $document) {
768 16
            $collection->add($document);
769
        }
770 17
    }
771
772 17
    public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) : Query
773
    {
774 17
        $hints   = $collection->getHints();
775 17
        $mapping = $collection->getMapping();
776 17
        $owner   = $collection->getOwner();
777
778 17
        if ($owner === null) {
779
            throw PersistentCollectionException::ownerRequiredToLoadCollection();
780
        }
781
782 17
        $ownerClass        = $this->dm->getClassMetadata(get_class($owner));
783 17
        $targetClass       = $this->dm->getClassMetadata($mapping['targetDocument']);
784 17
        $mappedByMapping   = $targetClass->fieldMappings[$mapping['mappedBy']] ?? [];
785 17
        $mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF, $mapping['mappedBy']);
786
787 17
        $criteria = $this->cm->merge(
788 17
            [$mappedByFieldName => $ownerClass->getIdentifierObject($owner)],
789 17
            $this->dm->getFilterCollection()->getFilterCriteria($targetClass),
790 17
            $mapping['criteria'] ?? []
791
        );
792 17
        $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria);
793 17
        $qb       = $this->dm->createQueryBuilder($mapping['targetDocument'])
794 17
            ->setQueryArray($criteria);
795
796 17
        if (isset($mapping['sort'])) {
797 17
            $qb->sort($mapping['sort']);
798
        }
799 17
        if (isset($mapping['limit'])) {
800 2
            $qb->limit($mapping['limit']);
801
        }
802 17
        if (isset($mapping['skip'])) {
803
            $qb->skip($mapping['skip']);
804
        }
805
806 17
        if (! empty($hints[Query::HINT_READ_PREFERENCE])) {
807
            $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE]);
808
        }
809
810 17
        foreach ($mapping['prime'] as $field) {
811 4
            $qb->field($field)->prime(true);
812
        }
813
814 17
        return $qb->getQuery();
815
    }
816
817 5
    private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) : void
818
    {
819 5
        $cursor    = $this->createReferenceManyWithRepositoryMethodCursor($collection);
820 5
        $mapping   = $collection->getMapping();
821 5
        $documents = $cursor->toArray();
822 5
        foreach ($documents as $key => $obj) {
823 5
            if (CollectionHelper::isHash($mapping['strategy'])) {
824 1
                $collection->set($key, $obj);
825
            } else {
826 4
                $collection->add($obj);
827
            }
828
        }
829 5
    }
830
831 5
    public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) : Iterator
832
    {
833 5
        $mapping          = $collection->getMapping();
834 5
        $repositoryMethod = $mapping['repositoryMethod'];
835 5
        $cursor           = $this->dm->getRepository($mapping['targetDocument'])
836 5
            ->$repositoryMethod($collection->getOwner());
837
838 5
        if (! $cursor instanceof Iterator) {
839
            throw new BadMethodCallException(sprintf('Expected repository method %s to return an iterable object', $repositoryMethod));
840
        }
841
842 5
        if (! empty($mapping['prime'])) {
843 1
            $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork());
844 1
            $primers         = array_combine($mapping['prime'], array_fill(0, count($mapping['prime']), true));
845 1
            $class           = $this->dm->getClassMetadata($mapping['targetDocument']);
846
847 1
            assert(is_array($primers));
848
849 1
            $cursor = new PrimingIterator($cursor, $class, $referencePrimer, $primers, $collection->getHints());
850
        }
851
852 5
        return $cursor;
853
    }
854
855
    /**
856
     * Prepare a projection array by converting keys, which are PHP property
857
     * names, to MongoDB field names.
858
     */
859 15
    public function prepareProjection(array $fields) : array
860
    {
861 15
        $preparedFields = [];
862
863 15
        foreach ($fields as $key => $value) {
864 15
            $preparedFields[$this->prepareFieldName($key)] = $value;
865
        }
866
867 15
        return $preparedFields;
868
    }
869
870
    /**
871
     * @param int|string $sort
872
     *
873
     * @return int|string|null
874
     */
875 26
    private function getSortDirection($sort)
876
    {
877 26
        switch (strtolower((string) $sort)) {
878 26
            case 'desc':
879 15
                return -1;
880
881 23
            case 'asc':
882 13
                return 1;
883
        }
884
885 13
        return $sort;
886
    }
887
888
    /**
889
     * Prepare a sort specification array by converting keys to MongoDB field
890
     * names and changing direction strings to int.
891
     */
892 142
    public function prepareSort(array $fields) : array
893
    {
894 142
        $sortFields = [];
895
896 142
        foreach ($fields as $key => $value) {
897 26
            if (is_array($value)) {
898 1
                $sortFields[$this->prepareFieldName($key)] = $value;
899
            } else {
900 26
                $sortFields[$this->prepareFieldName($key)] = $this->getSortDirection($value);
901
            }
902
        }
903
904 142
        return $sortFields;
905
    }
906
907
    /**
908
     * Prepare a mongodb field name and convert the PHP property names to
909
     * MongoDB field names.
910
     */
911 459
    public function prepareFieldName(string $fieldName) : string
912
    {
913 459
        $fieldNames = $this->prepareQueryElement($fieldName, null, null, false);
914
915 459
        return $fieldNames[0][0];
916
    }
917
918
    /**
919
     * Adds discriminator criteria to an already-prepared query.
920
     *
921
     * This method should be used once for query criteria and not be used for
922
     * nested expressions. It should be called before
923
     * {@link DocumentPerister::addFilterToPreparedQuery()}.
924
     */
925 522
    public function addDiscriminatorToPreparedQuery(array $preparedQuery) : array
926
    {
927
        /* If the class has a discriminator field, which is not already in the
928
         * criteria, inject it now. The field/values need no preparation.
929
         */
930 522
        if ($this->class->hasDiscriminator() && ! isset($preparedQuery[$this->class->discriminatorField])) {
931 27
            $discriminatorValues = $this->getClassDiscriminatorValues($this->class);
932 27
            if (count($discriminatorValues) === 1) {
933 19
                $preparedQuery[$this->class->discriminatorField] = $discriminatorValues[0];
934
            } else {
935 10
                $preparedQuery[$this->class->discriminatorField] = ['$in' => $discriminatorValues];
936
            }
937
        }
938
939 522
        return $preparedQuery;
940
    }
941
942
    /**
943
     * Adds filter criteria to an already-prepared query.
944
     *
945
     * This method should be used once for query criteria and not be used for
946
     * nested expressions. It should be called after
947
     * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}.
948
     */
949 523
    public function addFilterToPreparedQuery(array $preparedQuery) : array
950
    {
951
        /* If filter criteria exists for this class, prepare it and merge
952
         * over the existing query.
953
         *
954
         * @todo Consider recursive merging in case the filter criteria and
955
         * prepared query both contain top-level $and/$or operators.
956
         */
957 523
        $filterCriteria = $this->dm->getFilterCollection()->getFilterCriteria($this->class);
958 523
        if ($filterCriteria) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filterCriteria of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
959 18
            $preparedQuery = $this->cm->merge($preparedQuery, $this->prepareQueryOrNewObj($filterCriteria));
960
        }
961
962 523
        return $preparedQuery;
963
    }
964
965
    /**
966
     * Prepares the query criteria or new document object.
967
     *
968
     * PHP field names and types will be converted to those used by MongoDB.
969
     */
970 597
    public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : array
971
    {
972 597
        $preparedQuery = [];
973
974 597
        foreach ($query as $key => $value) {
975
            // Recursively prepare logical query clauses
976 555
            if (in_array($key, ['$and', '$or', '$nor'], true) && is_array($value)) {
977 20
                foreach ($value as $k2 => $v2) {
978 20
                    $preparedQuery[$key][$k2] = $this->prepareQueryOrNewObj($v2, $isNewObj);
979
                }
980 20
                continue;
981
            }
982
983 555
            if (isset($key[0]) && $key[0] === '$' && is_array($value)) {
984 74
                $preparedQuery[$key] = $this->prepareQueryOrNewObj($value, $isNewObj);
985 74
                continue;
986
            }
987
988 555
            $preparedQueryElements = $this->prepareQueryElement((string) $key, $value, null, true, $isNewObj);
989 555
            foreach ($preparedQueryElements as [$preparedKey, $preparedValue]) {
990 555
                $preparedQuery[$preparedKey] = is_array($preparedValue)
0 ignored issues
show
Bug introduced by
The variable $preparedKey does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $preparedValue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
991 153
                    ? array_map('\Doctrine\ODM\MongoDB\Types\Type::convertPHPToDatabaseValue', $preparedValue)
992 504
                    : Type::convertPHPToDatabaseValue($preparedValue);
993
            }
994
        }
995
996 597
        return $preparedQuery;
997
    }
998
999
    /**
1000
     * Prepares a query value and converts the PHP value to the database value
1001
     * if it is an identifier.
1002
     *
1003
     * It also handles converting $fieldName to the database name if they are
1004
     * different.
1005
     *
1006
     * @param mixed $value
1007
     */
1008 974
    private function prepareQueryElement(string $fieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false) : array
1009
    {
1010 974
        $class = $class ?? $this->class;
1011
1012
        // @todo Consider inlining calls to ClassMetadata methods
1013
1014
        // Process all non-identifier fields by translating field names
1015 974
        if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) {
1016 278
            $mapping   = $class->fieldMappings[$fieldName];
1017 278
            $fieldName = $mapping['name'];
1018
1019 278
            if (! $prepareValue) {
1020 74
                return [[$fieldName, $value]];
1021
            }
1022
1023
            // Prepare mapped, embedded objects
1024 214
            if (! empty($mapping['embedded']) && is_object($value) &&
1025 214
                ! $this->dm->getMetadataFactory()->isTransient(get_class($value))) {
1026 3
                return [[$fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value)]];
1027
            }
1028
1029 212
            if (! empty($mapping['reference']) && is_object($value) && ! ($value instanceof ObjectId)) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\ObjectId does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1030
                try {
1031 14
                    return $this->prepareReference($fieldName, $value, $mapping, $inNewObj);
1032 1
                } catch (MappingException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\Common\Persiste...apping\MappingException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
1033
                    // do nothing in case passed object is not mapped document
1034
                }
1035
            }
1036
1037
            // No further preparation unless we're dealing with a simple reference
1038 199
            if (empty($mapping['reference']) || $mapping['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID || empty((array) $value)) {
1039 132
                return [[$fieldName, $value]];
1040
            }
1041
1042
            // Additional preparation for one or more simple reference values
1043 94
            $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
1044
1045 94
            if (! is_array($value)) {
1046 90
                return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
1047
            }
1048
1049
            // Objects without operators or with DBRef fields can be converted immediately
1050 6
            if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
1051 3
                return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
1052
            }
1053
1054 6
            return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]];
1055
        }
1056
1057
        // Process identifier fields
1058 866
        if (($class->hasField($fieldName) && $class->isIdentifier($fieldName)) || $fieldName === '_id') {
1059 357
            $fieldName = '_id';
1060
1061 357
            if (! $prepareValue) {
1062 42
                return [[$fieldName, $value]];
1063
            }
1064
1065 318
            if (! is_array($value)) {
1066 292
                return [[$fieldName, $class->getDatabaseIdentifierValue($value)]];
1067
            }
1068
1069
            // Objects without operators or with DBRef fields can be converted immediately
1070 60
            if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
1071 6
                return [[$fieldName, $class->getDatabaseIdentifierValue($value)]];
1072
            }
1073
1074 55
            return [[$fieldName, $this->prepareQueryExpression($value, $class)]];
1075
        }
1076
1077
        // No processing for unmapped, non-identifier, non-dotted field names
1078 610
        if (strpos($fieldName, '.') === false) {
1079 462
            return [[$fieldName, $value]];
1080
        }
1081
1082
        /* Process "fieldName.objectProperty" queries (on arrays or objects).
1083
         *
1084
         * We can limit parsing here, since at most three segments are
1085
         * significant: "fieldName.objectProperty" with an optional index or key
1086
         * for collections stored as either BSON arrays or objects.
1087
         */
1088 160
        $e = explode('.', $fieldName, 4);
1089
1090
        // No further processing for unmapped fields
1091 160
        if (! isset($class->fieldMappings[$e[0]])) {
1092 6
            return [[$fieldName, $value]];
1093
        }
1094
1095 155
        $mapping = $class->fieldMappings[$e[0]];
1096 155
        $e[0]    = $mapping['name'];
1097
1098
        // Hash and raw fields will not be prepared beyond the field name
1099 155
        if ($mapping['type'] === Type::HASH || $mapping['type'] === Type::RAW) {
1100 1
            $fieldName = implode('.', $e);
1101
1102 1
            return [[$fieldName, $value]];
1103
        }
1104
1105 154
        if ($mapping['type'] === 'many' && CollectionHelper::isHash($mapping['strategy'])
1106 154
                && isset($e[2])) {
1107 1
            $objectProperty       = $e[2];
1108 1
            $objectPropertyPrefix = $e[1] . '.';
1109 1
            $nextObjectProperty   = implode('.', array_slice($e, 3));
1110 153
        } elseif ($e[1] !== '$') {
1111 152
            $fieldName            = $e[0] . '.' . $e[1];
1112 152
            $objectProperty       = $e[1];
1113 152
            $objectPropertyPrefix = '';
1114 152
            $nextObjectProperty   = implode('.', array_slice($e, 2));
1115 1
        } elseif (isset($e[2])) {
1116 1
            $fieldName            = $e[0] . '.' . $e[1] . '.' . $e[2];
1117 1
            $objectProperty       = $e[2];
1118 1
            $objectPropertyPrefix = $e[1] . '.';
1119 1
            $nextObjectProperty   = implode('.', array_slice($e, 3));
1120
        } else {
1121 1
            $fieldName = $e[0] . '.' . $e[1];
1122
1123 1
            return [[$fieldName, $value]];
1124
        }
1125
1126
        // No further processing for fields without a targetDocument mapping
1127 154
        if (! isset($mapping['targetDocument'])) {
1128 5
            if ($nextObjectProperty) {
1129
                $fieldName .= '.' . $nextObjectProperty;
1130
            }
1131
1132 5
            return [[$fieldName, $value]];
1133
        }
1134
1135 149
        $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
1136
1137
        // No further processing for unmapped targetDocument fields
1138 149
        if (! $targetClass->hasField($objectProperty)) {
1139 26
            if ($nextObjectProperty) {
1140
                $fieldName .= '.' . $nextObjectProperty;
1141
            }
1142
1143 26
            return [[$fieldName, $value]];
1144
        }
1145
1146 128
        $targetMapping      = $targetClass->getFieldMapping($objectProperty);
1147 128
        $objectPropertyIsId = $targetClass->isIdentifier($objectProperty);
1148
1149
        // Prepare DBRef identifiers or the mapped field's property path
1150 128
        $fieldName = $objectPropertyIsId && ! empty($mapping['reference']) && $mapping['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID
1151 108
            ? ClassMetadata::getReferenceFieldName($mapping['storeAs'], $e[0])
1152 128
            : $e[0] . '.' . $objectPropertyPrefix . $targetMapping['name'];
1153
1154
        // Process targetDocument identifier fields
1155 128
        if ($objectPropertyIsId) {
1156 109
            if (! $prepareValue) {
1157 7
                return [[$fieldName, $value]];
1158
            }
1159
1160 102
            if (! is_array($value)) {
1161 88
                return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
1162
            }
1163
1164
            // Objects without operators or with DBRef fields can be converted immediately
1165 16
            if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
1166 6
                return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
1167
            }
1168
1169 16
            return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]];
1170
        }
1171
1172
        /* The property path may include a third field segment, excluding the
1173
         * collection item pointer. If present, this next object property must
1174
         * be processed recursively.
1175
         */
1176 19
        if ($nextObjectProperty) {
1177
            // Respect the targetDocument's class metadata when recursing
1178 16
            $nextTargetClass = isset($targetMapping['targetDocument'])
1179 10
                ? $this->dm->getClassMetadata($targetMapping['targetDocument'])
1180 16
                : null;
1181
1182 16
            if (empty($targetMapping['reference'])) {
1183 14
                $fieldNames = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue);
1184
            } else {
1185
                // No recursive processing for references as most probably somebody is querying DBRef or alike
1186 4
                if ($nextObjectProperty[0] !== '$' && in_array($targetMapping['storeAs'], [ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB, ClassMetadata::REFERENCE_STORE_AS_DB_REF])) {
1187 1
                    $nextObjectProperty = '$' . $nextObjectProperty;
1188
                }
1189 4
                $fieldNames = [[$nextObjectProperty, $value]];
1190
            }
1191
1192
            return array_map(static function ($preparedTuple) use ($fieldName) {
1193 16
                [$key, $value] = $preparedTuple;
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1194
1195 16
                return [$fieldName . '.' . $key, $value];
1196 16
            }, $fieldNames);
1197
        }
1198
1199 5
        return [[$fieldName, $value]];
1200
    }
1201
1202 77
    private function prepareQueryExpression(array $expression, ClassMetadata $class) : array
1203
    {
1204 77
        foreach ($expression as $k => $v) {
1205
            // Ignore query operators whose arguments need no type conversion
1206 77
            if (in_array($k, ['$exists', '$type', '$mod', '$size'])) {
1207 16
                continue;
1208
            }
1209
1210
            // Process query operators whose argument arrays need type conversion
1211 77
            if (in_array($k, ['$in', '$nin', '$all']) && is_array($v)) {
1212 75
                foreach ($v as $k2 => $v2) {
1213 75
                    $expression[$k][$k2] = $class->getDatabaseIdentifierValue($v2);
1214
                }
1215 75
                continue;
1216
            }
1217
1218
            // Recursively process expressions within a $not operator
1219 18
            if ($k === '$not' && is_array($v)) {
1220 15
                $expression[$k] = $this->prepareQueryExpression($v, $class);
1221 15
                continue;
1222
            }
1223
1224 18
            $expression[$k] = $class->getDatabaseIdentifierValue($v);
1225
        }
1226
1227 77
        return $expression;
1228
    }
1229
1230
    /**
1231
     * Checks whether the value has DBRef fields.
1232
     *
1233
     * This method doesn't check if the the value is a complete DBRef object,
1234
     * although it should return true for a DBRef. Rather, we're checking that
1235
     * the value has one or more fields for a DBref. In practice, this could be
1236
     * $elemMatch criteria for matching a DBRef.
1237
     *
1238
     * @param mixed $value
1239
     */
1240 78
    private function hasDBRefFields($value) : bool
1241
    {
1242 78
        if (! is_array($value) && ! is_object($value)) {
1243
            return false;
1244
        }
1245
1246 78
        if (is_object($value)) {
1247
            $value = get_object_vars($value);
1248
        }
1249
1250 78
        foreach ($value as $key => $_) {
1251 78
            if ($key === '$ref' || $key === '$id' || $key === '$db') {
1252 4
                return true;
1253
            }
1254
        }
1255
1256 77
        return false;
1257
    }
1258
1259
    /**
1260
     * Checks whether the value has query operators.
1261
     *
1262
     * @param mixed $value
1263
     */
1264 82
    private function hasQueryOperators($value) : bool
1265
    {
1266 82
        if (! is_array($value) && ! is_object($value)) {
1267
            return false;
1268
        }
1269
1270 82
        if (is_object($value)) {
1271
            $value = get_object_vars($value);
1272
        }
1273
1274 82
        foreach ($value as $key => $_) {
1275 82
            if (isset($key[0]) && $key[0] === '$') {
1276 78
                return true;
1277
            }
1278
        }
1279
1280 11
        return false;
1281
    }
1282
1283
    /**
1284
     * Gets the array of discriminator values for the given ClassMetadata
1285
     */
1286 27
    private function getClassDiscriminatorValues(ClassMetadata $metadata) : array
1287
    {
1288 27
        $discriminatorValues = [$metadata->discriminatorValue];
1289 27
        foreach ($metadata->subClasses as $className) {
1290 8
            $key = array_search($className, $metadata->discriminatorMap);
1291 8
            if (! $key) {
1292
                continue;
1293
            }
1294
1295 8
            $discriminatorValues[] = $key;
1296
        }
1297
1298
        // If a defaultDiscriminatorValue is set and it is among the discriminators being queries, add NULL to the list
1299 27
        if ($metadata->defaultDiscriminatorValue && in_array($metadata->defaultDiscriminatorValue, $discriminatorValues)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $metadata->defaultDiscriminatorValue of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1300 2
            $discriminatorValues[] = null;
1301
        }
1302
1303 27
        return $discriminatorValues;
1304
    }
1305
1306 593
    private function handleCollections(object $document, array $options) : void
1307
    {
1308
        // Collection deletions (deletions of complete collections)
1309 593
        $collections = [];
1310 593
        foreach ($this->uow->getScheduledCollections($document) as $coll) {
1311 113
            if (! $this->uow->isCollectionScheduledForDeletion($coll)) {
1312 102
                continue;
1313
            }
1314
1315 33
            $collections[] = $coll;
1316
        }
1317 593
        if (! empty($collections)) {
1318 33
            $this->cp->delete($document, $collections, $options);
1319
        }
1320
        // Collection updates (deleteRows, updateRows, insertRows)
1321 593
        $collections = [];
1322 593
        foreach ($this->uow->getScheduledCollections($document) as $coll) {
1323 113
            if (! $this->uow->isCollectionScheduledForUpdate($coll)) {
1324 29
                continue;
1325
            }
1326
1327 105
            $collections[] = $coll;
1328
        }
1329 593
        if (! empty($collections)) {
1330 105
            $this->cp->update($document, $collections, $options);
1331
        }
1332
        // Take new snapshots from visited collections
1333 593
        foreach ($this->uow->getVisitedCollections($document) as $coll) {
1334 252
            $coll->takeSnapshot();
1335
        }
1336 593
    }
1337
1338
    /**
1339
     * If the document is new, ignore shard key field value, otherwise throw an
1340
     * exception. Also, shard key field should be present in actual document
1341
     * data.
1342
     *
1343
     * @throws MongoDBException
1344
     */
1345 10
    private function guardMissingShardKey(object $document, string $shardKeyField, array $actualDocumentData) : void
1346
    {
1347 10
        $dcs      = $this->uow->getDocumentChangeSet($document);
1348 10
        $isUpdate = $this->uow->isScheduledForUpdate($document);
1349
1350 10
        $fieldMapping = $this->class->getFieldMappingByDbFieldName($shardKeyField);
1351 10
        $fieldName    = $fieldMapping['fieldName'];
1352
1353 10
        if ($isUpdate && isset($dcs[$fieldName]) && $dcs[$fieldName][0] !== $dcs[$fieldName][1]) {
1354 2
            throw MongoDBException::shardKeyFieldCannotBeChanged($shardKeyField, $this->class->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
1355
        }
1356
1357 8
        if (! isset($actualDocumentData[$fieldName])) {
1358
            throw MongoDBException::shardKeyFieldMissing($shardKeyField, $this->class->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
1359
        }
1360 8
    }
1361
1362
    /**
1363
     * Get shard key aware query for single document.
1364
     */
1365 310
    private function getQueryForDocument(object $document) : array
1366
    {
1367 310
        $id = $this->uow->getDocumentIdentifier($document);
1368 310
        $id = $this->class->getDatabaseIdentifierValue($id);
1369
1370 310
        $shardKeyQueryPart = $this->getShardKeyQuery($document);
1371 308
        return array_merge(['_id' => $id], $shardKeyQueryPart);
1372
    }
1373
1374 604
    private function getWriteOptions(array $options = []) : array
1375
    {
1376 604
        $defaultOptions  = $this->dm->getConfiguration()->getDefaultCommitOptions();
1377 604
        $documentOptions = [];
1378 604
        if ($this->class->hasWriteConcern()) {
1379 9
            $documentOptions['w'] = $this->class->getWriteConcern();
1380
        }
1381
1382 604
        return array_merge($defaultOptions, $documentOptions, $options);
1383
    }
1384
1385 15
    private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj) : array
1386
    {
1387 15
        $reference = $this->dm->createReference($value, $mapping);
1388 14
        if ($inNewObj || $mapping['storeAs'] === ClassMetadata::REFERENCE_STORE_AS_ID) {
1389 8
            return [[$fieldName, $reference]];
1390
        }
1391
1392 6
        switch ($mapping['storeAs']) {
1393
            case ClassMetadata::REFERENCE_STORE_AS_REF:
1394
                $keys = ['id' => true];
1395
                break;
1396
1397
            case ClassMetadata::REFERENCE_STORE_AS_DB_REF:
1398
            case ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB:
1399 6
                $keys = ['$ref' => true, '$id' => true, '$db' => true];
1400
1401 6
                if ($mapping['storeAs'] === ClassMetadata::REFERENCE_STORE_AS_DB_REF) {
1402 5
                    unset($keys['$db']);
1403
                }
1404
1405 6
                if (isset($mapping['targetDocument'])) {
1406 4
                    unset($keys['$ref'], $keys['$db']);
1407
                }
1408 6
                break;
1409
1410
            default:
1411
                throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $mapping['storeAs']));
1412
        }
1413
1414 6
        if ($mapping['type'] === 'many') {
1415 2
            return [[$fieldName, ['$elemMatch' => array_intersect_key($reference, $keys)]]];
1416
        }
1417
1418 4
        return array_map(
1419
            static function ($key) use ($reference, $fieldName) {
1420 4
                return [$fieldName . '.' . $key, $reference[$key]];
1421 4
            },
1422 4
            array_keys($keys)
1423
        );
1424
    }
1425
}
1426