Completed
Pull Request — master (#1424)
by Andreas
10:18
created

UnitOfWork::computeOrRecomputeChangeSet()   D

Complexity

Conditions 54
Paths 121

Size

Total Lines 168
Code Lines 86

Duplication

Lines 6
Ratio 3.57 %

Code Coverage

Tests 81
CRAP Score 54.1326

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 168
ccs 81
cts 84
cp 0.9643
rs 4
cc 54
eloc 86
nc 121
nop 3
crap 54.1326

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB;
21
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Doctrine\Common\Collections\Collection;
24
use Doctrine\Common\EventManager;
25
use Doctrine\Common\NotifyPropertyChanged;
26
use Doctrine\Common\PropertyChangedListener;
27
use Doctrine\MongoDB\GridFSFile;
28
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
29
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
30
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
31
use Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder;
32
use Doctrine\ODM\MongoDB\Proxy\Proxy;
33
use Doctrine\ODM\MongoDB\Query\Query;
34
use Doctrine\ODM\MongoDB\Types\Type;
35
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
36
use Doctrine\ODM\MongoDB\Utility\LifecycleEventManager;
37
38
/**
39
 * The UnitOfWork is responsible for tracking changes to objects during an
40
 * "object-level" transaction and for writing out changes to the database
41
 * in the correct order.
42
 *
43
 * @since       1.0
44
 */
45
class UnitOfWork implements PropertyChangedListener
46
{
47
    /**
48
     * A document is in MANAGED state when its persistence is managed by a DocumentManager.
49
     */
50
    const STATE_MANAGED = 1;
51
52
    /**
53
     * A document is new if it has just been instantiated (i.e. using the "new" operator)
54
     * and is not (yet) managed by a DocumentManager.
55
     */
56
    const STATE_NEW = 2;
57
58
    /**
59
     * A detached document is an instance with a persistent identity that is not
60
     * (or no longer) associated with a DocumentManager (and a UnitOfWork).
61
     */
62
    const STATE_DETACHED = 3;
63
64
    /**
65
     * A removed document instance is an instance with a persistent identity,
66
     * associated with a DocumentManager, whose persistent state has been
67
     * deleted (or is scheduled for deletion).
68
     */
69
    const STATE_REMOVED = 4;
70
71
    /**
72
     * The identity map holds references to all managed documents.
73
     *
74
     * Documents are grouped by their class name, and then indexed by the
75
     * serialized string of their database identifier field or, if the class
76
     * has no identifier, the SPL object hash. Serializing the identifier allows
77
     * differentiation of values that may be equal (via type juggling) but not
78
     * identical.
79
     *
80
     * Since all classes in a hierarchy must share the same identifier set,
81
     * we always take the root class name of the hierarchy.
82
     *
83
     * @var array
84
     */
85
    private $identityMap = array();
86
87
    /**
88
     * Map of all identifiers of managed documents.
89
     * Keys are object ids (spl_object_hash).
90
     *
91
     * @var array
92
     */
93
    private $documentIdentifiers = array();
94
95
    /**
96
     * Map of the original document data of managed documents.
97
     * Keys are object ids (spl_object_hash). This is used for calculating changesets
98
     * at commit time.
99
     *
100
     * @var array
101
     * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage.
102
     *           A value will only really be copied if the value in the document is modified
103
     *           by the user.
104
     */
105
    private $originalDocumentData = array();
106
107
    /**
108
     * Map of document changes. Keys are object ids (spl_object_hash).
109
     * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end.
110
     *
111
     * @var array
112
     */
113
    private $documentChangeSets = array();
114
115
    /**
116
     * The (cached) states of any known documents.
117
     * Keys are object ids (spl_object_hash).
118
     *
119
     * @var array
120
     */
121
    private $documentStates = array();
122
123
    /**
124
     * Map of documents that are scheduled for dirty checking at commit time.
125
     *
126
     * Documents are grouped by their class name, and then indexed by their SPL
127
     * object hash. This is only used for documents with a change tracking
128
     * policy of DEFERRED_EXPLICIT.
129
     *
130
     * @var array
131
     * @todo rename: scheduledForSynchronization
132
     */
133
    private $scheduledForDirtyCheck = array();
134
135
    /**
136
     * A list of all pending document insertions.
137
     *
138
     * @var array
139
     */
140
    private $documentInsertions = array();
141
142
    /**
143
     * A list of all pending document updates.
144
     *
145
     * @var array
146
     */
147
    private $documentUpdates = array();
148
149
    /**
150
     * A list of all pending document upserts.
151
     *
152
     * @var array
153
     */
154
    private $documentUpserts = array();
155
156
    /**
157
     * A list of all pending document deletions.
158
     *
159
     * @var array
160
     */
161
    private $documentDeletions = array();
162
163
    /**
164
     * All pending collection deletions.
165
     *
166
     * @var array
167
     */
168
    private $collectionDeletions = array();
169
170
    /**
171
     * All pending collection updates.
172
     *
173
     * @var array
174
     */
175
    private $collectionUpdates = array();
176
177
    /**
178
     * A list of documents related to collections scheduled for update or deletion
179
     *
180
     * @var array
181
     */
182
    private $hasScheduledCollections = array();
183
184
    /**
185
     * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork.
186
     * At the end of the UnitOfWork all these collections will make new snapshots
187
     * of their data.
188
     *
189
     * @var array
190
     */
191
    private $visitedCollections = array();
192
193
    /**
194
     * The DocumentManager that "owns" this UnitOfWork instance.
195
     *
196
     * @var DocumentManager
197
     */
198
    private $dm;
199
200
    /**
201
     * The EventManager used for dispatching events.
202
     *
203
     * @var EventManager
204
     */
205
    private $evm;
206
207
    /**
208
     * Additional documents that are scheduled for removal.
209
     *
210
     * @var array
211
     */
212
    private $orphanRemovals = array();
213
214
    /**
215
     * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents.
216
     *
217
     * @var HydratorFactory
218
     */
219
    private $hydratorFactory;
220
221
    /**
222
     * The document persister instances used to persist document instances.
223
     *
224
     * @var array
225
     */
226
    private $persisters = array();
227
228
    /**
229
     * The collection persister instance used to persist changes to collections.
230
     *
231
     * @var Persisters\CollectionPersister
232
     */
233
    private $collectionPersister;
234
235
    /**
236
     * The persistence builder instance used in DocumentPersisters.
237
     *
238
     * @var PersistenceBuilder
239
     */
240
    private $persistenceBuilder;
241
242
    /**
243
     * Array of parent associations between embedded documents
244
     *
245
     * @todo We might need to clean up this array in clear(), doDetach(), etc.
246
     * @var array
247
     */
248
    private $parentAssociations = array();
249
250
    /**
251
     * @var LifecycleEventManager
252
     */
253
    private $lifecycleEventManager;
254
255
    /**
256
     * Initializes a new UnitOfWork instance, bound to the given DocumentManager.
257
     *
258
     * @param DocumentManager $dm
259
     * @param EventManager $evm
260
     * @param HydratorFactory $hydratorFactory
261
     */
262 1040
    public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory)
263
    {
264 1040
        $this->dm = $dm;
265 1040
        $this->evm = $evm;
266 1040
        $this->hydratorFactory = $hydratorFactory;
267 1040
        $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm);
268 1040
    }
269
270
    /**
271
     * Factory for returning new PersistenceBuilder instances used for preparing data into
272
     * queries for insert persistence.
273
     *
274
     * @return PersistenceBuilder $pb
275
     */
276 726
    public function getPersistenceBuilder()
277
    {
278 726
        if ( ! $this->persistenceBuilder) {
279 726
            $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this);
280
        }
281 726
        return $this->persistenceBuilder;
282
    }
283
284
    /**
285
     * Sets the parent association for a given embedded document.
286
     *
287
     * @param object $document
288
     * @param array $mapping
289
     * @param object $parent
290
     * @param string $propertyPath
291
     */
292 193
    public function setParentAssociation($document, $mapping, $parent, $propertyPath)
293
    {
294 193
        $oid = spl_object_hash($document);
295 193
        $this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath);
296 193
    }
297
298
    /**
299
     * Gets the parent association for a given embedded document.
300
     *
301
     *     <code>
302
     *     list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument);
303
     *     </code>
304
     *
305
     * @param object $document
306
     * @return array $association
307
     */
308 222
    public function getParentAssociation($document)
309
    {
310 222
        $oid = spl_object_hash($document);
311 222
        if ( ! isset($this->parentAssociations[$oid])) {
312 218
            return null;
313
        }
314 174
        return $this->parentAssociations[$oid];
315
    }
316
317
    /**
318
     * Get the document persister instance for the given document name
319
     *
320
     * @param string $documentName
321
     * @return Persisters\DocumentPersister
322
     */
323 724
    public function getDocumentPersister($documentName)
324
    {
325 724
        if ( ! isset($this->persisters[$documentName])) {
326 710
            $class = $this->dm->getClassMetadata($documentName);
327 710
            $pb = $this->getPersistenceBuilder();
328 710
            $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this->evm, $this, $this->hydratorFactory, $class);
329
        }
330 724
        return $this->persisters[$documentName];
331
    }
332
333
    /**
334
     * Get the collection persister instance.
335
     *
336
     * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister
337
     */
338 724
    public function getCollectionPersister()
339
    {
340 724
        if ( ! isset($this->collectionPersister)) {
341 724
            $pb = $this->getPersistenceBuilder();
342 724
            $this->collectionPersister = new Persisters\CollectionPersister($this->dm, $pb, $this);
343
        }
344 724
        return $this->collectionPersister;
345
    }
346
347
    /**
348
     * Set the document persister instance to use for the given document name
349
     *
350
     * @param string $documentName
351
     * @param Persisters\DocumentPersister $persister
352
     */
353 14
    public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister)
354
    {
355 14
        $this->persisters[$documentName] = $persister;
356 14
    }
357
358
    /**
359
     * Commits the UnitOfWork, executing all operations that have been postponed
360
     * up to this point. The state of all managed documents will be synchronized with
361
     * the database.
362
     *
363
     * The operations are executed in the following order:
364
     *
365
     * 1) All document insertions
366
     * 2) All document updates
367
     * 3) All document deletions
368
     *
369
     * @param object $document
370
     * @param array $options Array of options to be used with batchInsert(), update() and remove()
371
     */
372 604
    public function commit($document = null, array $options = array())
373
    {
374
        // Raise preFlush
375 604
        if ($this->evm->hasListeners(Events::preFlush)) {
376
            $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm));
377
        }
378
379
        // Compute changes done since last commit.
380 604
        if ($document === null) {
381 598
            $this->computeChangeSets();
382 13
        } elseif (is_object($document)) {
383 12
            $this->computeSingleDocumentChangeSet($document);
384 1
        } elseif (is_array($document)) {
385 1
            foreach ($document as $object) {
386 1
                $this->computeSingleDocumentChangeSet($object);
387
            }
388
        }
389
390 602
        if ( ! ($this->documentInsertions ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->documentInsertions 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...
391 260
            $this->documentUpserts ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->documentUpserts 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...
392 218
            $this->documentDeletions ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->documentDeletions 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...
393 203
            $this->documentUpdates ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->documentUpdates 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...
394 24
            $this->collectionUpdates ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->collectionUpdates 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...
395 24
            $this->collectionDeletions ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->collectionDeletions 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...
396 602
            $this->orphanRemovals)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->orphanRemovals 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...
397
        ) {
398 24
            return; // Nothing to do.
399
        }
400
401 599
        if ($this->orphanRemovals) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->orphanRemovals 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...
402 47
            foreach ($this->orphanRemovals as $removal) {
403 47
                $this->remove($removal);
404
            }
405
        }
406
407
        // Raise onFlush
408 599
        if ($this->evm->hasListeners(Events::onFlush)) {
409 7
            $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm));
410
        }
411
412 599
        foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) {
413 85
            list($class, $documents) = $classAndDocuments;
414 85
            $this->executeUpserts($class, $documents, $options);
415
        }
416
417 599
        foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) {
418 525
            list($class, $documents) = $classAndDocuments;
419 525
            $this->executeInserts($class, $documents, $options);
420
        }
421
422 598
        foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) {
423 231
            list($class, $documents) = $classAndDocuments;
424 231
            $this->executeUpdates($class, $documents, $options);
425
        }
426
427 598
        foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) {
428 69
            list($class, $documents) = $classAndDocuments;
429 69
            $this->executeDeletions($class, $documents, $options);
430
        }
431
432
        // Raise postFlush
433 598
        if ($this->evm->hasListeners(Events::postFlush)) {
434
            $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm));
435
        }
436
437
        // Clear up
438 598
        $this->documentInsertions =
439 598
        $this->documentUpserts =
440 598
        $this->documentUpdates =
441 598
        $this->documentDeletions =
442 598
        $this->documentChangeSets =
443 598
        $this->collectionUpdates =
444 598
        $this->collectionDeletions =
445 598
        $this->visitedCollections =
446 598
        $this->scheduledForDirtyCheck =
447 598
        $this->orphanRemovals =
448 598
        $this->hasScheduledCollections = array();
449 598
    }
450
451
    /**
452
     * Groups a list of scheduled documents by their class.
453
     *
454
     * @param array $documents Scheduled documents (e.g. $this->documentInsertions)
455
     * @param bool $includeEmbedded
456
     * @return array Tuples of ClassMetadata and a corresponding array of objects
457
     */
458 599
    private function getClassesForCommitAction($documents, $includeEmbedded = false)
