Failed Conditions
Pull Request — 2.7 (#7940)
by Benjamin
06:29
created

PersistentCollection::getSnapshot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\ORM;
21
22
use Doctrine\Common\Collections\AbstractLazyCollection;
23
use Doctrine\Common\Collections\Collection;
24
use Doctrine\Common\Collections\ArrayCollection;
25
use Doctrine\Common\Collections\Selectable;
26
use Doctrine\Common\Collections\Criteria;
27
use Doctrine\ORM\Mapping\ClassMetadata;
28
use function get_class;
29
30
/**
31
 * A PersistentCollection represents a collection of elements that have persistent state.
32
 *
33
 * Collections of entities represent only the associations (links) to those entities.
34
 * That means, if the collection is part of a many-many mapping and you remove
35
 * entities from the collection, only the links in the relation table are removed (on flush).
36
 * Similarly, if you remove entities from a collection that is part of a one-many
37
 * mapping this will only result in the nulling out of the foreign keys on flush.
38
 *
39
 * @since     2.0
40
 * @author    Konsta Vesterinen <[email protected]>
41
 * @author    Roman Borschel <[email protected]>
42
 * @author    Giorgio Sironi <[email protected]>
43
 * @author    Stefano Rodriguez <[email protected]>
44
 */
45
final class PersistentCollection extends AbstractLazyCollection implements Selectable
46
{
47
    /**
48
     * A snapshot of the collection at the moment it was fetched from the database.
49
     * This is used to create a diff of the collection at commit time.
50
     *
51
     * @var array
52
     */
53
    private $snapshot = [];
54
55
    /**
56
     * The entity that owns this collection.
57
     *
58
     * @var object
59
     */
60
    private $owner;
61
62
    /**
63
     * The association mapping the collection belongs to.
64
     * This is currently either a OneToManyMapping or a ManyToManyMapping.
65
     *
66
     * @var array
67
     */
68
    private $association;
69
70
    /**
71
     * The EntityManager that manages the persistence of the collection.
72
     *
73
     * @var \Doctrine\ORM\EntityManagerInterface
74
     */
75
    private $em;
76
77
    /**
78
     * The name of the field on the target entities that points to the owner
79
     * of the collection. This is only set if the association is bi-directional.
80
     *
81
     * @var string
82
     */
83
    private $backRefFieldName;
84
85
    /**
86
     * The class descriptor of the collection's entity type.
87
     *
88
     * @var ClassMetadata
89
     */
90
    private $typeClass;
91
92
    /**
93
     * Whether the collection is dirty and needs to be synchronized with the database
94
     * when the UnitOfWork that manages its persistent state commits.
95
     *
96
     * @var boolean
97
     */
98
    private $isDirty = false;
99
100
    /**
101
     * Creates a new persistent collection.
102
     *
103
     * @param EntityManagerInterface $em         The EntityManager the collection will be associated with.
104
     * @param ClassMetadata          $class      The class descriptor of the entity type of this collection.
105
     * @param Collection             $collection The collection elements.
106
     */
107 927
    public function __construct(EntityManagerInterface $em, $class, Collection $collection)
108
    {
109 927
        $this->collection  = $collection;
110 927
        $this->em          = $em;
111 927
        $this->typeClass   = $class;
112 927
        $this->initialized = true;
113 927
    }
114
115
    /**
116
     * INTERNAL:
117
     * Sets the collection's owning entity together with the AssociationMapping that
118
     * describes the association between the owner and the elements of the collection.
119
     *
120
     * @param object $entity
121
     * @param array  $assoc
122
     *
123
     * @return void
124
     */
125 921
    public function setOwner($entity, array $assoc)
126
    {
127 921
        $this->owner            = $entity;
128 921
        $this->association      = $assoc;
129 921
        $this->backRefFieldName = $assoc['inversedBy'] ?: $assoc['mappedBy'];
130 921
    }
131
132
    /**
133
     * INTERNAL:
134
     * Gets the collection owner.
135
     *
136
     * @return object
137
     */
138 539
    public function getOwner()
139
    {
140 539
        return $this->owner;
141
    }
142
143
    /**
144
     * @return Mapping\ClassMetadata
145
     */
146 21
    public function getTypeClass()
147
    {
148 21
        return $this->typeClass;
149
    }
150
151
    /**
152
     * INTERNAL:
153
     * Adds an element to a collection during hydration. This will automatically
154
     * complete bidirectional associations in the case of a one-to-many association.
155
     *
156
     * @param mixed $element The element to add.
157
     *
158
     * @return void
159
     */
160 168
    public function hydrateAdd($element)
161
    {
162 168
        $this->collection->add($element);
163
164
        // If _backRefFieldName is set and its a one-to-many association,
165
        // we need to set the back reference.
166 168
        if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) {
167
            // Set back reference to owner
168 110
            $this->typeClass->reflFields[$this->backRefFieldName]->setValue(
169 110
                $element, $this->owner
170
            );
171
172 110
            $this->em->getUnitOfWork()->setOriginalEntityProperty(
173 110
                spl_object_hash($element), $this->backRefFieldName, $this->owner
174
            );
175
        }
176 168
    }
177
178
    /**
179
     * INTERNAL:
180
     * Sets a keyed element in the collection during hydration.
181
     *
182
     * @param mixed $key     The key to set.
183
     * @param mixed $element The element to set.
184
     *
185
     * @return void
186
     */
187 42
    public function hydrateSet($key, $element)
188
    {
189 42
        $this->collection->set($key, $element);
190
191
        // If _backRefFieldName is set, then the association is bidirectional
192
        // and we need to set the back reference.
193 42
        if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) {
194
            // Set back reference to owner
195 24
            $this->typeClass->reflFields[$this->backRefFieldName]->setValue(
196 24
                $element, $this->owner
197
            );
198
        }
199 42
    }
