AbstractCollection::contains()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace GenericCollections\Abstracts;
3
4
use GenericCollections\Exceptions\InvalidElementTypeException;
5
use GenericCollections\Interfaces\CollectionInterface;
6
use GenericCollections\Internal\DataArray;
7
use GenericCollections\Traits\CollectionMethods;
8
9
/**
10
 * This is a partial implementation of CollectionInterface
11
 * The main Collection object extends this class but types methods
12
 *
13
 * Use this class to implement your own collection class.
14
 * You will need to implement the getElementType method on your concrete case.
15
 *
16
 * @package GenericCollections\Abstracts
17
 */
18
abstract class AbstractCollection extends DataArray implements CollectionInterface
19
{
20
    use CollectionMethods;
21
22 24
    public function add($element)
23
    {
24 24
        if ($this->optionUniqueValues() && $this->contains($element)) {
25 1
            return false;
26
        }
27 24
        if (! $this->checkElementType($element)) {
28 1
            throw new InvalidElementTypeException('collection', $this->getElementType(), get_class($this));
29
        }
30 23
        $this->data[] = $element;
31 23
        return true;
32
    }
33
34 7
    public function contains($element)
35
    {
36 7
        return in_array($element, $this->data, $this->optionComparisonIsIdentical());
37
    }
38
39 6
    public function remove($element)
40
    {
41 6
        $index = array_search($element, $this->data, $this->optionComparisonIsIdentical());
42 6
        if (false === $index) {
43 4
            return false;
44
        }
45 6
        unset($this->data[$index]);
46 6
        if ($index < count($this->data)) {
47 6
            $this->data = array_values($this->data);
48
        }
49 6
        return true;
50
    }
51
52 2
    public function retainAll(array $elements)
53
    {
54 2
        $changed = false;
55 2
        foreach ($this->data as $index => $element) {
56 2
            if (! in_array($element, $elements, $this->optionComparisonIsIdentical())) {
57 2
                unset($this->data[$index]);
58 2
                $changed = true;
59
            }
60
        }
61 2
        if ($changed) {
62 2
            $this->data = array_values($this->data);
63
        }
64 2
        return $changed;
65
    }
66
67 2
    public function removeAll(array $elements)
68
    {
69 2
        $changed = false;
70 2
        foreach ($elements as $element) {
71
            do {
72 2
                $removed = $this->remove($element);
73 2
                if ($removed) {
74 2
                    $changed = true;
75
                }
76 2
            } while ($removed);
77
        }
78 2
        return $changed;
79
    }
80
81 1
    public function removeIf(callable $callable)
82
    {
83 1
        $changed = false;
84 1
        foreach ($this->data as $element) {
85 1
            if (true === call_user_func($callable, $element)) {
86 1
                if ($this->remove($element)) {
87 1
                    $changed = true;
88
                }
89
            }
90
        }
91 1
        return $changed;
92
    }
93
}
94