459
    {
460 599
        if (empty($documents)) {
461 599
            return array();
462
        }
463 598
        $divided = array();
464 598
        $embeds = array();
465 598
        foreach ($documents as $oid => $d) {
466 598
            $className = get_class($d);
467 598
            if (isset($embeds[$className])) {
468 74
                continue;
469
            }
470 598
            if (isset($divided[$className])) {
471 146
                $divided[$className][1][$oid] = $d;
472 146
                continue;
473
            }
474 598
            $class = $this->dm->getClassMetadata($className);
475 598
            if ($class->isEmbeddedDocument && ! $includeEmbedded) {
476 177
                $embeds[$className] = true;
477 177
                continue;
478
            }
479 598
            if (empty($divided[$class->name])) {
480 598
                $divided[$class->name] = array($class, array($oid => $d));
481
            } else {
482 598
                $divided[$class->name][1][$oid] = $d;
483
            }
484
        }
485 598
        return $divided;
486
    }
487
488
    /**
489
     * Compute changesets of all documents scheduled for insertion.
490
     *
491
     * Embedded documents will not be processed.
492
     */
493 606 View Code Duplication
    private function computeScheduleInsertsChangeSets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
    {
495 606
        foreach ($this->documentInsertions as $document) {
496 535
            $class = $this->dm->getClassMetadata(get_class($document));
497 535
            if ( ! $class->isEmbeddedDocument) {
498 535
                $this->computeChangeSet($class, $document);
499
            }
500
        }
501 605
    }
502
503
    /**
504
     * Compute changesets of all documents scheduled for upsert.
505
     *
506
     * Embedded documents will not be processed.
507
     */
508 605 View Code Duplication
    private function computeScheduleUpsertsChangeSets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
509
    {
510 605
        foreach ($this->documentUpserts as $document) {
511 84
            $class = $this->dm->getClassMetadata(get_class($document));
512 84
            if ( ! $class->isEmbeddedDocument) {
513 84
                $this->computeChangeSet($class, $document);
514
            }
515
        }
516 605
    }
517
518
    /**
519
     * Only flush the given document according to a ruleset that keeps the UoW consistent.
520
     *
521
     * 1. All documents scheduled for insertion and (orphan) removals are processed as well!
522
     * 2. Proxies are skipped.
523
     * 3. Only if document is properly managed.
524
     *
525
     * @param  object $document
526
     * @throws \InvalidArgumentException If the document is not STATE_MANAGED
527
     * @return void
528
     */
529 13
    private function computeSingleDocumentChangeSet($document)
530
    {
531 13
        $state = $this->getDocumentState($document);
532
533 13
        if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) {
534 1
            throw new \InvalidArgumentException('Document has to be managed or scheduled for removal for single computation ' . $this->objToStr($document));
535
        }
536
537 12
        $class = $this->dm->getClassMetadata(get_class($document));
538
539 12
        if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) {
540 9
            $this->persist($document);
541
        }
542
543
        // Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case.
544 12
        $this->computeScheduleInsertsChangeSets();
545 12
        $this->computeScheduleUpsertsChangeSets();
546
547
        // Ignore uninitialized proxy objects
548 12
        if ($document instanceof Proxy && ! $document->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
549
            return;
550
        }
551
552
        // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here.
553 12
        $oid = spl_object_hash($document);
554
555 12 View Code Duplication
        if ( ! isset($this->documentInsertions[$oid])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
556 12
            && ! isset($this->documentUpserts[$oid])
557 12
            && ! isset($this->documentDeletions[$oid])
558 12
            && isset($this->documentStates[$oid])
559
        ) {
560 8
            $this->computeChangeSet($class, $document);
561
        }
562 12
    }
563
564
    /**
565
     * Gets the changeset for a document.
566
     *
567
     * @param object $document
568
     * @return array array('property' => array(0 => mixed|null, 1 => mixed|null))
569
     */
570 599
    public function getDocumentChangeSet($document)
571
    {
572 599
        $oid = spl_object_hash($document);
573 599
        if (isset($this->documentChangeSets[$oid])) {
574 596
            return $this->documentChangeSets[$oid];
575
        }
576 62
        return array();
577
    }
578
579
    /**
580
     * INTERNAL:
581
     * Sets the changeset for a document.
582
     *
583
     * @param object $document
584
     * @param array $changeset
585
     */
586 1
    public function setDocumentChangeSet($document, $changeset)
587
    {
588 1
        $this->documentChangeSets[spl_object_hash($document)] = $changeset;
589 1
    }
590
591
    /**
592
     * Get a documents actual data, flattening all the objects to arrays.
593
     *
594
     * @param object $document
595
     * @return array
596
     */
597 606
    public function getDocumentActualData($document)
598
    {
599 606
        $class = $this->dm->getClassMetadata(get_class($document));
600 606
        $actualData = array();
601 606
        foreach ($class->reflFields as $name => $refProp) {
602 606
            $mapping = $class->fieldMappings[$name];
603
            // skip not saved fields
604 606
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
605 51
                continue;
606
            }
607 606
            $value = $refProp->getValue($document);
608 606
            if (isset($mapping['file']) && ! $value instanceof GridFSFile) {
609 6
                $value = new GridFSFile($value);
610 6
                $class->reflFields[$name]->setValue($document, $value);
611 6
                $actualData[$name] = $value;
612 606
            } elseif ((isset($mapping['association']) && $mapping['type'] === 'many')
613 606
                && $value !== null && ! ($value instanceof PersistentCollectionInterface)) {
614
                // If $actualData[$name] is not a Collection then use an ArrayCollection.
615 388
                if ( ! $value instanceof Collection) {
616 127
                    $value = new ArrayCollection($value);
617
                }
618
619
                // Inject PersistentCollection
620 388
                $coll = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $mapping, $value);
621 388
                $coll->setOwner($document, $mapping);
622 388
                $coll->setDirty( ! $value->isEmpty());
623 388
                $class->reflFields[$name]->setValue($document, $coll);
624 388
                $actualData[$name] = $coll;
625
            } else {
626 606
                $actualData[$name] = $value;
627
            }
628
        }
629 606
        return $actualData;
630
    }
631
632
    /**
633
     * Computes the changes that happened to a single document.
634
     *
635
     * Modifies/populates the following properties:
636
     *
637
     * {@link originalDocumentData}
638
     * If the document is NEW or MANAGED but not yet fully persisted (only has an id)
639
     * then it was not fetched from the database and therefore we have no original
640
     * document data yet. All of the current document data is stored as the original document data.
641
     *
642
     * {@link documentChangeSets}
643
     * The changes detected on all properties of the document are stored there.
644
     * A change is a tuple array where the first entry is the old value and the second
645
     * entry is the new value of the property. Changesets are used by persisters
646
     * to INSERT/UPDATE the persistent document state.
647
     *
648
     * {@link documentUpdates}
649
     * If the document is already fully MANAGED (has been fetched from the database before)
650
     * and any changes to its properties are detected, then a reference to the document is stored
651
     * there to mark it for an update.
652
     *
653
     * @param ClassMetadata $class The class descriptor of the document.
654
     * @param object $document The document for which to compute the changes.
655
     */
656 603
    public function computeChangeSet(ClassMetadata $class, $document)
657
    {
658 603
        if ( ! $class->isInheritanceTypeNone()) {
659 180
            $class = $this->dm->getClassMetadata(get_class($document));
660
        }
661
662
        // Fire PreFlush lifecycle callbacks
663 603 View Code Duplication
        if ( ! empty($class->lifecycleCallbacks[Events::preFlush])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
664 11
            $class->invokeLifecycleCallbacks(Events::preFlush, $document, array(new Event\PreFlushEventArgs($this->dm)));
665
        }
666
667 603
        $this->computeOrRecomputeChangeSet($class, $document);
668 602
    }
669
670
    /**
671
     * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet
672
     *
673
     * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class
674
     * @param object $document
675
     * @param boolean $recompute
676
     */
677 603
    private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false)
678
    {
679 603
        $oid = spl_object_hash($document);
680 603
        $actualData = $this->getDocumentActualData($document);
681 603
        $isNewDocument = ! isset($this->originalDocumentData[$oid]);
682 603
        if ($isNewDocument) {
683
            // Document is either NEW or MANAGED but not yet fully persisted (only has an id).
684
            // These result in an INSERT.
685 603
            $this->originalDocumentData[$oid] = $actualData;
686 603
            $changeSet = array();
687 603
            foreach ($actualData as $propName => $actualValue) {
688
                /* At this PersistentCollection shouldn't be here, probably it
689
                 * was cloned and its ownership must be fixed
690
                 */
691 603
                if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) {
692
                    $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName);
693
                    $actualValue = $actualData[$propName];
694
                }
695
                // ignore inverse side of reference relationship
696 603 View Code Duplication
                if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
697 184
                    continue;
698
                }
699 603
                $changeSet[$propName] = array(null, $actualValue);
700
            }
701 603
            $this->documentChangeSets[$oid] = $changeSet;
702
        } else {
703
            // Document is "fully" MANAGED: it was already fully persisted before
704
            // and we have a copy of the original data
705 291
            $originalData = $this->originalDocumentData[$oid];
706 291
            $isChangeTrackingNotify = $class->isChangeTrackingNotify();
707 291
            if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) {
708 2
                $changeSet = $this->documentChangeSets[$oid];
709
            } else {
710 291
                $changeSet = array();
711
            }
712
713 291
            foreach ($actualData as $propName => $actualValue) {
714
                // skip not saved fields
715 291
                if (isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) {
716
                    continue;
717
                }
718
719 291
                $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
720
721
                // skip if value has not changed
722 291
                if ($orgValue === $actualValue) {
723 290
                    if ($actualValue instanceof PersistentCollectionInterface) {
724 203
                        if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) {
725
                            // consider dirty collections as changed as well
726 203
                            continue;
727
                        }
728 290
                    } elseif ( ! (isset($class->fieldMappings[$propName]['file']) && $actualValue->isDirty())) {
729
                        // but consider dirty GridFSFile instances as changed
730 290
                        continue;
731
                    }
732
                }
733
734
                // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations
735 250
                if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') {
736 11
                    if ($orgValue !== null) {
737 6
                        $this->scheduleOrphanRemoval($orgValue);
738
                    }
739
740 11
                    $changeSet[$propName] = array($orgValue, $actualValue);
741 11
                    continue;
742
                }
743
744
                // if owning side of reference-one relationship
745 244
                if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) {
746 13
                    if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) {
747 1
                        $this->scheduleOrphanRemoval($orgValue);
748
                    }
749
750 13
                    $changeSet[$propName] = array($orgValue, $actualValue);
751 13
                    continue;
752
                }
753
754 237
                if ($isChangeTrackingNotify) {
755 3
                    continue;
756
                }
757
758
                // ignore inverse side of reference relationship
759 235 View Code Duplication
                if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
760 6
                    continue;
761
                }
762
763
                // Persistent collection was exchanged with the "originally"
764
                // created one. This can only mean it was cloned and replaced
765
                // on another document.
766 233
                if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) {
767 6
                    $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName);
768
                }
769
770
                // if embed-many or reference-many relationship
771 233
                if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') {
772 117
                    $changeSet[$propName] = array($orgValue, $actualValue);
773
                    /* If original collection was exchanged with a non-empty value
774
                     * and $set will be issued, there is no need to $unset it first
775
                     */
776 117
                    if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) {
777 28
                        continue;
778
                    }
779 97
                    if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) {
780 17
                        $this->scheduleCollectionDeletion($orgValue);
781
                    }
782 97
                    continue;
783
                }
784
785
                // skip equivalent date values
786 153
                if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') {
787 36
                    $dateType = Type::getType('date');
788 36
                    $dbOrgValue = $dateType->convertToDatabaseValue($orgValue);
789 36
                    $dbActualValue = $dateType->convertToDatabaseValue($actualValue);
790
791 36
                    if ($dbOrgValue instanceof \MongoDate && $dbActualValue instanceof \MongoDate && $dbOrgValue == $dbActualValue) {
792 29
                        continue;
793
                    }
794
                }
795
796
                // regular field
797 137
                $changeSet[$propName] = array($orgValue, $actualValue);
798
            }
799 291
            if ($changeSet) {
800 239
                $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid])
801 21
                    ? $changeSet + $this->documentChangeSets[$oid]
802 234
                    : $changeSet;
803
804 239
                $this->originalDocumentData[$oid] = $actualData;
805 239
                $this->scheduleForUpdate($document);
806
            }
807
        }
808
809
        // Look for changes in associations of the document
810 603
        $associationMappings = array_filter(
811 603
            $class->associationMappings,
812
            function ($assoc) { return empty($assoc['notSaved']); }
813
        );
814
815 603
        foreach ($associationMappings as $mapping) {
816 458
            $value = $class->reflFields[$mapping['fieldName']]->getValue($document);
817
818 458
            if ($value === null) {
819 309
                continue;
820
            }
821
822 445
            $this->computeAssociationChanges($document, $mapping, $value);
823
824 444
            if (isset($mapping['reference'])) {
825 337
                continue;
826
            }
827
828 346
            $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value->unwrap();
829
830 346
            foreach ($values as $obj) {
831 181
                $oid2 = spl_object_hash($obj);
832
833 181
                if (isset($this->documentChangeSets[$oid2])) {
834 179
                    $this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value);
835
836 179
                    if ( ! $isNewDocument) {
837 78
                        $this->scheduleForUpdate($document);
838
                    }
839
840 346
                    break;
841
                }
842
            }
843
        }
844 602
    }