200
201
    /**
202
     * Initializes the collection by loading its contents from the database
203
     * if the collection is not yet initialized.
204
     *
205
     * @return void
206
     */
207 793
    public function initialize()
208
    {
209 793
        if ($this->initialized || ! $this->association) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->association 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...
210 770
            return;
211
        }
212
213 161
        $this->doInitialize();
214
215 161
        $this->initialized = true;
216 161
    }
217
218
    /**
219
     * INTERNAL:
220
     * Tells this collection to take a snapshot of its current state.
221
     *
222
     * @return void
223
     */
224 606
    public function takeSnapshot()
225
    {
226 606
        $this->snapshot = $this->collection->toArray();
227 606
        $this->isDirty  = false;
228 606
    }
229
230
    /**
231
     * INTERNAL:
232
     * Returns the last snapshot of the elements in the collection.
233
     *
234
     * @return array The last snapshot of the elements.
235
     */
236 24
    public function getSnapshot()
237
    {
238 24
        return $this->snapshot;
239
    }
240
241
    /**
242
     * INTERNAL:
243
     * getDeleteDiff
244
     *
245
     * @return array
246
     */
247 337
    public function getDeleteDiff()
248
    {
249 337
        return array_udiff_assoc(
250 337
            $this->snapshot,
251 337
            $this->collection->toArray(),
252
            function($a, $b) { return $a === $b ? 0 : 1; }
253
        );
254
    }
255
256
    /**
257
     * INTERNAL:
258
     * getInsertDiff
259
     *
260
     * @return array
261
     */
262 337
    public function getInsertDiff()
263
    {
264 337
        return array_udiff_assoc(
265 337
            $this->collection->toArray(),
266 337
            $this->snapshot,
267
            function($a, $b) { return $a === $b ? 0 : 1; }
268
        );
269
    }
270
271
    /**
272
     * INTERNAL: Gets the association mapping of the collection.
273
     *
274
     * @return array
275
     */
276 571
    public function getMapping()
277
    {
278 571
        return $this->association;
279
    }
280
281
    /**
282
     * Marks this collection as changed/dirty.
283
     *
284
     * @return void
285
     */
286 162
    private function changed()
287
    {
288 162
        if ($this->isDirty) {
289 78
            return;
290
        }
291
292 162
        $this->isDirty = true;
293
294 162
        if ($this->association !== null &&
295 162
            $this->association['isOwningSide'] &&
296 162
            $this->association['type'] === ClassMetadata::MANY_TO_MANY &&
297 162
            $this->owner &&
298 162
            $this->em->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify()) {
299 1
            $this->em->getUnitOfWork()->scheduleForDirtyCheck($this->owner);
300
        }
301 162
    }
302
303
    /**
304
     * Gets a boolean flag indicating whether this collection is dirty which means
305
     * its state needs to be synchronized with the database.
306
     *
307
     * @return boolean TRUE if the collection is dirty, FALSE otherwise.
308
     */
309 836
    public function isDirty()
310
    {
311 836
        return $this->isDirty;
312
    }
313
314
    /**
315
     * Sets a boolean flag, indicating whether this collection is dirty.
316
     *
317
     * @param boolean $dirty Whether the collection should be marked dirty or not.
318
     *
319
     * @return void
320
     */
