ArrayCollection   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 230
ccs 56
cts 56
cp 1
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A first() 0 4 1
A last() 0 4 1
A getIterator() 0 4 1
A add() 0 6 1
A set() 0 4 1
A get() 0 4 2
A isEmpty() 0 4 1
A contains() 0 4 1
A indexOf() 0 4 1
A count() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 8 2
A offsetUnset() 0 4 1
A slice() 0 4 1
A toArray() 0 4 1
A replaceWith() 0 6 1
A removeGaps() 0 4 1
A remove() 0 11 2
A containsKey() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Util;
13
14
/**
15
 * Array collection
16
 *
17
 * Provides a wrapper around a standard PHP array.
18
 */
19
class ArrayCollection implements \IteratorAggregate, \Countable, \ArrayAccess
20
{
21
    /**
22
     * @var array
23
     */
24
    private $elements;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param array $elements
30
     */
31 2118
    public function __construct(array $elements = [])
32
    {
33 2118
        $this->elements = $elements;
34 2118
    }
35
36
    /**
37
     * @return string
38
     */
39 111
    public function first()
40
    {
41 111
        return reset($this->elements);
42
    }
43
44
    /**
45
     * @return mixed
46
     */
47 3
    public function last()
48
    {
49 3
        return end($this->elements);
50
    }
51
52
    /**
53
     * Retrieve an external iterator
54
     *
55
     * @return \ArrayIterator
56
     */
57 3
    public function getIterator()
58
    {
59 3
        return new \ArrayIterator($this->elements);
60
    }
61
62
    /**
63
     * @param mixed $element
64
     *
65
     * @return bool
66
     */
67 1941
    public function add($element)
68
    {
69 1941
        $this->elements[] = $element;
70
71 1941
        return true;
72
    }
73
74
    /**
75
     * @param mixed $key
76
     * @param mixed $value
77
     */
78 6
    public function set($key, $value)
79
    {
80 6
        $this->elements[$key] = $value;
81 6
    }
82
83
    /**
84
     * @param mixed $key
85
     *
86
     * @return mixed
87
     */
88 6
    public function get($key)
89
    {
90 6
        return isset($this->elements[$key]) ? $this->elements[$key] : null;
91
    }
92
93
    /**
94
     * @param mixed $key
95
     *
96
     * @return mixed|null
97
     */
98 12
    public function remove($key)
99
    {
100 12
        if (!array_key_exists($key, $this->elements)) {
101 9
            return;
102
        }
103
104 12
        $removed = $this->elements[$key];
105 12
        unset($this->elements[$key]);
106
107 12
        return $removed;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113 3
    public function isEmpty()
114
    {
115 3
        return empty($this->elements);
116
    }
117
118
    /**
119
     * @param mixed $element
120
     *
121
     * @return bool
122
     */
123 3
    public function contains($element)
124
    {
125 3
        return in_array($element, $this->elements, true);
126
    }
127
128
    /**
129
     * @param mixed $element
130
     *
131
     * @return mixed|false
132
     */
133 3
    public function indexOf($element)
134
    {
135 3
        return array_search($element, $this->elements, true);
136
    }
137
138
    /**
139
     * @param mixed $key
140
     *
141
     * @return bool
142
     */
143 6
    public function containsKey($key)
144
    {
145 6
        return array_key_exists($key, $this->elements);
146
    }
147
148
    /**
149
     * Count elements of an object
150
     *
151
     * @return int The count as an integer.
152
     */
153 111
    public function count()
154
    {
155 111
        return count($this->elements);
156
    }
157
158
    /**
159
     * Whether an offset exists
160
     *
161
     * @param mixed $offset An offset to check for.
162
     *
163
     * @return bool true on success or false on failure.
164
     */
165 3
    public function offsetExists($offset)
166
    {
167 3
        return $this->containsKey($offset);
168
    }
169
170
    /**
171
     * Offset to retrieve
172
     *
173
     * @param mixed $offset The offset to retrieve.
174
     *
175
     * @return mixed
176
     */
177 3
    public function offsetGet($offset)
178
    {
179 3
        return $this->get($offset);
180
    }
181
182
    /**
183
     * Offset to set
184
     *
185
     * @param mixed $offset The offset to assign the value to.
186
     * @param mixed $value  The value to set.
187
     *
188
     * @return void
189
     */
190 3
    public function offsetSet($offset, $value)
191
    {
192 3
        if ($offset === null) {
193 3
            $this->add($value);
194 1
        } else {
195 3
            $this->set($offset, $value);
196
        }
197 3
    }
198
199
    /**
200
     * Offset to unset
201
     *
202
     * @param mixed $offset The offset to unset.
203
     *
204
     * @return void
205
     */
206 3
    public function offsetUnset($offset)
207
    {
208 3
        $this->remove($offset);
209 3
    }
210
211
    /**
212
     * Returns a subset of the array
213
     *
214
     * @param int      $offset
215
     * @param int|null $length
216
     *
217
     * @return array
218
     */
219 99
    public function slice($offset, $length = null)
220
    {
221 99
        return array_slice($this->elements, $offset, $length, true);
222
    }
223
224
    /**
225
     * @return array
226
     */
227 1863
    public function toArray()
228
    {
229 1863
        return $this->elements;
230
    }
231
232
    /**
233
     * @param array $elements
234
     *
235
     * @return $this
236
     */
237 3
    public function replaceWith(array $elements)
238
    {
239 3
        $this->elements = $elements;
240
241 3
        return $this;
242
    }
243
244 3
    public function removeGaps()
245
    {
246 3
        $this->elements = array_filter($this->elements);
247 3
    }
248
}
249