845
846
    /**
847
     * Computes all the changes that have been done to documents and collections
848
     * since the last commit and stores these changes in the _documentChangeSet map
849
     * temporarily for access by the persisters, until the UoW commit is finished.
850
     */
851 601
    public function computeChangeSets()
852
    {
853 601
        $this->computeScheduleInsertsChangeSets();
854 600
        $this->computeScheduleUpsertsChangeSets();
855
856
        // Compute changes for other MANAGED documents. Change tracking policies take effect here.
857 600
        foreach ($this->identityMap as $className => $documents) {
858 600
            $class = $this->dm->getClassMetadata($className);
859 600
            if ($class->isEmbeddedDocument) {
860
                /* we do not want to compute changes to embedded documents up front
861
                 * in case embedded document was replaced and its changeset
862
                 * would corrupt data. Embedded documents' change set will
863
                 * be calculated by reachability from owning document.
864
                 */
865 169
                continue;
866
            }
867
868
            // If change tracking is explicit or happens through notification, then only compute
869
            // changes on document of that type that are explicitly marked for synchronization.
870
            switch (true) {
871 600
                case ($class->isChangeTrackingDeferredImplicit()):
872 599
                    $documentsToProcess = $documents;
873 599
                    break;
874
875 4
                case (isset($this->scheduledForDirtyCheck[$className])):
876 3
                    $documentsToProcess = $this->scheduledForDirtyCheck[$className];
877 3
                    break;
878
879
                default:
880 4
                    $documentsToProcess = array();
881
882
            }
883
884 600
            foreach ($documentsToProcess as $document) {
885
                // Ignore uninitialized proxy objects
886 596
                if ($document instanceof Proxy && ! $document->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
887 10
                    continue;
888
                }
889
                // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here.
890 596
                $oid = spl_object_hash($document);
891 596 View Code Duplication
                if ( ! isset($this->documentInsertions[$oid])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
892 596
                    && ! isset($this->documentUpserts[$oid])
893 596
                    && ! isset($this->documentDeletions[$oid])
894 596
                    && isset($this->documentStates[$oid])
895
                ) {
896 600
                    $this->computeChangeSet($class, $document);
897
                }
898
            }
899
        }
900 600
    }
901
902
    /**
903
     * Computes the changes of an association.
904
     *
905
     * @param object $parentDocument
906
     * @param array $assoc
907
     * @param mixed $value The value of the association.
908
     * @throws \InvalidArgumentException
909
     */
910 445
    private function computeAssociationChanges($parentDocument, array $assoc, $value)
911
    {
912 445
        $isNewParentDocument = isset($this->documentInsertions[spl_object_hash($parentDocument)]);
913 445
        $class = $this->dm->getClassMetadata(get_class($parentDocument));
914 445
        $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument);
915
916 445
        if ($value instanceof Proxy && ! $value->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
917 8
            return;
918
        }
919
920 444
        if ($value instanceof PersistentCollectionInterface && $value->isDirty() && ($assoc['isOwningSide'] || isset($assoc['embedded']))) {
921 243
            if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) {
922 239
                $this->scheduleCollectionUpdate($value);
923
            }
924 243
            $topmostOwner = $this->getOwningDocument($value->getOwner());
925 243
            $this->visitedCollections[spl_object_hash($topmostOwner)][] = $value;
926 243
            if ( ! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) {
927 142
                $value->initialize();
928 142
                foreach ($value->getDeletedDocuments() as $orphan) {
929 23
                    $this->scheduleOrphanRemoval($orphan);
930
                }
931
            }
932
        }
933
934
        // Look through the documents, and in any of their associations,
935
        // for transient (new) documents, recursively. ("Persistence by reachability")
936
        // Unwrap. Uninitialized collections will simply be empty.
937 444
        $unwrappedValue = ($assoc['type'] === ClassMetadata::ONE) ? array($value) : $value->unwrap();
938
939 444
        $count = 0;
940 444
        foreach ($unwrappedValue as $key => $entry) {
941 349
            if ( ! is_object($entry)) {
942 1
                throw new \InvalidArgumentException(
943 1
                        sprintf('Expected object, found "%s" in %s::%s', $entry, get_class($parentDocument), $assoc['name'])
944
                );
945
            }
946
947 348
            $targetClass = $this->dm->getClassMetadata(get_class($entry));
948
949 348
            $state = $this->getDocumentState($entry, self::STATE_NEW);
950
951
            // Handle "set" strategy for multi-level hierarchy
952 348
            $pathKey = ! isset($assoc['strategy']) || CollectionHelper::isList($assoc['strategy']) ? $count : $key;
953 348
            $path = $assoc['type'] === 'many' ? $assoc['name'] . '.' . $pathKey : $assoc['name'];
954
955 348
            $count++;
956
957
            switch ($state) {
958 348
                case self::STATE_NEW:
959 60
                    if ( ! $assoc['isCascadePersist']) {
960
                        throw new \InvalidArgumentException('A new document was found through a relationship that was not'
961
                            . ' configured to cascade persist operations: ' . $this->objToStr($entry) . '.'
962
                            . ' Explicitly persist the new document or configure cascading persist operations'
963
                            . ' on the relationship.');
964
                    }
965
966 60
                    $this->persistNew($targetClass, $entry);
967 60
                    $this->setParentAssociation($entry, $assoc, $parentDocument, $path);
968 60
                    $this->computeChangeSet($targetClass, $entry);
969 60
                    break;
970
971 342
                case self::STATE_MANAGED:
972 342
                    if ($targetClass->isEmbeddedDocument) {
973 171
                        list(, $knownParent, ) = $this->getParentAssociation($entry);
974 171
                        if ($knownParent && $knownParent !== $parentDocument) {
975 6
                            $entry = clone $entry;
976 6
                            if ($assoc['type'] === ClassMetadata::ONE) {
977 3
                                $class->setFieldValue($parentDocument, $assoc['name'], $entry);
978 3
                                $this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['name'], $entry);
979
                            } else {
980
                                // must use unwrapped value to not trigger orphan removal
981 6
                                $unwrappedValue[$key] = $entry;
982
                            }
983 6
                            $this->persistNew($targetClass, $entry);
984
                        }
985 171
                        $this->setParentAssociation($entry, $assoc, $parentDocument, $path);
986 171
                        $this->computeChangeSet($targetClass, $entry);
987
                    }
988 342
                    break;
989
990 1
                case self::STATE_REMOVED:
991
                    // Consume the $value as array (it's either an array or an ArrayAccess)
992
                    // and remove the element from Collection.
993 1
                    if ($assoc['type'] === ClassMetadata::MANY) {
994
                        unset($value[$key]);
995
                    }
996 1
                    break;
997
998
                case self::STATE_DETACHED:
999
                    // Can actually not happen right now as we assume STATE_NEW,
1000
                    // so the exception will be raised from the DBAL layer (constraint violation).
1001
                    throw new \InvalidArgumentException('A detached document was found through a '
1002
                        . 'relationship during cascading a persist operation.');
1003
1004 348
                default:
1005
                    // MANAGED associated documents are already taken into account
1006
                    // during changeset calculation anyway, since they are in the identity map.
1007
1008
            }
1009
        }
1010 443
    }
1011
1012
    /**
1013
     * INTERNAL:
1014
     * Computes the changeset of an individual document, independently of the
1015
     * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit().
1016
     *
1017
     * The passed document must be a managed document. If the document already has a change set
1018
     * because this method is invoked during a commit cycle then the change sets are added.
1019
     * whereby changes detected in this method prevail.
1020
     *
1021
     * @ignore
1022
     * @param ClassMetadata $class The class descriptor of the document.
1023
     * @param object $document The document for which to (re)calculate the change set.
1024
     * @throws \InvalidArgumentException If the passed document is not MANAGED.
1025
     */
1026 20
    public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document)
1027
    {
1028
        // Ignore uninitialized proxy objects
1029 20
        if ($document instanceof Proxy && ! $document->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
1030 1
            return;
1031
        }
1032
1033 19
        $oid = spl_object_hash($document);
1034
1035 19
        if ( ! isset($this->documentStates[$oid]) || $this->documentStates[$oid] != self::STATE_MANAGED) {
1036
            throw new \InvalidArgumentException('Document must be managed.');
1037
        }
1038
1039 19
        if ( ! $class->isInheritanceTypeNone()) {
1040 2
            $class = $this->dm->getClassMetadata(get_class($document));
1041
        }
1042
1043 19
        $this->computeOrRecomputeChangeSet($class, $document, true);
1044 19
    }
1045
1046
    /**
1047
     * @param ClassMetadata $class
1048
     * @param object $document
1049
     * @throws \InvalidArgumentException If there is something wrong with document's identifier.
1050
     */
1051 628
    private function persistNew(ClassMetadata $class, $document)
1052
    {
1053 628
        $this->lifecycleEventManager->prePersist($class, $document);
1054 628
        $oid = spl_object_hash($document);
1055 628
        $upsert = false;
1056 628
        if ($class->identifier) {
1057 628
            $idValue = $class->getIdentifierValue($document);
1058 628
            $upsert = ! $class->isEmbeddedDocument && $idValue !== null;
1059
1060 628
            if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) {
1061 3
                throw new \InvalidArgumentException(sprintf(
1062 3
                    '%s uses NONE identifier generation strategy but no identifier was provided when persisting.',
1063
                    get_class($document)
1064
                ));
1065
            }
1066
1067 627
            if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_AUTO && $idValue !== null && ! \MongoId::isValid($idValue)) {
1068 1
                throw new \InvalidArgumentException(sprintf(
1069 1
                    '%s uses AUTO identifier generation strategy but provided identifier is not valid MongoId.',
1070
                    get_class($document)
1071
                ));
1072
            }
1073
1074 626
            if ($class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) {
1075 548
                $idValue = $class->idGenerator->generate($this->dm, $document);
1076 548
                $idValue = $class->getPHPIdentifierValue($class->getDatabaseIdentifierValue($idValue));
1077 548
                $class->setIdentifierValue($document, $idValue);
1078
            }
1079
1080 626
            $this->documentIdentifiers[$oid] = $idValue;
1081
        } else {
1082
            // this is for embedded documents without identifiers
1083 149
            $this->documentIdentifiers[$oid] = $oid;
1084
        }
1085
1086 626
        $this->documentStates[$oid] = self::STATE_MANAGED;
1087
1088 626
        if ($upsert) {
1089 88
            $this->scheduleForUpsert($class, $document);
1090
        } else {
1091 555
            $this->scheduleForInsert($class, $document);
1092
        }
1093 626
    }
1094
1095
    /**
1096
     * Executes all document insertions for documents of the specified type.
1097
     *
1098
     * @param ClassMetadata $class
1099
     * @param array $documents Array of documents to insert
1100
     * @param array $options Array of options to be used with batchInsert()
1101
     */
