Failed Conditions
Pull Request — 2.6 (#7882)
by
unknown
06:45
created

PersistentCollection::offsetUnset()   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 1
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 363
    public function __construct(EntityManagerInterface $em, $class, Collection $collection)
108
    {
109 363
        $this->collection  = $collection;
110 363
        $this->em          = $em;
111 363
        $this->typeClass   = $class;
112 363
        $this->initialized = true;
113 363
    }
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 357
    public function setOwner($entity, array $assoc)
126
    {
127 357
        $this->owner            = $entity;
128 357
        $this->association      = $assoc;
129 357
        $this->backRefFieldName = $assoc['inversedBy'] ?: $assoc['mappedBy'];
130 357
    }
131
132
    /**
133
     * INTERNAL:
134
     * Gets the collection owner.
135
     *
136
     * @return object
137
     */
138 149
    public function getOwner()
139
    {
140 149
        return $this->owner;
141
    }
142
143
    /**
144
     * @return Mapping\ClassMetadata
145
     */
146 6
    public function getTypeClass()
147
    {
148 6
        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 57
    public function hydrateAdd($element)
161
    {
162 57
        $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 57
        if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) {
167
            // Set back reference to owner
168 12
            $this->typeClass->reflFields[$this->backRefFieldName]->setValue(
169 12
                $element, $this->owner
170
            );
171
172 12
            $this->em->getUnitOfWork()->setOriginalEntityProperty(
173 12
                spl_object_hash($element), $this->backRefFieldName, $this->owner
174
            );
175
        }
176 57
    }
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 3
    public function hydrateSet($key, $element)
188
    {
189 3
        $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 3
        if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) {
194
            // Set back reference to owner
195 2
            $this->typeClass->reflFields[$this->backRefFieldName]->setValue(
196 2
                $element, $this->owner
197
            );
198
        }
199 3
    }
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 288
    public function initialize()
208
    {
209 288
        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 278
            return;
211
        }
212
213 57
        $this->doInitialize();
214
215 57
        $this->initialized = true;
216 57
    }
217
218
    /**
219
     * INTERNAL:
220
     * Tells this collection to take a snapshot of its current state.
221
     *
222
     * @return void
223
     */
224 140
    public function takeSnapshot()
225
    {
226 140
        $this->snapshot = $this->collection->toArray();
227 140
        $this->isDirty  = false;
228 140
    }
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 2
    public function getSnapshot()
237
    {
238 2
        return $this->snapshot;
239
    }
240
241
    /**
242
     * INTERNAL:
243
     * getDeleteDiff
244
     *
245
     * @return array
246
     */
247 98
    public function getDeleteDiff()
