Completed
Push — master ( ff01c5...5cf8b4 )
by Ítalo
03:14
created

Set   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 15
c 2
b 1
f 1
lcom 1
cbo 5
dl 0
loc 109
ccs 0
cts 60
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A at() 0 4 1
A get() 0 6 1
A containsKey() 0 4 1
A contains() 0 4 1
A set() 0 6 1
A addAll() 0 10 4
A remove() 0 7 1
A removeKey() 0 7 1
A getIterator() 0 4 1
A add() 0 6 1
A removeAll() 0 8 2
1
<?php
2
3
namespace Collections;
4
5
use Collections\Iterator\SetIterator;
6
use Collections\Traits\GuardTrait;
7
use Collections\Traits\StrictKeyedIterableTrait;
8
9
class Set extends AbstractCollectionArray implements SetInterface
10
{
11
    use GuardTrait,
12
        StrictKeyedIterableTrait;
13
14
    public function at($k)
15
    {
16
        return $this[$k];
17
    }
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function get($key)
23
    {
24
        $this->validateKeyBounds($key);
25
26
        return $this->container[$key];
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function containsKey($key)
33
    {
34
        return array_key_exists($key, $this->container);
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function contains($item)
41
    {
42
        return in_array($item, $this->container, true);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function set($key, $value)
49
    {
50
        $this->container[$key] = $value;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function addAll($items)
59
    {
60
        if (!is_array($items) && !$items instanceof \Traversable) {
61
            throw new \InvalidArgumentException('The items must be an array or Traversable');
62
        }
63
64
        foreach ($items as $value) {
65
            $this->add($value);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function remove($element)
73
    {
74
        $key = array_search($element, $this->container);
75
        $this->removeKey($key);
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function removeKey($key)
84
    {
85
        $this->validateKeyBounds($key);
86
        unset($this->container[$key]);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function getIterator()
95
    {
96
        return new SetIterator();
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function add($item)
103
    {
104
        $this->container[] = $item;
105
106
        return $this;
107
    }
108
109
    public function removeAll(Iterable $iterable)
110
    {
111
        $iterable->each(function ($item) {
112
            if ($this->contains($item)) {
113
                $this->remove($item);
114
            }
115
        });
116
    }
117
}
118