321 824
    public function setDirty($dirty)
322
    {
323 824
        $this->isDirty = $dirty;
324 824
    }
325
326
    /**
327
     * Sets the initialized flag of the collection, forcing it into that state.
328
     *
329
     * @param boolean $bool
330
     *
331
     * @return void
332
     */
333 554
    public function setInitialized($bool)
334
    {
335 554
        $this->initialized = $bool;
336 554
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341 16
    public function remove($key)
342
    {
343
        // TODO: If the keys are persistent as well (not yet implemented)
344
        //       and the collection is not initialized and orphanRemoval is
345
        //       not used we can issue a straight SQL delete/update on the
346
        //       association (table). Without initializing the collection.
347 16
        $removed = parent::remove($key);
348
349 16
        if ( ! $removed) {
350
            return $removed;
351
        }
352
353 16
        $this->changed();
354
355 16
        if ($this->association !== null &&
356 16
            $this->association['type'] & ClassMetadata::TO_MANY &&
357 16
            $this->owner &&
358 16
            $this->association['orphanRemoval']) {
359 4
            $this->em->getUnitOfWork()->scheduleOrphanRemoval($removed);
360
        }
361
362 16
        return $removed;
363
    }
364
365
    /**
366
     * {@inheritdoc}
367
     */
368 27
    public function removeElement($element)
369
    {
370 27
        if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
371 14
            if ($this->collection->contains($element)) {
372
                return $this->collection->removeElement($element);
373
            }
374
375 14
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
376
377 14
            if ($persister->removeElement($this, $element) === true) {
378
                // only  owning side or orphan removal triggers successful
379
                // delete so that we can keep this collection unitialized.
380 4
                return true;
381
            }
382
        }
383
384 25
        $removed = parent::removeElement($element);
385
386 25
        if ( ! $removed) {
387 6
            return $removed;
388
        }
389
390 19
        $this->changed();
391
392 19
        if ($this->association !== null &&
393 19
            $this->association['type'] & ClassMetadata::TO_MANY &&
394 19
            $this->owner &&
395 19
            $this->association['orphanRemoval']) {
396 4
            $this->em->getUnitOfWork()->scheduleOrphanRemoval($element);
397
        }
398
399 19
        return $removed;
400
    }
401
402
    /**
403
     * {@inheritdoc}
404
     */
405 28
    public function containsKey($key)
406
    {
407 28
        if (! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY
408 28
            && isset($this->association['indexBy'])) {
409 11
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
410
411 11
            return $this->collection->containsKey($key) || $persister->containsKey($this, $key);
412
        }
413
414 17
        return parent::containsKey($key);
415
    }
416
417
    /**
418
     * {@inheritdoc}
419
     */
420 33
    public function contains($element)
421
    {
422 33
        if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
423 16
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
424
425 16
            return $this->collection->contains($element) || $persister->contains($this, $element);
426
        }
427
428 17
        return parent::contains($element);
429
    }
430
431
    /**
432
     * {@inheritdoc}
433
     */
434 87
    public function get($key)
435
    {
436 87
        if ( ! $this->initialized
437 87
            && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY
438 87
            && isset($this->association['indexBy'])
439
        ) {
440 5
            if (!$this->typeClass->isIdentifierComposite && $this->typeClass->isIdentifier($this->association['indexBy'])) {
441 1
                return $this->em->find($this->typeClass->name, $key);
442
            }
443
444 4
            return $this->em->getUnitOfWork()->getCollectionPersister($this->association)->get($this, $key);
445
        }
446
447 82
        return parent::get($key);
448
    }
449
450
    /**
451
     * {@inheritdoc}
452
     */
453 758
    public function count()
454
    {
455 758
        if (! $this->initialized && $this->association !== null && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
456 32
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
457
458 32
            return $persister->count($this) + ($this->isDirty ? $this->collection->count() : 0);
459
        }
460
461 752
        return parent::count();
462
    }
463
464
    /**
465
     * {@inheritdoc}
466
     */
467 4
    public function set($key, $value)
468
    {
469 4
        parent::set($key, $value);
470
471 4
        $this->changed();
472
473 4
        if (is_object($value) && $this->em) {
474 4
            $this->em->getUnitOfWork()->cancelOrphanRemoval($value);
475
        }
476 4
    }
477
478
    /**
479
     * {@inheritdoc}
480
     */
481 117
    public function add($value)
482
    {
483 117
        $this->collection->add($value);
484
485 117
        $this->changed();
486
487 117
        if (is_object($value) && $this->em) {
488 116
            $this->em->getUnitOfWork()->cancelOrphanRemoval($value);
489
        }
490
491 117
        return true;
492
    }
