Failed Conditions
Push — 2.8.x ( 0ee171...60c486 )
by Benjamin
22:42 queued 16:29
created

PersistentCollection::remove()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

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

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

715
        $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

715
        $newObjectsThatWereNotLoaded = \array_diff_key($newObjectsByOid, /** @scrutinizer ignore-type */ $loadedObjectsByOid);
Loading history...
716
717 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...
718
            // Reattach NEW objects added through add(), if any.
719 16
            \array_walk($newObjectsThatWereNotLoaded, [$this->collection, 'add']);
720
721 16
            $this->isDirty = true;
722
        }
723 17
    }
724
}
725