248
    {
249 98
        return array_udiff_assoc(
250 98
            $this->snapshot,
251 98
            $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 98
    public function getInsertDiff()
263
    {
264 98
        return array_udiff_assoc(
265 98
            $this->collection->toArray(),
266 98
            $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 115
    public function getMapping()
277
    {
278 115
        return $this->association;
279
    }
280
281
    /**
282
     * Marks this collection as changed/dirty.
283
     *
284
     * @return void
285
     */
286 61
    private function changed()
287
    {
288 61
        if ($this->isDirty) {
289 19
            return;
290
        }
291
292 61
        $this->isDirty = true;
293
294 61
        if ($this->association !== null &&
295 61
            $this->association['isOwningSide'] &&
296 61
            $this->association['type'] === ClassMetadata::MANY_TO_MANY &&
297 61
            $this->owner &&
298 61
            $this->em->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify()) {
299 1
            $this->em->getUnitOfWork()->scheduleForDirtyCheck($this->owner);
300
        }
301 61
    }
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 289
    public function isDirty()
310
    {
311 289
        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 278
    public function setDirty($dirty)
322
    {
323 278
        $this->isDirty = $dirty;
324 278
    }
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 205
    public function setInitialized($bool)
334
    {
335 205
        $this->initialized = $bool;
336 205
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341 9
    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 9
        $removed = parent::remove($key);
348
349 9
        if ( ! $removed) {
350
            return $removed;
351
        }
352
353 9
        $this->changed();
354
355 9
        if ($this->association !== null &&
356 9
            $this->association['type'] & ClassMetadata::TO_MANY &&
357 9
            $this->owner &&
358 9
            $this->association['orphanRemoval']) {
359 1
            $this->em->getUnitOfWork()->scheduleOrphanRemoval($removed);
360
        }
361
362 9
        return $removed;
363
    }
364
365
    /**
366
     * {@inheritdoc}
367
     */
368 9
    public function removeElement($element)
369
    {
370 9
        if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
371
            if ($this->collection->contains($element)) {
372
                return $this->collection->removeElement($element);
373
            }
374
375
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
376
377
            return $persister->removeElement($this, $element);
378
        }
379
380 9
        $removed = parent::removeElement($element);
381
382 9
        if ( ! $removed) {
383
            return $removed;
384
        }
385
386 9
        $this->changed();
387
388 9
        if ($this->association !== null &&
389 9
            $this->association['type'] & ClassMetadata::TO_MANY &&
390 9
            $this->owner &&
391 9
            $this->association['orphanRemoval']) {
392 3
            $this->em->getUnitOfWork()->scheduleOrphanRemoval($element);
393
        }
394
395 9
        return $removed;
396
    }
397
398
    /**
399
     * {@inheritdoc}
400
     */
401 10
    public function containsKey($key)
402
    {
403 10
        if (! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY
404 10
            && isset($this->association['indexBy'])) {
405 2
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
406
407 2
            return $this->collection->containsKey($key) || $persister->containsKey($this, $key);
408
        }
409
410 8
        return parent::containsKey($key);
411
    }
412
413
    /**
414
     * {@inheritdoc}
415
     */
416 11
    public function contains($element)
417
    {
418 11
        if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
419 2
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
420
421 2
            return $this->collection->contains($element) || $persister->contains($this, $element);
422
        }
423
424 9
        return parent::contains($element);
425
    }
426
427
    /**
428
     * {@inheritdoc}
429
     */
430 37
    public function get($key)
431
    {
432 37
        if ( ! $this->initialized
433 37
            && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY
434 37
            && isset($this->association['indexBy'])
435
        ) {
436
            if (!$this->typeClass->isIdentifierComposite && $this->typeClass->isIdentifier($this->association['indexBy'])) {
437
                return $this->em->find($this->typeClass->name, $key);
438
            }
439
440
            return $this->em->getUnitOfWork()->getCollectionPersister($this->association)->get($this, $key);
441
        }
442
443 37
        return parent::get($key);
444
    }
445
446
    /**
447
     * {@inheritdoc}
448
     */
449 268
    public function count()
450
    {
451 268
        if (! $this->initialized && $this->association !== null && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
452 9
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
453
454 9
            return $persister->count($this) + ($this->isDirty ? $this->collection->count() : 0);
455
        }
456
457 265
        return parent::count();
458
    }
459
460
    /**
461
     * {@inheritdoc}
462
     */
463 4
    public function set($key, $value)
464
    {
465 4
        parent::set($key, $value);
466
467 4
        $this->changed();
468
469 4
        if (is_object($value) && $this->em) {
470 4
            $this->em->getUnitOfWork()->cancelOrphanRemoval($value);
471
        }
472 4
    }
473
474
    /**
475
     * {@inheritdoc}
476
     */
477 30
    public function add($value)
478
    {
479 30
        $this->collection->add($value);
480
481 30
        $this->changed();
482
483 30
        if (is_object($value) && $this->em) {
484 29
            $this->em->getUnitOfWork()->cancelOrphanRemoval($value);
485
        }
486
487 30
        return true;
488
    }
489
490
    /* ArrayAccess implementation */
491
492
    /**
493
     * {@inheritdoc}
494
     */
495 8
    public function offsetExists($offset)
496
    {
497 8
        return $this->containsKey($offset);
498
    }
499
500
    /**
501
     * {@inheritdoc}
502
     */
503 31
    public function offsetGet($offset)
504
    {
505 31
        return $this->get($offset);
506
    }
507
508
    /**
509
     * {@inheritdoc}
510
     */
511 12
    public function offsetSet($offset, $value)
512
    {
513 12
        if ( ! isset($offset)) {
514 12
            $this->add($value);
515 12
            return;
516
        }
517
518
        $this->set($offset, $value);
519
    }
520
521
    /**
522
     * {@inheritdoc}
523
     */
524 5
    public function offsetUnset($offset)
525
    {
526 5
        return $this->remove($offset);
527
    }
528
529
    /**
530
     * {@inheritdoc}
531
     */
532 280
    public function isEmpty()
533
    {
534 280
        return $this->collection->isEmpty() && $this->count() === 0;
535
    }
536
537
    /**
538
     * {@inheritdoc}
539
     */
540 16
    public function clear()
541
    {
542 16
        if ($this->initialized && $this->isEmpty()) {
543 1
            $this->collection->clear();
544
545 1
            return;
546
        }
547
548 15
        $uow = $this->em->getUnitOfWork();
549
550 15
        if ($this->association['type'] & ClassMetadata::TO_MANY &&
551 15
            $this->association['orphanRemoval'] &&
552 15
            $this->owner) {
553
            // we need to initialize here, as orphan removal acts like implicit cascadeRemove,
554
            // hence for event listeners we need the objects in memory.
555 2
            $this->initialize();
556
557 2
            foreach ($this->collection as $element) {
558 2
                $uow->scheduleOrphanRemoval($element);
559
            }
560
        }
561
562 15
        $this->collection->clear();
563
564 15
        $this->initialized = true; // direct call, {@link initialize()} is too expensive
565
566 15
        if ($this->association['isOwningSide'] && $this->owner) {
567 14
            $this->changed();
568
569 14
            if (! $this->em->getClassMetadata(get_class($this->owner))->isChangeTrackingDeferredExplicit()) {
570 12
                $uow->scheduleCollectionDeletion($this);
571
            }
572
573 14
            $this->takeSnapshot();
574
        }
575 15
    }
576
577
    /**
578
     * Called by PHP when this collection is serialized. Ensures that only the
579
     * elements are properly serialized.
580
     *
581
     * Internal note: Tried to implement Serializable first but that did not work well
582
     *                with circular references. This solution seems simpler and works well.
583
     *
584
     * @return array
585
     */
586
    public function __sleep()
587
    {
588
        return ['collection', 'initialized'];
589
    }
590
591
    /**
592
     * Extracts a slice of $length elements starting at position $offset from the Collection.
593
     *
594
     * If $length is null it returns all elements from $offset to the end of the Collection.
595
     * Keys have to be preserved by this method. Calling this method will only return the
596
     * selected slice and NOT change the elements contained in the collection slice is called on.
597
     *
598
     * @param int      $offset
599
     * @param int|null $length
600
     *
601
     * @return array
602
     */
603 2
    public function slice($offset, $length = null)
604
    {
605 2
        if ( ! $this->initialized && ! $this->isDirty && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
606 2
            $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
607
608 2
            return $persister->slice($this, $offset, $length);
609
        }
610
611
        return parent::slice($offset, $length);
612
    }
613
614
    /**
615
     * Cleans up internal state of cloned persistent collection.
616
     *
617
     * The following problems have to be prevented:
618
     * 1. Added entities are added to old PC
619
     * 2. New collection is not dirty, if reused on other entity nothing
620
     * changes.
621
     * 3. Snapshot leads to invalid diffs being generated.
622
     * 4. Lazy loading grabs entities from old owner object.
623
     * 5. New collection is connected to old owner and leads to duplicate keys.
624
     *
625
     * @return void
626
     */
627 8
    public function __clone()
628
    {
629 8
        if (is_object($this->collection)) {
630 8
            $this->collection = clone $this->collection;
631
        }
632
633 8
        $this->initialize();
634
635 8
        $this->owner    = null;
636 8
        $this->snapshot = [];
637
638 8
        $this->changed();
639 8
    }
640
641
    /**
642
     * Selects all elements from a selectable that match the expression and
643
     * return a new collection containing these elements.
644
     *
645
     * @param \Doctrine\Common\Collections\Criteria $criteria
646
     *
647
     * @return Collection
648
     *
649
     * @throws \RuntimeException
650
     */
651 14
    public function matching(Criteria $criteria)
652
    {
653 14
        if ($this->isDirty) {
654 1
            $this->initialize();
655
        }
656
657 14
        if ($this->initialized) {
658 1
            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

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

727
        $newObjectsThatWereNotLoaded = \array_diff_key($newObjectsByOid, /** @scrutinizer ignore-type */ $loadedObjectsByOid);
Loading history...
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

727
        $newObjectsThatWereNotLoaded = \array_diff_key(/** @scrutinizer ignore-type */ $newObjectsByOid, $loadedObjectsByOid);
Loading history...
728
729 8
        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...
730
            // Reattach NEW objects added through add(), if any.
731 7
            \array_walk($newObjectsThatWereNotLoaded, [$this->collection, 'add']);
732
733 7
            $this->isDirty = true;
734
        }
735 8
    }
736
}
737