Completed
Pull Request — master (#615)
by Filippo
14:02 queued 03:29
created

DisallowRemoveByReference   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 13 2
1
<?php
2
3
namespace DoctrineModule\Stdlib\Hydrator\Strategy;
4
5
/**
6
 * When this strategy is used for Collections, if the new collection does not contain elements that are present in
7
 * the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
8
 * instance, if the collection initially contains elements A and B, and that the new collection contains elements B
9
 * and C, then the final collection will contain elements A, B and C.
10
 *
11
 * This strategy is by reference, this means it won't use the public API to remove elements
12
 *
13
 * @license MIT
14
 * @link    http://www.doctrine-project.org/
15
 * @since   0.7.0
16
 * @author  Michael Gallego <[email protected]>
17
 */
18
class DisallowRemoveByReference extends AbstractCollectionStrategy
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 1
    public function hydrate($value)
24
    {
25 1
        $collection      = $this->getCollectionFromObjectByReference();
26 1
        $collectionArray = $collection->toArray();
27
28 1
        $toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
29
30 1
        foreach ($toAdd as $element) {
31 1
            $collection->add($element);
32
        }
33
34 1
        return $collection;
35
    }
36
}
37