1102 525 View Code Duplication
    private function executeInserts(ClassMetadata $class, array $documents, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1103
    {
1104 525
        $persister = $this->getDocumentPersister($class->name);
1105
1106 525
        foreach ($documents as $oid => $document) {
1107 525
            $persister->addInsert($document);
1108 525
            unset($this->documentInsertions[$oid]);
1109
        }
1110
1111 525
        $persister->executeInserts($options);
1112
1113 524
        foreach ($documents as $document) {
1114 524
            $this->lifecycleEventManager->postPersist($class, $document);
1115
        }
1116 524
    }
1117
1118
    /**
1119
     * Executes all document upserts for documents of the specified type.
1120
     *
1121
     * @param ClassMetadata $class
1122
     * @param array $documents Array of documents to upsert
1123
     * @param array $options Array of options to be used with batchInsert()
1124
     */
1125 85 View Code Duplication
    private function executeUpserts(ClassMetadata $class, array $documents, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1126
    {
1127 85
        $persister = $this->getDocumentPersister($class->name);
1128
1129
1130 85
        foreach ($documents as $oid => $document) {
1131 85
            $persister->addUpsert($document);
1132 85
            unset($this->documentUpserts[$oid]);
1133
        }
1134
1135 85
        $persister->executeUpserts($options);
1136
1137 85
        foreach ($documents as $document) {
1138 85
            $this->lifecycleEventManager->postPersist($class, $document);
1139
        }
1140 85
    }
1141
1142
    /**
1143
     * Executes all document updates for documents of the specified type.
1144
     *
1145
     * @param Mapping\ClassMetadata $class
1146
     * @param array $documents Array of documents to update
1147
     * @param array $options Array of options to be used with update()
1148
     */
1149 231
    private function executeUpdates(ClassMetadata $class, array $documents, array $options = array())
1150
    {
1151 231
        $className = $class->name;
1152 231
        $persister = $this->getDocumentPersister($className);
1153
1154 231
        foreach ($documents as $oid => $document) {
1155 231
            $this->lifecycleEventManager->preUpdate($class, $document);
1156
1157 231
            if ( ! empty($this->documentChangeSets[$oid]) || $this->hasScheduledCollections($document)) {
1158 229
                $persister->update($document, $options);
1159
            }
1160
1161 225
            unset($this->documentUpdates[$oid]);
1162
1163 225
            $this->lifecycleEventManager->postUpdate($class, $document);
1164
        }
1165 224
    }
1166
1167
    /**
1168
     * Executes all document deletions for documents of the specified type.
1169
     *
1170
     * @param ClassMetadata $class
1171
     * @param array $documents Array of documents to delete
1172
     * @param array $options Array of options to be used with remove()
1173
     */
1174 69
    private function executeDeletions(ClassMetadata $class, array $documents, array $options = array())
1175
    {
1176 69
        $persister = $this->getDocumentPersister($class->name);
1177
1178 69
        foreach ($documents as $oid => $document) {
1179 69
            if ( ! $class->isEmbeddedDocument) {
1180 33
                $persister->delete($document, $options);
1181
            }
1182
            unset(
1183 67
                $this->documentDeletions[$oid],
1184 67
                $this->documentIdentifiers[$oid],
1185 67
                $this->originalDocumentData[$oid]
1186
            );
1187
1188
            // Clear snapshot information for any referenced PersistentCollection
1189
            // http://www.doctrine-project.org/jira/browse/MODM-95
1190 67
            foreach ($class->associationMappings as $fieldMapping) {
1191 43
                if (isset($fieldMapping['type']) && $fieldMapping['type'] === ClassMetadata::MANY) {
1192 27
                    $value = $class->reflFields[$fieldMapping['fieldName']]->getValue($document);
1193 27
                    if ($value instanceof PersistentCollectionInterface) {
1194 43
                        $value->clearSnapshot();
1195
                    }
1196
                }
1197
            }
1198
1199
            // Document with this $oid after deletion treated as NEW, even if the $oid
1200
            // is obtained by a new document because the old one went out of scope.
1201 67
            $this->documentStates[$oid] = self::STATE_NEW;
1202
1203 67
            $this->lifecycleEventManager->postRemove($class, $document);
1204
        }
1205 67
    }
1206
1207
    /**
1208
     * Schedules a document for insertion into the database.
1209
     * If the document already has an identifier, it will be added to the
1210
     * identity map.
1211
     *
1212
     * @param ClassMetadata $class
1213
     * @param object $document The document to schedule for insertion.
1214
     * @throws \InvalidArgumentException
1215
     */
1216 558
    public function scheduleForInsert(ClassMetadata $class, $document)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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...
1217
    {
1218 558
        $oid = spl_object_hash($document);
1219
1220 558
        if (isset($this->documentUpdates[$oid])) {
1221
            throw new \InvalidArgumentException('Dirty document can not be scheduled for insertion.');
1222
        }
1223 558
        if (isset($this->documentDeletions[$oid])) {
1224
            throw new \InvalidArgumentException('Removed document can not be scheduled for insertion.');
1225
        }
1226 558
        if (isset($this->documentInsertions[$oid])) {
1227
            throw new \InvalidArgumentException('Document can not be scheduled for insertion twice.');
1228
        }
1229
1230 558
        $this->documentInsertions[$oid] = $document;
1231
1232 558
        if (isset($this->documentIdentifiers[$oid])) {
1233 555
            $this->addToIdentityMap($document);
1234
        }
1235 558
    }
1236
1237
    /**
1238
     * Schedules a document for upsert into the database and adds it to the
1239
     * identity map
1240
     *
1241
     * @param ClassMetadata $class
1242
     * @param object $document The document to schedule for upsert.
1243
     * @throws \InvalidArgumentException
1244
     */
1245 91
    public function scheduleForUpsert(ClassMetadata $class, $document)
1246
    {
1247 91
        $oid = spl_object_hash($document);
1248
1249 91
        if ($class->isEmbeddedDocument) {
1250
            throw new \InvalidArgumentException('Embedded document can not be scheduled for upsert.');
1251
        }
1252 91
        if (isset($this->documentUpdates[$oid])) {
1253
            throw new \InvalidArgumentException('Dirty document can not be scheduled for upsert.');
1254
        }
1255 91
        if (isset($this->documentDeletions[$oid])) {
1256
            throw new \InvalidArgumentException('Removed document can not be scheduled for upsert.');
1257
        }
1258 91
        if (isset($this->documentUpserts[$oid])) {
1259
            throw new \InvalidArgumentException('Document can not be scheduled for upsert twice.');
1260
        }
1261
1262 91
        $this->documentUpserts[$oid] = $document;
1263 91
        $this->documentIdentifiers[$oid] = $class->getIdentifierValue($document);
1264 91
        $this->addToIdentityMap($document);
1265 91
    }
1266
1267
    /**
1268
     * Checks whether a document is scheduled for insertion.
1269
     *
1270
     * @param object $document
1271
     * @return boolean
1272
     */
1273 104
    public function isScheduledForInsert($document)
1274
    {
1275 104
        return isset($this->documentInsertions[spl_object_hash($document)]);
1276
    }
1277
1278
    /**
1279
     * Checks whether a document is scheduled for upsert.
1280
     *
1281
     * @param object $document
1282
     * @return boolean
1283
     */
1284 5
    public function isScheduledForUpsert($document)
1285
    {
1286 5
        return isset($this->documentUpserts[spl_object_hash($document)]);
1287
    }
1288
1289
    /**
1290
     * Schedules a document for being updated.
1291
     *
1292
     * @param object $document The document to schedule for being updated.
1293
     * @throws \InvalidArgumentException
1294
     */
1295 240
    public function scheduleForUpdate($document)
1296
    {
1297 240
        $oid = spl_object_hash($document);
1298 240
        if ( ! isset($this->documentIdentifiers[$oid])) {
1299
            throw new \InvalidArgumentException('Document has no identity.');
1300
        }
1301
1302 240
        if (isset($this->documentDeletions[$oid])) {
1303
            throw new \InvalidArgumentException('Document is removed.');
1304
        }
1305
1306 240
        if ( ! isset($this->documentUpdates[$oid])
1307 240
            && ! isset($this->documentInsertions[$oid])
1308 240
            && ! isset($this->documentUpserts[$oid])) {
1309 236
            $this->documentUpdates[$oid] = $document;
1310
        }
1311 240
    }
1312
1313
    /**
1314
     * Checks whether a document is registered as dirty in the unit of work.
1315
     * Note: Is not very useful currently as dirty documents are only registered
1316
     * at commit time.
1317
     *
1318
     * @param object $document
1319
     * @return boolean
1320
     */
1321 22
    public function isScheduledForUpdate($document)
1322
    {
1323 22
        return isset($this->documentUpdates[spl_object_hash($document)]);
1324
    }
1325
1326 1
    public function isScheduledForDirtyCheck($document)
1327
    {
1328 1
        $class = $this->dm->getClassMetadata(get_class($document));
1329 1
        return isset($this->scheduledForDirtyCheck[$class->name][spl_object_hash($document)]);
1330
    }
1331
1332
    /**
1333
     * INTERNAL:
1334
     * Schedules a document for deletion.
1335
     *
1336
     * @param object $document
1337
     */
1338 74
    public function scheduleForDelete($document)
1339
    {
1340 74
        $oid = spl_object_hash($document);
1341
1342 74
        if (isset($this->documentInsertions[$oid])) {
1343 2
            if ($this->isInIdentityMap($document)) {
1344 2
                $this->removeFromIdentityMap($document);
1345
            }
1346 2
            unset($this->documentInsertions[$oid]);
1347 2
            return; // document has not been persisted yet, so nothing more to do.
1348
        }
1349
1350 73
        if ( ! $this->isInIdentityMap($document)) {
1351 2
            return; // ignore
1352
        }
1353
1354 72
        $this->removeFromIdentityMap($document);
1355 72
        $this->documentStates[$oid] = self::STATE_REMOVED;
1356
1357 72
        if (isset($this->documentUpdates[$oid])) {
1358
            unset($this->documentUpdates[$oid]);
1359
        }
1360 72
        if ( ! isset($this->documentDeletions[$oid])) {
1361 72
            $this->documentDeletions[$oid] = $document;
1362
        }
1363 72
    }
1364
1365
    /**
1366
     * Checks whether a document is registered as removed/deleted with the unit
1367
     * of work.
1368
     *
1369
     * @param object $document
1370
     * @return boolean
1371
     */
1372 8
    public function isScheduledForDelete($document)
1373
    {
1374 8
        return isset($this->documentDeletions[spl_object_hash($document)]);
1375
    }
1376
1377
    /**
1378
     * Checks whether a document is scheduled for insertion, update or deletion.
1379
     *
1380
     * @param $document
1381
     * @return boolean
1382
     */
1383 242
    public function isDocumentScheduled($document)
1384
    {
1385 242
        $oid = spl_object_hash($document);
1386 242
        return isset($this->documentInsertions[$oid]) ||
1387 130
            isset($this->documentUpserts[$oid]) ||
1388 120
            isset($this->documentUpdates[$oid]) ||
1389 242
            isset($this->documentDeletions[$oid]);
1390
    }
1391
1392
    /**
1393
     * INTERNAL:
1394
     * Registers a document in the identity map.
1395
     *
1396
     * Note that documents in a hierarchy are registered with the class name of
1397
     * the root document. Identifiers are serialized before being used as array
1398
     * keys to allow differentiation of equal, but not identical, values.
1399
     *
1400
     * @ignore
1401
     * @param object $document  The document to register.
1402
     * @return boolean  TRUE if the registration was successful, FALSE if the identity of
1403
     *                  the document in question is already managed.
1404
     */
1405 657
    public function addToIdentityMap($document)
1406
    {
1407 657
        $class = $this->dm->getClassMetadata(get_class($document));
1408 657
        $id = $this->getIdForIdentityMap($document);
1409
1410 657
        if (isset($this->identityMap[$class->name][$id])) {
1411 55
            return false;
1412
        }
1413
1414 657
        $this->identityMap[$class->name][$id] = $document;
1415
1416 657
        if ($document instanceof NotifyPropertyChanged &&
1417 657
            ( ! $document instanceof Proxy || $document->__isInitialized())) {
1418 4
            $document->addPropertyChangedListener($this);
1419
        }
1420
1421 657
        return true;
1422
    }
1423
1424
    /**
1425
     * Gets the state of a document with regard to the current unit of work.
1426
     *
1427
     * @param object   $document
1428
     * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED).
1429
     *                         This parameter can be set to improve performance of document state detection
1430
     *                         by potentially avoiding a database lookup if the distinction between NEW and DETACHED
1431
     *                         is either known or does not matter for the caller of the method.
1432
     * @return int The document state.
1433
     */
1434 631
    public function getDocumentState($document, $assume = null)
1435
    {
1436 631
        $oid = spl_object_hash($document);
1437
1438 631
        if (isset($this->documentStates[$oid])) {
1439 389
            return $this->documentStates[$oid];
1440
        }
1441
1442 631
        $class = $this->dm->getClassMetadata(get_class($document));
1443
1444 631
        if ($class->isEmbeddedDocument) {
1445 187
            return self::STATE_NEW;
1446
        }
1447
1448 628
        if ($assume !== null) {
1449 625
            return $assume;
1450
        }
1451
1452
        /* State can only be NEW or DETACHED, because MANAGED/REMOVED states are
1453
         * known. Note that you cannot remember the NEW or DETACHED state in
1454
         * _documentStates since the UoW does not hold references to such
1455
         * objects and the object hash can be reused. More generally, because
1456
         * the state may "change" between NEW/DETACHED without the UoW being
1457
         * aware of it.
1458
         */
1459 4
        $id = $class->getIdentifierObject($document);
1460
1461 4
        if ($id === null) {
1462 2
            return self::STATE_NEW;
1463
        }
1464
1465
        // Check for a version field, if available, to avoid a DB lookup.
1466 2
        if ($class->isVersioned) {
1467
            return $class->getFieldValue($document, $class->versionField)
1468
                ? self::STATE_DETACHED
1469
                : self::STATE_NEW;
1470
        }
1471
1472
        // Last try before DB lookup: check the identity map.
1473 2
        if ($this->tryGetById($id, $class)) {
1474 1
            return self::STATE_DETACHED;
1475
        }
1476
1477
        // DB lookup
1478 2
        if ($this->getDocumentPersister($class->name)->exists($document)) {
1479 1
            return self::STATE_DETACHED;
1480
        }
1481
1482 1
        return self::STATE_NEW;
1483
    }
1484
1485
    /**
1486
     * INTERNAL:
1487
     * Removes a document from the identity map. This effectively detaches the
1488
     * document from the persistence management of Doctrine.
1489
     *
1490
     * @ignore
1491
     * @param object $document
1492
     * @throws \InvalidArgumentException
1493
     * @return boolean
1494
     */
1495 83
    public function removeFromIdentityMap($document)
1496
    {
1497 83
        $oid = spl_object_hash($document);
1498
1499
        // Check if id is registered first
1500 83
        if ( ! isset($this->documentIdentifiers[$oid])) {
1501
            return false;
1502
        }
1503
1504 83
        $class = $this->dm->getClassMetadata(get_class($document));
1505 83
        $id = $this->getIdForIdentityMap($document);
1506
1507 83
        if (isset($this->identityMap[$class->name][$id])) {
1508 83
            unset($this->identityMap[$class->name][$id]);
1509 83
            $this->documentStates[$oid] = self::STATE_DETACHED;
1510 83
            return true;
1511
        }
1512
1513
        return false;
1514
    }
1515
1516
    /**
1517
     * INTERNAL:
1518
     * Gets a document in the identity map by its identifier hash.
1519
     *
1520
     * @ignore
1521
     * @param mixed         $id    Document identifier
1522
     * @param ClassMetadata $class Document class
1523
     * @return object
1524
     * @throws InvalidArgumentException if the class does not have an identifier
1525
     */
1526 34
    public function getById($id, ClassMetadata $class)
1527
    {
1528 34
        if ( ! $class->identifier) {
1529
            throw new \InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name));
1530
        }
1531
1532 34
        $serializedId = serialize($class->getDatabaseIdentifierValue($id));
1533
1534 34
        return $this->identityMap[$class->name][$serializedId];
1535
    }