493
494
    /* ArrayAccess implementation */
495
496
    /**
497
     * {@inheritdoc}
498
     */
499 17
    public function offsetExists($offset)
500
    {
501 17
        return $this->containsKey($offset);
502
    }
503
504
    /**
505
     * {@inheritdoc}
506
     */
507 63
    public function offsetGet($offset)
508
    {
509 63
        return $this->get($offset);
510
    }
511
512
    /**
513
     * {@inheritdoc}
514
     */
515 96
    public function offsetSet($offset, $value)
516
    {
517 96
        if ( ! isset($offset)) {
518 96
            $this->add($value);
519 96
            return;
520
        }
521
522
        $this->set($offset, $value);
523
    }
524
525
    /**
526
     * {@inheritdoc}
527
     */
528 6
    public function offsetUnset($offset)
529
    {
530 6
        return $this->remove($offset);
531
    }
532
533
    /**
534
     * {@inheritdoc}
535
     */
536 825
    public function isEmpty()
537
    {
538 825
        return $this->collection->isEmpty() && $this->count() === 0;
539
    }
540
541
    /**
542
     * {@inheritdoc}
543
     */
544 24
    public function clear()
545
    {
546 24
        if ($this->initialized && $this->isEmpty()) {
547 1
            $this->collection->clear();
548
549 1
            return;
550
        }
551
552 23
        $uow = $this->em->getUnitOfWork();
553
554 23
        if ($this->association['type'] & ClassMetadata::TO_MANY &&
555 23
            $this->association['orphanRemoval'] &&
556 23
            $this->owner) {
557
            // we need to initialize here, as orphan removal acts like implicit cascadeRemove,
558
            // hence for event listeners we need the objects in memory.
559 6
            $this->initialize();
560
561 6
            foreach ($this->collection as $element) {
562 6
                $uow->scheduleOrphanRemoval($element);
563
            }
564
        }
565
566 23
        $this->collection->clear();
567
568 23
        $this->initialized = true; // direct call, {@link initialize()} is too expensive
569
570 23
        if ($this->association['isOwningSide'] && $this->owner) {
571 17
            $this->changed();
572
573 17
            $uow->scheduleCollectionDeletion($this);
574
575 17
            $this->takeSnapshot();
576
        }
577 23
    }
578
579
    /**
580
     * Called by PHP when this collection is serialized. Ensures that only the
581
     * elements are properly serialized.
582
     *
583
     * Internal note: Tried to implement Serializable first but that did not work well
584
     *                with circular references. This solution seems simpler and works well.
585
     *
586
     * @return array
587
     */
588 2
    public function __sleep()
589
    {
590 2
        return ['collection', 'initialized'];
591
    }
592
593
    /**
594
     * Extracts a slice of $length elements starting at position $offset from the Collection.
595
     *
596
     * If $length is null it returns all elements from $offset to the end of the Collection.
597
     * Keys have to be preserved by this method. Calling this method will only return the
598
     * selected slice and NOT change the elements contained in the collection slice is called on.
599
     *
600
     * @param int      $offset
601
     * @param int|null $length
602
     *
603
     * @return array
604
     */
605 15
    public function slice($offset, $length = null)
606
    {
607 15
        if ( ! $this->initialized && ! $this->isDirty && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
608 13
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
609
610 13
            return $persister->slice($this, $offset, $length);
611
        }
612
613 2
        return parent::slice($offset, $length);
614
    }
615
616
    /**
617
     * Cleans up internal state of cloned persistent collection.
618
     *
619
     * The following problems have to be prevented:
620
     * 1. Added entities are added to old PC
621
     * 2. New collection is not dirty, if reused on other entity nothing
622
     * changes.
623
     * 3. Snapshot leads to invalid diffs being generated.
624
     * 4. Lazy loading grabs entities from old owner object.
625
     * 5. New collection is connected to old owner and leads to duplicate keys.
626
     *
627
     * @return void
628
     */
629 11
    public function __clone()
630
    {
631 11
        if (is_object($this->collection)) {
632 11
            $this->collection = clone $this->collection;
633
        }
634
635 11
        $this->initialize();
636
637 11
        $this->owner    = null;
638 11
        $this->snapshot = [];
639
640 11
        $this->changed();
641 11
    }
642
643
    /**
644
     * Selects all elements from a selectable that match the expression and
645
     * return a new collection containing these elements.
646
     *
647
     * @param \Doctrine\Common\Collections\Criteria $criteria
648
     *
649
     * @return Collection
650
     *
651
     * @throws \RuntimeException
652
     */