1536
1537
    /**
1538
     * INTERNAL:
1539
     * Tries to get a document by its identifier hash. If no document is found
1540
     * for the given hash, FALSE is returned.
1541
     *
1542
     * @ignore
1543
     * @param mixed         $id    Document identifier
1544
     * @param ClassMetadata $class Document class
1545
     * @return mixed The found document or FALSE.
1546
     * @throws InvalidArgumentException if the class does not have an identifier
1547
     */
1548 305
    public function tryGetById($id, ClassMetadata $class)
1549
    {
1550 305
        if ( ! $class->identifier) {
1551
            throw new \InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name));
1552
        }
1553
1554 305
        $serializedId = serialize($class->getDatabaseIdentifierValue($id));
1555
1556 305
        return isset($this->identityMap[$class->name][$serializedId]) ?
1557 305
            $this->identityMap[$class->name][$serializedId] : false;
1558
    }
1559
1560
    /**
1561
     * Schedules a document for dirty-checking at commit-time.
1562
     *
1563
     * @param object $document The document to schedule for dirty-checking.
1564
     * @todo Rename: scheduleForSynchronization
1565
     */
1566 3
    public function scheduleForDirtyCheck($document)
1567
    {
1568 3
        $class = $this->dm->getClassMetadata(get_class($document));
1569 3
        $this->scheduledForDirtyCheck[$class->name][spl_object_hash($document)] = $document;
1570 3
    }
1571
1572
    /**
1573
     * Checks whether a document is registered in the identity map.
1574
     *
1575
     * @param object $document
1576
     * @return boolean
1577
     */
1578 85
    public function isInIdentityMap($document)
1579
    {
1580 85
        $oid = spl_object_hash($document);
1581
1582 85
        if ( ! isset($this->documentIdentifiers[$oid])) {
1583 6
            return false;
1584
        }
1585
1586 83
        $class = $this->dm->getClassMetadata(get_class($document));
1587 83
        $id = $this->getIdForIdentityMap($document);
1588
1589 83
        return isset($this->identityMap[$class->name][$id]);
1590
    }
1591
1592
    /**
1593
     * @param object $document
1594
     * @return string
1595
     */
1596 657
    private function getIdForIdentityMap($document)
1597
    {
1598 657
        $class = $this->dm->getClassMetadata(get_class($document));
1599
1600 657
        if ( ! $class->identifier) {
1601 152
            $id = spl_object_hash($document);
1602
        } else {
1603 656
            $id = $this->documentIdentifiers[spl_object_hash($document)];
1604 656
            $id = serialize($class->getDatabaseIdentifierValue($id));
1605
        }
1606
1607 657
        return $id;
1608
    }
1609
1610
    /**
1611
     * INTERNAL:
1612
     * Checks whether an identifier exists in the identity map.
1613
     *
1614
     * @ignore
1615
     * @param string $id
1616
     * @param string $rootClassName
1617
     * @return boolean
1618
     */
1619
    public function containsId($id, $rootClassName)
1620
    {
1621
        return isset($this->identityMap[$rootClassName][serialize($id)]);
1622
    }
1623
1624
    /**
1625
     * Persists a document as part of the current unit of work.
1626
     *
1627
     * @param object $document The document to persist.
1628
     * @throws MongoDBException If trying to persist MappedSuperclass.
1629
     * @throws \InvalidArgumentException If there is something wrong with document's identifier.
1630
     */
1631 625
    public function persist($document)
1632
    {
1633 625
        $class = $this->dm->getClassMetadata(get_class($document));
1634 625
        if ($class->isMappedSuperclass) {
1635 1
            throw MongoDBException::cannotPersistMappedSuperclass($class->name);
1636
        }
1637 624
        $visited = array();
1638 624
        $this->doPersist($document, $visited);
1639 620
    }
1640
1641
    /**
1642
     * Saves a document as part of the current unit of work.
1643
     * This method is internally called during save() cascades as it tracks
1644
     * the already visited documents to prevent infinite recursions.
1645
     *
1646
     * NOTE: This method always considers documents that are not yet known to
1647
     * this UnitOfWork as NEW.
1648
     *
1649
     * @param object $document The document to persist.
1650
     * @param array $visited The already visited documents.
1651
     * @throws \InvalidArgumentException
1652
     * @throws MongoDBException
1653
     */
1654 624
    private function doPersist($document, array &$visited)
1655
    {
1656 624
        $oid = spl_object_hash($document);
1657 624
        if (isset($visited[$oid])) {
1658 24
            return; // Prevent infinite recursion
1659
        }
1660
1661 624
        $visited[$oid] = $document; // Mark visited
1662
1663 624
        $class = $this->dm->getClassMetadata(get_class($document));
1664
1665 624
        $documentState = $this->getDocumentState($document, self::STATE_NEW);
1666
        switch ($documentState) {
1667 624
            case self::STATE_MANAGED:
1668
                // Nothing to do, except if policy is "deferred explicit"
1669 51
                if ($class->isChangeTrackingDeferredExplicit()) {
1670
                    $this->scheduleForDirtyCheck($document);
1671
                }
1672 51
                break;
1673 624
            case self::STATE_NEW:
1674 624
                $this->persistNew($class, $document);
1675 622
                break;
1676
1677 2
            case self::STATE_REMOVED:
1678
                // Document becomes managed again
1679 2
                unset($this->documentDeletions[$oid]);
1680
1681 2
                $this->documentStates[$oid] = self::STATE_MANAGED;
1682 2
                break;
1683
1684
            case self::STATE_DETACHED:
1685
                throw new \InvalidArgumentException(
1686
                    'Behavior of persist() for a detached document is not yet defined.');
1687
1688
            default:
1689
                throw MongoDBException::invalidDocumentState($documentState);
1690
        }
1691
1692 622
        $this->cascadePersist($document, $visited);
1693 620
    }
1694
1695
    /**
1696
     * Deletes a document as part of the current unit of work.
1697
     *
1698
     * @param object $document The document to remove.
1699
     */
1700 73
    public function remove($document)
1701
    {
1702 73
        $visited = array();
1703 73
        $this->doRemove($document, $visited);
1704 73
    }
1705
1706
    /**
1707
     * Deletes a document as part of the current unit of work.
1708
     *
1709
     * This method is internally called during delete() cascades as it tracks
1710
     * the already visited documents to prevent infinite recursions.
1711
     *
1712
     * @param object $document The document to delete.
1713
     * @param array $visited The map of the already visited documents.
1714
     * @throws MongoDBException
1715
     */
1716 73
    private function doRemove($document, array &$visited)
1717
    {
1718 73
        $oid = spl_object_hash($document);
1719 73
        if (isset($visited[$oid])) {
1720 1
            return; // Prevent infinite recursion
1721
        }
1722
1723 73
        $visited[$oid] = $document; // mark visited
1724
1725
        /* Cascade first, because scheduleForDelete() removes the entity from
1726
         * the identity map, which can cause problems when a lazy Proxy has to
1727
         * be initialized for the cascade operation.
1728
         */
1729 73
        $this->cascadeRemove($document, $visited);
1730
1731 73
        $class = $this->dm->getClassMetadata(get_class($document));
1732 73
        $documentState = $this->getDocumentState($document);
1733
        switch ($documentState) {
1734 73
            case self::STATE_NEW:
1735 73
            case self::STATE_REMOVED:
1736
                // nothing to do
1737 1
                break;
1738 73
            case self::STATE_MANAGED:
1739 73
                $this->lifecycleEventManager->preRemove($class, $document);
1740 73
                $this->scheduleForDelete($document);
1741 73
                break;
1742
            case self::STATE_DETACHED:
1743
                throw MongoDBException::detachedDocumentCannotBeRemoved();
1744
            default:
1745
                throw MongoDBException::invalidDocumentState($documentState);
1746
        }
1747 73
    }
1748
1749
    /**
1750
     * Merges the state of the given detached document into this UnitOfWork.
1751
     *
1752
     * @param object $document
1753
     * @return object The managed copy of the document.
1754
     */
1755 15
    public function merge($document)
1756
    {
1757 15
        $visited = array();
1758
1759 15
        return $this->doMerge($document, $visited);
1760
    }
1761
1762
    /**
1763
     * Executes a merge operation on a document.
1764
     *
1765
     * @param object      $document
1766
     * @param array       $visited
1767
     * @param object|null $prevManagedCopy
1768
     * @param array|null  $assoc
1769
     *
1770
     * @return object The managed copy of the document.
1771
     *
1772
     * @throws InvalidArgumentException If the entity instance is NEW.
1773
     * @throws LockException If the document uses optimistic locking through a
1774
     *                       version attribute and the version check against the
1775
     *                       managed copy fails.
1776
     */
1777 15
    private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null)
1778
    {
1779 15
        $oid = spl_object_hash($document);
1780
1781 15
        if (isset($visited[$oid])) {
1782 1
            return $visited[$oid]; // Prevent infinite recursion
1783
        }
1784
1785 15
        $visited[$oid] = $document; // mark visited
1786
1787 15
        $class = $this->dm->getClassMetadata(get_class($document));
1788
1789
        /* First we assume DETACHED, although it can still be NEW but we can
1790
         * avoid an extra DB round trip this way. If it is not MANAGED but has
1791
         * an identity, we need to fetch it from the DB anyway in order to
1792
         * merge. MANAGED documents are ignored by the merge operation.
1793
         */
1794 15
        $managedCopy = $document;
1795
1796 15
        if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) {
1797 15
            if ($document instanceof Proxy && ! $document->__isInitialized()) {
1798
                $document->__load();
1799
            }
1800
1801 15
            $id = $class->getIdentifier() ? $class->getIdentifierObject($document) : null;
1802 15
            $managedCopy = null;
1803
1804
            // Try to fetch document from the database
1805 15
            if (! $class->isEmbeddedDocument && $id !== null) {
1806 12
                $managedCopy = $this->dm->find($class->name, $id);
1807
1808
                // Managed copy may be removed in which case we can't merge
1809 12
                if ($managedCopy && $this->getDocumentState($managedCopy) === self::STATE_REMOVED) {
1810
                    throw new \InvalidArgumentException('Removed entity detected during merge. Cannot merge with a removed entity.');
1811
                }
1812
1813 12
                if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
1814
                    $managedCopy->__load();
1815
                }
1816
            }
1817
1818 15
            if ($managedCopy === null) {
1819
                // Create a new managed instance
1820 6
                $managedCopy = $class->newInstance();
1821 6
                if ($id !== null) {
1822 3
                    $class->setIdentifierValue($managedCopy, $id);
1823
                }
1824 6
                $this->persistNew($class, $managedCopy);
1825
            }
1826
1827 15
            if ($class->isVersioned) {
1828
                $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy);
1829
                $documentVersion = $class->reflFields[$class->versionField]->getValue($document);
1830
1831
                // Throw exception if versions don't match
1832
                if ($managedCopyVersion != $documentVersion) {
1833
                    throw LockException::lockFailedVersionMissmatch($document, $documentVersion, $managedCopyVersion);
1834
                }
1835
            }
1836
1837
            // Merge state of $document into existing (managed) document
1838 15
            foreach ($class->reflClass->getProperties() as $prop) {
1839 15
                $name = $prop->name;
1840 15
                $prop->setAccessible(true);
1841 15
                if ( ! isset($class->associationMappings[$name])) {
1842 15
                    if ( ! $class->isIdentifier($name)) {
1843 15
                        $prop->setValue($managedCopy, $prop->getValue($document));
1844
                    }
1845
                } else {
1846 15
                    $assoc2 = $class->associationMappings[$name];
1847
1848 15
                    if ($assoc2['type'] === 'one') {
1849 7
                        $other = $prop->getValue($document);
1850
1851 7
                        if ($other === null) {
1852 2
                            $prop->setValue($managedCopy, null);
1853 6
                        } elseif ($other instanceof Proxy && ! $other->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
1854
                            // Do not merge fields marked lazy that have not been fetched
1855 1
                            continue;
1856 5
                        } elseif ( ! $assoc2['isCascadeMerge']) {
1857
                            if ($this->getDocumentState($other) === self::STATE_DETACHED) {
1858
                                $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other);
1859
                                /* @var $targetClass \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */
1860
                                $targetClass = $this->dm->getClassMetadata($targetDocument);
1861
                                $relatedId = $targetClass->getIdentifierObject($other);
1862
1863
                                if ($targetClass->subClasses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $targetClass->subClasses 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...
1864
                                    $other = $this->dm->find($targetClass->name, $relatedId);
1865
                                } else {
1866
                                    $other = $this
1867
                                        ->dm
1868
                                        ->getProxyFactory()
1869
                                        ->getProxy($assoc2['targetDocument'], array($targetClass->identifier => $relatedId));
1870
                                    $this->registerManaged($other, $relatedId, array());
0 ignored issues
show
Documentation introduced by
$relatedId is of type object<MongoId>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1871
                                }
1872
                            }
1873
1874 6
                            $prop->setValue($managedCopy, $other);
1875
                        }
1876
                    } else {
1877 12
                        $mergeCol = $prop->getValue($document);
1878
1879 12
                        if ($mergeCol instanceof PersistentCollectionInterface && ! $mergeCol->isInitialized() && ! $assoc2['isCascadeMerge']) {
1880
                            /* Do not merge fields marked lazy that have not
1881
                             * been fetched. Keep the lazy persistent collection
1882
                             * of the managed copy.
1883
                             */
1884 3
                            continue;
1885
                        }
1886
1887 12
                        $managedCol = $prop->getValue($managedCopy);
1888
1889 12
                        if ( ! $managedCol) {
1890 3
                            $managedCol = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $assoc2, null);
1891 3
                            $managedCol->setOwner($managedCopy, $assoc2);
1892 3
                            $prop->setValue($managedCopy, $managedCol);
1893 3
                            $this->originalDocumentData[$oid][$name] = $managedCol;
1894
                        }
1895
1896
                        /* Note: do not process association's target documents.
1897
                         * They will be handled during the cascade. Initialize
1898
                         * and, if necessary, clear $managedCol for now.
1899
                         */
1900 12
                        if ($assoc2['isCascadeMerge']) {
1901 12
                            $managedCol->initialize();
1902
1903
                            // If $managedCol differs from the merged collection, clear and set dirty
1904 12
                            if ( ! $managedCol->isEmpty() && $managedCol !== $mergeCol) {
1905 3
                                $managedCol->unwrap()->clear();
1906 3
                                $managedCol->setDirty(true);
1907
1908 3
                                if ($assoc2['isOwningSide'] && $class->isChangeTrackingNotify()) {
1909
                                    $this->scheduleForDirtyCheck($managedCopy);
1910
                                }
1911
                            }
1912
                        }
1913
                    }
1914
                }
1915
1916 15
                if ($class->isChangeTrackingNotify()) {
1917
                    // Just treat all properties as changed, there is no other choice.
1918 15
                    $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
1919
                }
1920
            }
1921
1922 15
            if ($class->isChangeTrackingDeferredExplicit()) {
1923
                $this->scheduleForDirtyCheck($document);
1924
            }
1925
        }
1926
1927 15
        if ($prevManagedCopy !== null) {
1928 4
            $assocField = $assoc['fieldName'];
1929 4
            $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy));
1930
1931 4
            if ($assoc['type'] === 'one') {
1932 2
                $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
1933
            } else {
1934 4
                $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy);
1935
1936 4
                if ($assoc['type'] === 'many' && isset($assoc['mappedBy'])) {
1937 1
                    $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy);
1938
                }
1939
            }
1940
        }
1941
1942
        // Mark the managed copy visited as well
1943 15
        $visited[spl_object_hash($managedCopy)] = true;
1944
1945 15
        $this->cascadeMerge($document, $managedCopy, $visited);
1946
1947 11
        return $managedCopy;
1948
    }
1949
1950
    /**
1951
     * Detaches a document from the persistence management. It's persistence will
1952
     * no longer be managed by Doctrine.
1953
     *
1954
     * @param object $document The document to detach.
1955
     */
1956 9
    public function detach($document)
1957
    {
1958 9
        $visited = array();
1959 9
        $this->doDetach($document, $visited);
1960 9
    }
1961
1962
    /**
1963
     * Executes a detach operation on the given document.
1964
     *
1965
     * @param object $document
1966
     * @param array $visited
1967
     * @internal This method always considers documents with an assigned identifier as DETACHED.
1968
     */
1969 12
    private function doDetach($document, array &$visited)
1970
    {
1971 12
        $oid = spl_object_hash($document);
1972 12
        if (isset($visited[$oid])) {
1973 4
            return; // Prevent infinite recursion
1974
        }
1975
1976 12
        $visited[$oid] = $document; // mark visited
1977
1978 12
        switch ($this->getDocumentState($document, self::STATE_DETACHED)) {
1979 12
            case self::STATE_MANAGED:
1980 12
                $this->removeFromIdentityMap($document);
1981 12
                unset($this->documentInsertions[$oid], $this->documentUpdates[$oid],
1982 12
                    $this->documentDeletions[$oid], $this->documentIdentifiers[$oid],
1983 12
                    $this->documentStates[$oid], $this->originalDocumentData[$oid],
1984 12
                    $this->parentAssociations[$oid], $this->documentUpserts[$oid],
1985 12
                    $this->hasScheduledCollections[$oid]);
1986 12
                break;
1987 4
            case self::STATE_NEW:
1988 4
            case self::STATE_DETACHED:
1989 4
                return;
1990
        }
1991
1992 12
        $this->cascadeDetach($document, $visited);
1993 12
    }
1994
1995
    /**
1996
     * Refreshes the state of the given document from the database, overwriting
1997
     * any local, unpersisted changes.
1998
     *
1999
     * @param object $document The document to refresh.
2000
     * @throws \InvalidArgumentException If the document is not MANAGED.
2001
     */
2002 22
    public function refresh($document)
2003
    {
2004 22
        $visited = array();
2005 22
        $this->doRefresh($document, $visited);
2006 21
    }
2007
2008
    /**
2009
     * Executes a refresh operation on a document.
2010
     *
2011
     * @param object $document The document to refresh.
2012
     * @param array $visited The already visited documents during cascades.
2013
     * @throws \InvalidArgumentException If the document is not MANAGED.
2014
     */
2015 22
    private function doRefresh($document, array &$visited)
2016
    {
2017 22
        $oid = spl_object_hash($document);
2018 22
        if (isset($visited[$oid])) {
2019
            return; // Prevent infinite recursion
2020
        }
2021
2022 22
        $visited[$oid] = $document; // mark visited
2023
2024 22
        $class = $this->dm->getClassMetadata(get_class($document));
2025
2026 22
        if ( ! $class->isEmbeddedDocument) {
2027 22
            if ($this->getDocumentState($document) == self::STATE_MANAGED) {
2028 21
                $id = $class->getDatabaseIdentifierValue($this->documentIdentifiers[$oid]);
2029 21
                $this->getDocumentPersister($class->name)->refresh($id, $document);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ODM\MongoDB\Per...entPersister::refresh() has been deprecated with message: The first argument is deprecated.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
2030
            } else {
2031 1
                throw new \InvalidArgumentException('Document is not MANAGED.');
2032
            }
2033
        }
2034
2035 21
        $this->cascadeRefresh($document, $visited);
2036 21
    }
2037
2038
    /**
2039
     * Cascades a refresh operation to associated documents.
2040
     *
2041
     * @param object $document
2042
     * @param array $visited
2043
     */
2044 21
    private function cascadeRefresh($document, array &$visited)
2045
    {
2046 21
        $class = $this->dm->getClassMetadata(get_class($document));
2047
2048 21
        $associationMappings = array_filter(
2049 21
            $class->associationMappings,
2050
            function ($assoc) { return $assoc['isCascadeRefresh']; }
2051
        );
2052
2053 21
        foreach ($associationMappings as $mapping) {
2054 15
            $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document);
2055 15
            if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) {
2056 15
                if ($relatedDocuments instanceof PersistentCollectionInterface) {
2057
                    // Unwrap so that foreach() does not initialize
2058 15
                    $relatedDocuments = $relatedDocuments->unwrap();
2059
                }
2060 15
                foreach ($relatedDocuments as $relatedDocument) {
2061 15
                    $this->doRefresh($relatedDocument, $visited);
2062
                }
2063 10
            } elseif ($relatedDocuments !== null) {
2064 15
                $this->doRefresh($relatedDocuments, $visited);
2065
            }
2066
        }
2067 21
    }
2068
2069
    /**
2070
     * Cascades a detach operation to associated documents.
2071
     *
2072
     * @param object $document
2073
     * @param array $visited
2074
     */
2075 12 View Code Duplication
    private function cascadeDetach($document, array &$visited)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2076
    {
2077 12
        $class = $this->dm->getClassMetadata(get_class($document));
2078 12
        foreach ($class->fieldMappings as $mapping) {
2079 12
            if ( ! $mapping['isCascadeDetach']) {
2080 12
                continue;
2081
            }
2082 7
            $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document);
2083 7
            if (($relatedDocuments instanceof Collection || is_array($relatedDocuments))) {
2084 7
                if ($relatedDocuments instanceof PersistentCollectionInterface) {
2085
                    // Unwrap so that foreach() does not initialize
2086 6
                    $relatedDocuments = $relatedDocuments->unwrap();
2087
                }
2088 7
                foreach ($relatedDocuments as $relatedDocument) {
2089 7
                    $this->doDetach($relatedDocument, $visited);
2090
                }
2091 7
            } elseif ($relatedDocuments !== null) {
2092 7
                $this->doDetach($relatedDocuments, $visited);
2093
            }
2094
        }
2095 12
    }
2096
    /**
2097
     * Cascades a merge operation to associated documents.
2098
     *
2099
     * @param object $document
2100
     * @param object $managedCopy
2101
     * @param array $visited
2102
     */
2103 15
    private function cascadeMerge($document, $managedCopy, array &$visited)
2104
    {
2105 15
        $class = $this->dm->getClassMetadata(get_class($document));
2106
2107 15
        $associationMappings = array_filter(
2108 15
            $class->associationMappings,
2109
            function ($assoc) { return $assoc['isCascadeMerge']; }
2110
        );
2111
2112 15
        foreach ($associationMappings as $assoc) {
2113 14
            $relatedDocuments = $class->reflFields[$assoc['fieldName']]->getValue($document);
2114
2115 14
            if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) {
2116 10
                if ($relatedDocuments === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) {
2117
                    // Collections are the same, so there is nothing to do
2118 1
                    continue;
2119
                }
2120
2121 10
                foreach ($relatedDocuments as $relatedDocument) {
2122 10
                    $this->doMerge($relatedDocument, $visited, $managedCopy, $assoc);
2123
                }
2124 7
            } elseif ($relatedDocuments !== null) {
2125 12
                $this->doMerge($relatedDocuments, $visited, $managedCopy, $assoc);
2126
            }
2127
        }
2128 11
    }
2129
2130
    /**
2131
     * Cascades the save operation to associated documents.
2132
     *
2133
     * @param object $document
2134
     * @param array $visited
2135
     */
2136 622
    private function cascadePersist($document, array &$visited)
2137
    {
2138 622
        $class = $this->dm->getClassMetadata(get_class($document));
2139
2140 622
        $associationMappings = array_filter(
2141 622
            $class->associationMappings,
2142
            function ($assoc) { return $assoc['isCascadePersist']; }
2143
        );
2144
2145 622
        foreach ($associationMappings as $fieldName => $mapping) {
2146 427
            $relatedDocuments = $class->reflFields[$fieldName]->getValue($document);
2147
2148 427
            if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) {
2149 357
                if ($relatedDocuments instanceof PersistentCollectionInterface) {
2150 17
                    if ($relatedDocuments->getOwner() !== $document) {
2151 2
                        $relatedDocuments = $this->fixPersistentCollectionOwnership($relatedDocuments, $document, $class, $mapping['fieldName']);
2152
                    }
2153
                    // Unwrap so that foreach() does not initialize
2154 17
                    $relatedDocuments = $relatedDocuments->unwrap();
2155
                }
2156
2157 357
                $count = 0;
2158 357
                foreach ($relatedDocuments as $relatedKey => $relatedDocument) {
2159 197
                    if ( ! empty($mapping['embedded'])) {
2160 120
                        list(, $knownParent, ) = $this->getParentAssociation($relatedDocument);
2161 120
                        if ($knownParent && $knownParent !== $document) {
2162 4
                            $relatedDocument = clone $relatedDocument;
2163 4
                            $relatedDocuments[$relatedKey] = $relatedDocument;
2164
                        }
2165 120
                        $pathKey = CollectionHelper::isList($mapping['strategy']) ? $count++ : $relatedKey;
2166 120
                        $this->setParentAssociation($relatedDocument, $mapping, $document, $mapping['fieldName'] . '.' . $pathKey);
2167
                    }
2168 357
                    $this->doPersist($relatedDocument, $visited);
2169
                }
2170 337
            } elseif ($relatedDocuments !== null) {
2171 127
                if ( ! empty($mapping['embedded'])) {
2172 70
                    list(, $knownParent, ) = $this->getParentAssociation($relatedDocuments);
2173 70
                    if ($knownParent && $knownParent !== $document) {
2174 5
                        $relatedDocuments = clone $relatedDocuments;
2175 5
                        $class->setFieldValue($document, $mapping['fieldName'], $relatedDocuments);
2176
                    }
2177 70
                    $this->setParentAssociation($relatedDocuments, $mapping, $document, $mapping['fieldName']);
2178
                }
2179 427
                $this->doPersist($relatedDocuments, $visited);
2180
            }
2181
        }
2182 620
    }
2183
2184
    /**
2185
     * Cascades the delete operation to associated documents.
2186
     *
2187
     * @param object $document
2188
     * @param array $visited
2189
     */
2190 73 View Code Duplication
    private function cascadeRemove($document, array &$visited)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2191
    {
2192 73
        $class = $this->dm->getClassMetadata(get_class($document));
2193 73
        foreach ($class->fieldMappings as $mapping) {
2194 72
            if ( ! $mapping['isCascadeRemove']) {
2195 72
                continue;
2196
            }
2197 35
            if ($document instanceof Proxy && ! $document->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
2198 2
                $document->__load();
2199
            }
2200
2201 35
            $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document);
2202 35
            if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) {
2203
                // If its a PersistentCollection initialization is intended! No unwrap!
2204 25
                foreach ($relatedDocuments as $relatedDocument) {
2205 25
                    $this->doRemove($relatedDocument, $visited);
2206
                }
2207 23
            } elseif ($relatedDocuments !== null) {
2208 35
                $this->doRemove($relatedDocuments, $visited);
2209
            }
2210
        }
2211 73
    }
2212
2213
    /**
2214
     * Acquire a lock on the given document.
2215
     *
2216
     * @param object $document
2217
     * @param int $lockMode
2218
     * @param int $lockVersion
2219
     * @throws LockException
2220
     * @throws \InvalidArgumentException
2221
     */
2222 9
    public function lock($document, $lockMode, $lockVersion = null)