653 25
    public function matching(Criteria $criteria)
654
    {
655 25
        if ($this->isDirty) {
656 3
            $this->initialize();
657
        }
658
659 25
        if ($this->initialized) {
660 3
            return $this->collection->matching($criteria);
0 ignored issues
show
Bug introduced by
The method matching() does not exist on Doctrine\Common\Collections\Collection. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Collections\AbstractLazyCollection. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

660
            return $this->collection->/** @scrutinizer ignore-call */ matching($criteria);
Loading history...
661
        }
662
663 22
        if ($this->association['type'] === ClassMetadata::MANY_TO_MANY) {
664 12
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
665
666 12
            return new ArrayCollection($persister->loadCriteria($this, $criteria));
667
        }
668
669 10
        $builder         = Criteria::expr();
670 10
        $ownerExpression = $builder->eq($this->backRefFieldName, $this->owner);
671 10
        $expression      = $criteria->getWhereExpression();
672 10
        $expression      = $expression ? $builder->andX($expression, $ownerExpression) : $ownerExpression;
673
674 10
        $criteria = clone $criteria;
675 10
        $criteria->where($expression);
676 10
        $criteria->orderBy($criteria->getOrderings() ?: $this->association['orderBy'] ?? []);
677
678 10
        $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);
679
680 10
        return ($this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY)
681 7
            ? new LazyCriteriaCollection($persister, $criteria)
682 10
            : new ArrayCollection($persister->loadCriteria($criteria));
683
    }
684
685
    /**
686
     * Retrieves the wrapped Collection instance.
687
     *
688
     * @return \Doctrine\Common\Collections\Collection
689
     */
690 829
    public function unwrap()
691
    {
692 829
        return $this->collection;
693
    }
694
695
    /**
696
     * {@inheritdoc}
697
     */
698 161
    protected function doInitialize()
699
    {
700
        // Has NEW objects added through add(). Remember them.
701 161
        $newlyAddedDirtyObjects = [];
702
703 161
        if ($this->isDirty) {
704 17
            $newlyAddedDirtyObjects = $this->collection->toArray();
705
        }
706
707 161
        $this->collection->clear();
708 161
        $this->em->getUnitOfWork()->loadCollection($this);
709 161
        $this->takeSnapshot();
710
711 161
        if ($newlyAddedDirtyObjects) {
712 17
            $this->restoreNewObjectsInDirtyCollection($newlyAddedDirtyObjects);
713
        }
714 161
    }
715
716
    /**
717
     * @param object[] $newObjects
718
     *
719
     * Note: the only reason why this entire looping/complexity is performed via `spl_object_hash`
720
     *       is because we want to prevent using `array_udiff()`, which is likely to cause very
721
     *       high overhead (complexity of O(n^2)). `array_diff_key()` performs the operation in
722
     *       core, which is faster than using a callback for comparisons
723
     */
724 17
    private function restoreNewObjectsInDirtyCollection(array $newObjects) : void
725
    {
726 17
        $loadedObjects               = $this->collection->toArray();
727 17
        $newObjectsByOid             = \array_combine(\array_map('spl_object_hash', $newObjects), $newObjects);
728 17
        $loadedObjectsByOid          = \array_combine(\array_map('spl_object_hash', $loadedObjects), $loadedObjects);
729 17
        $newObjectsThatWereNotLoaded = \array_diff_key($newObjectsByOid, $loadedObjectsByOid);
0 ignored issues
show
Bug introduced by
It seems like $newObjectsByOid can also be of type false; however, parameter $array1 of array_diff_key() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

729
        $newObjectsThatWereNotLoaded = \array_diff_key(/** @scrutinizer ignore-type */ $newObjectsByOid, $loadedObjectsByOid);
Loading history...
Bug introduced by
It seems like $loadedObjectsByOid can also be of type false; however, parameter $array2 of array_diff_key() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

729
        $newObjectsThatWereNotLoaded = \array_diff_key($newObjectsByOid, /** @scrutinizer ignore-type */ $loadedObjectsByOid);
Loading history...
730
731 17
        if ($newObjectsThatWereNotLoaded) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $newObjectsThatWereNotLoaded 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...
732
            // Reattach NEW objects added through add(), if any.
733 16
            \array_walk($newObjectsThatWereNotLoaded, [$this->collection, 'add']);
734
735 16
            $this->isDirty = true;
736
        }
737 17
    }
738
}
739