2223
    {
2224 9
        if ($this->getDocumentState($document) != self::STATE_MANAGED) {
2225 1
            throw new \InvalidArgumentException('Document is not MANAGED.');
2226
        }
2227
2228 8
        $documentName = get_class($document);
2229 8
        $class = $this->dm->getClassMetadata($documentName);
2230
2231 8
        if ($lockMode == LockMode::OPTIMISTIC) {
2232 3
            if ( ! $class->isVersioned) {
2233 1
                throw LockException::notVersioned($documentName);
2234
            }
2235
2236 2
            if ($lockVersion != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $lockVersion of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
2237 2
                $documentVersion = $class->reflFields[$class->versionField]->getValue($document);
2238 2
                if ($documentVersion != $lockVersion) {
2239 2
                    throw LockException::lockFailedVersionMissmatch($document, $lockVersion, $documentVersion);
2240
                }
2241
            }
2242 5
        } elseif (in_array($lockMode, array(LockMode::PESSIMISTIC_READ, LockMode::PESSIMISTIC_WRITE))) {
2243 5
            $this->getDocumentPersister($class->name)->lock($document, $lockMode);
2244
        }
2245 6
    }
2246
2247
    /**
2248
     * Releases a lock on the given document.
2249
     *
2250
     * @param object $document
2251
     * @throws \InvalidArgumentException
2252
     */
2253 1
    public function unlock($document)
2254
    {
2255 1
        if ($this->getDocumentState($document) != self::STATE_MANAGED) {
2256
            throw new \InvalidArgumentException("Document is not MANAGED.");
2257
        }
2258 1
        $documentName = get_class($document);
2259 1
        $this->getDocumentPersister($documentName)->unlock($document);
2260 1
    }
2261
2262
    /**
2263
     * Clears the UnitOfWork.
2264
     *
2265
     * @param string|null $documentName if given, only documents of this type will get detached.
2266
     */
2267 402
    public function clear($documentName = null)
2268
    {
2269 402
        if ($documentName === null) {
2270 396
            $this->identityMap =
2271 396
            $this->documentIdentifiers =
2272 396
            $this->originalDocumentData =
2273 396
            $this->documentChangeSets =
2274 396
            $this->documentStates =
2275 396
            $this->scheduledForDirtyCheck =
2276 396
            $this->documentInsertions =
2277 396
            $this->documentUpserts =
2278 396
            $this->documentUpdates =
2279 396
            $this->documentDeletions =
2280 396
            $this->collectionUpdates =
2281 396
            $this->collectionDeletions =
2282 396
            $this->parentAssociations =
2283 396
            $this->orphanRemovals =
2284 396
            $this->hasScheduledCollections = array();
2285
        } else {
2286 6
            $visited = array();
2287 6
            foreach ($this->identityMap as $className => $documents) {
2288 6
                if ($className === $documentName) {
2289 3
                    foreach ($documents as $document) {
2290 6
                        $this->doDetach($document, $visited);
2291
                    }
2292
                }
2293
            }
2294
        }
2295
2296 402 View Code Duplication
        if ($this->evm->hasListeners(Events::onClear)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2297
            $this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->dm, $documentName));
2298
        }
2299 402
    }
2300
2301
    /**
2302
     * INTERNAL:
2303
     * Schedules an embedded document for removal. The remove() operation will be
2304
     * invoked on that document at the beginning of the next commit of this
2305
     * UnitOfWork.
2306
     *
2307
     * @ignore
2308
     * @param object $document
2309
     */
2310 49
    public function scheduleOrphanRemoval($document)
2311
    {
2312 49
        $this->orphanRemovals[spl_object_hash($document)] = $document;
2313 49
    }
2314
2315
    /**
2316
     * INTERNAL:
2317
     * Unschedules an embedded or referenced object for removal.
2318
     *
2319
     * @ignore
2320
     * @param object $document
2321
     */
2322 110
    public function unscheduleOrphanRemoval($document)
2323
    {
2324 110
        $oid = spl_object_hash($document);
2325 110
        if (isset($this->orphanRemovals[$oid])) {
2326 1
            unset($this->orphanRemovals[$oid]);
2327
        }
2328 110
    }
2329
2330
    /**
2331
     * Fixes PersistentCollection state if it wasn't used exactly as we had in mind:
2332
     *  1) sets owner if it was cloned
2333
     *  2) clones collection, sets owner, updates document's property and, if necessary, updates originalData
2334
     *  3) NOP if state is OK
2335
     * Returned collection should be used from now on (only important with 2nd point)
2336
     *
2337
     * @param PersistentCollectionInterface $coll
2338
     * @param object $document
2339
     * @param ClassMetadata $class
2340
     * @param string $propName
2341
     * @return PersistentCollectionInterface
2342
     */
2343 8
    private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName)
2344
    {
2345 8
        $owner = $coll->getOwner();
2346 8
        if ($owner === null) { // cloned
2347 6
            $coll->setOwner($document, $class->fieldMappings[$propName]);
2348 2
        } elseif ($owner !== $document) { // no clone, we have to fix
2349 2
            if ( ! $coll->isInitialized()) {
2350 1
                $coll->initialize(); // we have to do this otherwise the cols share state
2351
            }
2352 2
            $newValue = clone $coll;
2353 2
            $newValue->setOwner($document, $class->fieldMappings[$propName]);
2354 2
            $class->reflFields[$propName]->setValue($document, $newValue);
2355 2
            if ($this->isScheduledForUpdate($document)) {
2356
                // @todo following line should be superfluous once collections are stored in change sets
2357
                $this->setOriginalDocumentProperty(spl_object_hash($document), $propName, $newValue);
2358
            }
2359 2
            return $newValue;
2360
        }
2361 6
        return $coll;
2362
    }
2363
2364
    /**
2365
     * INTERNAL:
2366
     * Schedules a complete collection for removal when this UnitOfWork commits.
2367
     *
2368
     * @param PersistentCollectionInterface $coll
2369
     */
2370 42
    public function scheduleCollectionDeletion(PersistentCollectionInterface $coll)
2371
    {
2372 42
        $oid = spl_object_hash($coll);
2373 42
        unset($this->collectionUpdates[$oid]);
2374 42
        if ( ! isset($this->collectionDeletions[$oid])) {
2375 42
            $this->collectionDeletions[$oid] = $coll;
2376 42
            $this->scheduleCollectionOwner($coll);
2377
        }
2378 42
    }
2379
2380
    /**
2381
     * Checks whether a PersistentCollection is scheduled for deletion.
2382
     *
2383
     * @param PersistentCollectionInterface $coll
2384
     * @return boolean
2385
     */
2386 218
    public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll)
2387
    {
2388 218
        return isset($this->collectionDeletions[spl_object_hash($coll)]);
2389
    }
2390
2391
    /**
2392
     * INTERNAL:
2393
     * Unschedules a collection from being deleted when this UnitOfWork commits.
2394
     *
2395
     * @param PersistentCollectionInterface $coll
2396
     */
2397 218 View Code Duplication
    public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2398
    {
2399 218
        $oid = spl_object_hash($coll);
2400 218
        if (isset($this->collectionDeletions[$oid])) {
2401 12
            $topmostOwner = $this->getOwningDocument($coll->getOwner());
2402 12
            unset($this->collectionDeletions[$oid]);
2403 12
            unset($this->hasScheduledCollections[spl_object_hash($topmostOwner)][$oid]);
2404
        }
2405 218
    }
2406
2407
    /**
2408
     * INTERNAL:
2409
     * Schedules a collection for update when this UnitOfWork commits.
2410
     *
2411
     * @param PersistentCollectionInterface $coll
2412
     */
2413 239
    public function scheduleCollectionUpdate(PersistentCollectionInterface $coll)
2414
    {
2415 239
        $mapping = $coll->getMapping();
2416 239
        if (CollectionHelper::usesSet($mapping['strategy'])) {
2417
            /* There is no need to $unset collection if it will be $set later
2418
             * This is NOP if collection is not scheduled for deletion
2419
             */
2420 41
            $this->unscheduleCollectionDeletion($coll);
2421
        }
2422 239
        $oid = spl_object_hash($coll);
2423 239
        if ( ! isset($this->collectionUpdates[$oid])) {
2424 239
            $this->collectionUpdates[$oid] = $coll;
2425 239
            $this->scheduleCollectionOwner($coll);
2426
        }
2427 239
    }
2428
2429
    /**
2430
     * INTERNAL:
2431
     * Unschedules a collection from being updated when this UnitOfWork commits.
2432
     *
2433
     * @param PersistentCollectionInterface $coll
2434
     */
2435 218 View Code Duplication
    public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2436
    {
2437 218
        $oid = spl_object_hash($coll);
2438 218
        if (isset($this->collectionUpdates[$oid])) {
2439 208
            $topmostOwner = $this->getOwningDocument($coll->getOwner());
2440 208
            unset($this->collectionUpdates[$oid]);
2441 208
            unset($this->hasScheduledCollections[spl_object_hash($topmostOwner)][$oid]);
2442
        }
2443 218
    }
2444
2445
    /**
2446
     * Checks whether a PersistentCollection is scheduled for update.
2447
     *
2448
     * @param PersistentCollectionInterface $coll
2449
     * @return boolean
2450
     */
2451 131
    public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll)
2452
    {
2453 131
        return isset($this->collectionUpdates[spl_object_hash($coll)]);
2454
    }
2455
2456
    /**
2457
     * INTERNAL:
2458
     * Gets PersistentCollections that have been visited during computing change
2459
     * set of $document
2460
     *
2461
     * @param object $document
2462
     * @return PersistentCollectionInterface[]
2463
     */
2464 584
    public function getVisitedCollections($document)
2465
    {
2466 584
        $oid = spl_object_hash($document);
2467 584
        return isset($this->visitedCollections[$oid])
2468 242
                ? $this->visitedCollections[$oid]
2469 584
                : array();
2470
    }
2471
2472
    /**
2473
     * INTERNAL:
2474
     * Gets PersistentCollections that are scheduled to update and related to $document
2475
     *
2476
     * @param object $document
2477
     * @return array
2478
     */
2479 584
    public function getScheduledCollections($document)
2480
    {
2481 584
        $oid = spl_object_hash($document);
2482 584
        return isset($this->hasScheduledCollections[$oid])
2483 240
                ? $this->hasScheduledCollections[$oid]
2484 584
                : array();
2485
    }
2486
2487
    /**
2488
     * Checks whether the document is related to a PersistentCollection
2489
     * scheduled for update or deletion.
2490
     *
2491
     * @param object $document
2492
     * @return boolean
2493
     */
2494 52
    public function hasScheduledCollections($document)
2495
    {
2496 52
        return isset($this->hasScheduledCollections[spl_object_hash($document)]);
2497
    }
2498
2499
    /**
2500
     * Marks the PersistentCollection's top-level owner as having a relation to
2501
     * a collection scheduled for update or deletion.
2502
     *
2503
     * If the owner is not scheduled for any lifecycle action, it will be
2504
     * scheduled for update to ensure that versioning takes place if necessary.
2505
     *
2506
     * If the collection is nested within atomic collection, it is immediately
2507
     * unscheduled and atomic one is scheduled for update instead. This makes
2508
     * calculating update data way easier.
2509
     *
2510
     * @param PersistentCollectionInterface $coll
2511
     */
2512 241
    private function scheduleCollectionOwner(PersistentCollectionInterface $coll)
2513
    {
2514 241
        $document = $this->getOwningDocument($coll->getOwner());
2515 241
        $this->hasScheduledCollections[spl_object_hash($document)][spl_object_hash($coll)] = $coll;
2516
2517 241
        if ($document !== $coll->getOwner()) {
2518 25
            $parent = $coll->getOwner();
2519 25
            while (null !== ($parentAssoc = $this->getParentAssociation($parent))) {
2520 25
                list($mapping, $parent, ) = $parentAssoc;
2521
            }
2522 25
            if (CollectionHelper::isAtomic($mapping['strategy'])) {
2523 8
                $class = $this->dm->getClassMetadata(get_class($document));
2524 8
                $atomicCollection = $class->getFieldValue($document, $mapping['fieldName']);
0 ignored issues
show
Bug introduced by
The variable $mapping does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
2525 8
                $this->scheduleCollectionUpdate($atomicCollection);
2526 8
                $this->unscheduleCollectionDeletion($coll);
2527 8
                $this->unscheduleCollectionUpdate($coll);
2528
            }
2529
        }
2530
2531 241
        if ( ! $this->isDocumentScheduled($document)) {
2532 49
            $this->scheduleForUpdate($document);
2533
        }
2534 241
    }
2535
2536
    /**
2537
     * Get the top-most owning document of a given document
2538
     *
2539
     * If a top-level document is provided, that same document will be returned.
2540
     * For an embedded document, we will walk through parent associations until
2541
     * we find a top-level document.
2542
     *
2543
     * @param object $document
2544
     * @throws \UnexpectedValueException when a top-level document could not be found
2545
     * @return object
2546
     */
2547 243
    public function getOwningDocument($document)
2548
    {
2549 243
        $class = $this->dm->getClassMetadata(get_class($document));
2550 243
        while ($class->isEmbeddedDocument) {
2551 40
            $parentAssociation = $this->getParentAssociation($document);
2552
2553 40
            if ( ! $parentAssociation) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parentAssociation 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...
2554
                throw new \UnexpectedValueException('Could not determine parent association for ' . get_class($document));
2555
            }
2556
2557 40
            list(, $document, ) = $parentAssociation;
2558 40
            $class = $this->dm->getClassMetadata(get_class($document));
2559
        }
2560
2561 243
        return $document;
2562
    }
2563
2564
    /**
2565
     * Gets the class name for an association (embed or reference) with respect
2566
     * to any discriminator value.
2567
     *
2568
     * @param array      $mapping Field mapping for the association
2569
     * @param array|null $data    Data for the embedded document or reference
2570
     * @return string Class name.
2571
     */
2572 220
    public function getClassNameForAssociation(array $mapping, $data)
2573
    {
2574 220
        $discriminatorField = isset($mapping['discriminatorField']) ? $mapping['discriminatorField'] : null;
2575
2576 220
        $discriminatorValue = null;
2577 220
        if (isset($discriminatorField, $data[$discriminatorField])) {
2578 21
            $discriminatorValue = $data[$discriminatorField];
2579 200
        } elseif (isset($mapping['defaultDiscriminatorValue'])) {
2580
            $discriminatorValue = $mapping['defaultDiscriminatorValue'];
2581
        }
2582
2583 220
        if ($discriminatorValue !== null) {
2584 21
            return isset($mapping['discriminatorMap'][$discriminatorValue])
2585 10
                ? $mapping['discriminatorMap'][$discriminatorValue]
2586 21
                : $discriminatorValue;
2587
        }
2588
2589 200
            $class = $this->dm->getClassMetadata($mapping['targetDocument']);
2590
2591 200 View Code Duplication
        if (isset($class->discriminatorField, $data[$class->discriminatorField])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2592 15
            $discriminatorValue = $data[$class->discriminatorField];
2593 185
        } elseif ($class->defaultDiscriminatorValue !== null) {
2594 1
            $discriminatorValue = $class->defaultDiscriminatorValue;
2595
        }
2596
2597 200
        if ($discriminatorValue !== null) {
2598 16
            return isset($class->discriminatorMap[$discriminatorValue])
2599 14
                ? $class->discriminatorMap[$discriminatorValue]
2600 16
                : $discriminatorValue;
2601
        }
2602
2603 184
        return $mapping['targetDocument'];
2604
    }
2605
2606
    /**
2607
     * INTERNAL:
2608
     * Creates a document. Used for reconstitution of documents during hydration.
2609
     *
2610
     * @ignore
2611
     * @param string $className The name of the document class.
2612
     * @param array $data The data for the document.
2613
     * @param array $hints Any hints to account for during reconstitution/lookup of the document.
2614
     * @param object $document The document to be hydrated into in case of creation
2615
     * @return object The document instance.
2616
     * @internal Highly performance-sensitive method.
2617
     */
2618 408
    public function getOrCreateDocument($className, $data, &$hints = array(), $document = null)
2619
    {
2620 408
        $class = $this->dm->getClassMetadata($className);
2621
2622
        // @TODO figure out how to remove this
2623 408
        $discriminatorValue = null;
2624 408 View Code Duplication
        if (isset($class->discriminatorField, $data[$class->discriminatorField])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2625 19
            $discriminatorValue = $data[$class->discriminatorField];
2626
        } elseif (isset($class->defaultDiscriminatorValue)) {
2627 2
            $discriminatorValue = $class->defaultDiscriminatorValue;
2628
        }
2629
2630 408
        if ($discriminatorValue !== null) {
2631 20
            $className = isset($class->discriminatorMap[$discriminatorValue])
2632 18
                ? $class->discriminatorMap[$discriminatorValue]
2633 20
                : $discriminatorValue;
2634
2635 20
            $class = $this->dm->getClassMetadata($className);
2636
2637 20
            unset($data[$class->discriminatorField]);
2638
        }
2639
        
2640 408
        if (! empty($hints[Query::HINT_READ_ONLY])) {
2641 2
            $document = $class->newInstance();
2642 2
            $this->hydratorFactory->hydrate($document, $data, $hints);
2643 2
            return $document;
2644
        }
2645
2646 407
        $id = $class->getDatabaseIdentifierValue($data['_id']);
2647 407
        $serializedId = serialize($id);
2648
2649 407
        if (isset($this->identityMap[$class->name][$serializedId])) {
2650 103
            $document = $this->identityMap[$class->name][$serializedId];
2651 103
            $oid = spl_object_hash($document);
2652 103
            if ($document instanceof Proxy && ! $document->__isInitialized__) {
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
2653 12
                $document->__isInitialized__ = true;
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ODM\MongoDB\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
2654 12
                $overrideLocalValues = true;
2655 12
                if ($document instanceof NotifyPropertyChanged) {
2656 12
                    $document->addPropertyChangedListener($this);
2657
                }
2658
            } else {
2659 99
                $overrideLocalValues = ! empty($hints[Query::HINT_REFRESH]);
2660
            }
2661 103
            if ($overrideLocalValues) {
2662 49
                $data = $this->hydratorFactory->hydrate($document, $data, $hints);
2663 103
                $this->originalDocumentData[$oid] = $data;
2664
            }
2665
        } else {
2666 369
            if ($document === null) {
2667 369
                $document = $class->newInstance();
2668
            }
2669 369
            $this->registerManaged($document, $id, $data);
2670 369
            $oid = spl_object_hash($document);
2671 369
            $this->documentStates[$oid] = self::STATE_MANAGED;
2672 369
            $this->identityMap[$class->name][$serializedId] = $document;
2673 369
            $data = $this->hydratorFactory->hydrate($document, $data, $hints);
2674 369
            $this->originalDocumentData[$oid] = $data;
2675
        }
2676 407
        return $document;
2677
    }
2678
2679
    /**
2680
     * Initializes (loads) an uninitialized persistent collection of a document.
2681
     *
2682
     * @param PersistentCollectionInterface $collection The collection to initialize.
2683
     */
2684 168
    public function loadCollection(PersistentCollectionInterface $collection)
2685
    {
2686 168
        $this->getDocumentPersister(get_class($collection->getOwner()))->loadCollection($collection);
2687 168
        $this->lifecycleEventManager->postCollectionLoad($collection);
2688 168
    }
2689
2690
    /**
2691
     * Gets the identity map of the UnitOfWork.
2692
     *
2693
     * @return array
2694
     */
2695
    public function getIdentityMap()
2696
    {
2697
        return $this->identityMap;
2698
    }
2699
2700
    /**
2701
     * Gets the original data of a document. The original data is the data that was
2702
     * present at the time the document was reconstituted from the database.
2703
     *
2704
     * @param object $document
2705
     * @return array
2706
     */
2707 1
    public function getOriginalDocumentData($document)
2708
    {
2709 1
        $oid = spl_object_hash($document);
2710 1
        if (isset($this->originalDocumentData[$oid])) {
2711 1
            return $this->originalDocumentData[$oid];
2712
        }
2713
        return array();
2714
    }
2715
2716
    /**
2717
     * @ignore
2718
     */
2719 55
    public function setOriginalDocumentData($document, array $data)
2720
    {
2721 55
        $oid = spl_object_hash($document);
2722 55
        $this->originalDocumentData[$oid] = $data;
2723 55
        unset($this->documentChangeSets[$oid]);
2724 55
    }
2725
2726
    /**
2727
     * INTERNAL:
2728
     * Sets a property value of the original data array of a document.
2729
     *
2730
     * @ignore
2731
     * @param string $oid
2732
     * @param string $property
2733
     * @param mixed $value
2734
     */
2735 3
    public function setOriginalDocumentProperty($oid, $property, $value)
2736
    {
2737 3
        $this->originalDocumentData[$oid][$property] = $value;
2738 3
    }
2739
2740
    /**
2741
     * Gets the identifier of a document.
2742
     *
2743
     * @param object $document
2744
     * @return mixed The identifier value
2745
     */
2746 425
    public function getDocumentIdentifier($document)
2747
    {
2748 425
        return isset($this->documentIdentifiers[spl_object_hash($document)]) ?
2749 425
            $this->documentIdentifiers[spl_object_hash($document)] : null;
2750
    }
2751
2752
    /**
2753
     * Checks whether the UnitOfWork has any pending insertions.
2754
     *
2755
     * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise.
2756
     */
2757
    public function hasPendingInsertions()
2758
    {
2759
        return ! empty($this->documentInsertions);
2760
    }
2761
2762
    /**
2763
     * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the
2764
     * number of documents in the identity map.
2765
     *
2766
     * @return integer
2767
     */
2768 2
    public function size()
2769
    {
2770 2
        $count = 0;
2771 2
        foreach ($this->identityMap as $documentSet) {
2772 2
            $count += count($documentSet);
2773
        }
2774 2
        return $count;
2775
    }
2776
2777
    /**
2778
     * INTERNAL:
2779
     * Registers a document as managed.
2780
     *
2781
     * TODO: This method assumes that $id is a valid PHP identifier for the
2782
     * document class. If the class expects its database identifier to be a
2783
     * MongoId, and an incompatible $id is registered (e.g. an integer), the
2784
     * document identifiers map will become inconsistent with the identity map.
2785
     * In the future, we may want to round-trip $id through a PHP and database
2786
     * conversion and throw an exception if it's inconsistent.
2787
     *
2788
     * @param object $document The document.
2789
     * @param array $id The identifier values.
2790
     * @param array $data The original document data.
2791
     */
2792 391
    public function registerManaged($document, $id, array $data)
2793
    {
2794 391
        $oid = spl_object_hash($document);
2795 391
        $class = $this->dm->getClassMetadata(get_class($document));
2796
2797 391
        if ( ! $class->identifier || $id === null) {
2798 106
            $this->documentIdentifiers[$oid] = $oid;
2799
        } else {
2800 385
            $this->documentIdentifiers[$oid] = $class->getPHPIdentifierValue($id);
2801
        }
2802
2803 391
        $this->documentStates[$oid] = self::STATE_MANAGED;
2804 391
        $this->originalDocumentData[$oid] = $data;
2805 391
        $this->addToIdentityMap($document);
2806 391
    }
2807
2808
    /**
2809
     * INTERNAL:
2810
     * Clears the property changeset of the document with the given OID.
2811
     *
2812
     * @param string $oid The document's OID.
2813
     */
2814 1
    public function clearDocumentChangeSet($oid)
2815
    {
2816 1
        $this->documentChangeSets[$oid] = array();
2817 1
    }
2818
2819
    /* PropertyChangedListener implementation */
2820
2821
    /**
2822
     * Notifies this UnitOfWork of a property change in a document.
2823
     *
2824
     * @param object $document The document that owns the property.
2825
     * @param string $propertyName The name of the property that changed.
2826
     * @param mixed $oldValue The old value of the property.
2827
     * @param mixed $newValue The new value of the property.
2828
     */
2829 2
    public function propertyChanged($document, $propertyName, $oldValue, $newValue)
2830
    {
2831 2
        $oid = spl_object_hash($document);
2832 2
        $class = $this->dm->getClassMetadata(get_class($document));
2833
2834 2
        if ( ! isset($class->fieldMappings[$propertyName])) {
2835 1
            return; // ignore non-persistent fields
2836
        }
2837
2838
        // Update changeset and mark document for synchronization
2839 2
        $this->documentChangeSets[$oid][$propertyName] = array($oldValue, $newValue);
2840 2
        if ( ! isset($this->scheduledForDirtyCheck[$class->name][$oid])) {
2841 2
            $this->scheduleForDirtyCheck($document);
2842
        }
2843 2
    }
2844
2845
    /**
2846
     * Gets the currently scheduled document insertions in this UnitOfWork.
2847
     *
2848
     * @return array
2849
     */
2850 5
    public function getScheduledDocumentInsertions()
2851
    {
2852 5
        return $this->documentInsertions;
2853
    }
2854
2855
    /**
2856
     * Gets the currently scheduled document upserts in this UnitOfWork.
2857
     *
2858
     * @return array
2859
     */
2860 3
    public function getScheduledDocumentUpserts()
2861
    {
2862 3
        return $this->documentUpserts;
2863
    }
2864
2865
    /**
2866
     * Gets the currently scheduled document updates in this UnitOfWork.
2867
     *
2868
     * @return array
2869
     */
2870 3
    public function getScheduledDocumentUpdates()
2871
    {
2872 3
        return $this->documentUpdates;
2873
    }
2874
2875
    /**
2876
     * Gets the currently scheduled document deletions in this UnitOfWork.
2877
     *
2878
     * @return array
2879
     */
2880
    public function getScheduledDocumentDeletions()
2881
    {
2882
        return $this->documentDeletions;
2883
    }
2884
2885
    /**
2886
     * Get the currently scheduled complete collection deletions
2887
     *
2888
     * @return array
2889
     */
2890
    public function getScheduledCollectionDeletions()
2891
    {
2892
        return $this->collectionDeletions;
2893
    }
2894
2895
    /**
2896
     * Gets the currently scheduled collection inserts, updates and deletes.
2897
     *
2898
     * @return array
2899
     */
2900
    public function getScheduledCollectionUpdates()
2901
    {
2902
        return $this->collectionUpdates;
2903
    }
2904
2905
    /**
2906
     * Helper method to initialize a lazy loading proxy or persistent collection.
2907
     *
2908
     * @param object
2909
     * @return void
2910
     */
2911
    public function initializeObject($obj)
2912
    {
2913
        if ($obj instanceof Proxy) {
2914
            $obj->__load();
2915
        } elseif ($obj instanceof PersistentCollectionInterface) {
2916
            $obj->initialize();
2917
        }
2918
    }
2919
2920 1
    private function objToStr($obj)
2921
    {
2922 1
        return method_exists($obj, '__toString') ? (string)$obj : get_class($obj) . '@' . spl_object_hash($obj);
2923
    }
2924
}
2925