AbstractCollection::filter()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 Baleen\Migrations\Common\Collection;
21
22
use ArrayIterator;
23
use Baleen\Migrations\Exception\InvalidArgumentException;
24
use Baleen\Migrations\Exception\Version\Collection\AlreadyExistsException;
25
use Baleen\Migrations\Exception\Version\Collection\CollectionException;
26
use Baleen\Migrations\Delta\DeltaInterface;
27
use Closure;
28
use Zend\Stdlib\ArrayUtils;
29
30
/**
31
 * Based on the Doctrine\CollectionAbstract project.
32
 *
33
 * @see https://github.com/doctrine/collections
34
 *
35
 * @author Gabriel Somoza <[email protected]>
36
 */
37
abstract class AbstractCollection implements CollectionInterface
38
{
39
    /**
40
     * An array containing the entries of this collection.
41
     *
42
     * @var DeltaInterface[]
43
     */
44
    private $elements;
45
46
    /**
47
     * Initializes a new AbstractCollection.
48
     *
49
     * @param DeltaInterface[] $elements
50
     *
51
     * @throws AlreadyExistsException
52
     * @throws CollectionException
53
     * @throws InvalidArgumentException
54
     */
55 158
    public function __construct($elements = [])
56
    {
57 158
        if (!is_array($elements)) {
58 4
            if ($elements instanceof \Traversable) {
59 3
                $elements = ArrayUtils::iteratorToArray($elements);
60 3
            } else {
61 1
                throw new InvalidArgumentException(
62
                    "Constructor parameter 'versions' must be an array or Traversable object."
63 1
                );
64
            }
65 3
        }
66
67 157
        $this->elements = [];
68 157
        foreach ($elements as $element) {
69 141
            if (!is_object($element) || !$element instanceof DeltaInterface) {
70 1
                throw CollectionException::invalidObjectException($element, DeltaInterface::class);
71
            }
72 140
            $this->add($element);
73 156
        }
74 156
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 21
    final public function toArray()
80
    {
81 21
        return $this->elements;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 26
    final public function first()
88
    {
89 26
        return reset($this->elements);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 13
    final public function last()
96
    {
97 13
        return end($this->elements);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103 1
    final public function key()
104
    {
105 1
        return key($this->elements);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 1
    final public function next()
112
    {
113 1
        return next($this->elements);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 1
    final public function current()
120
    {
121 1
        return current($this->elements);
122
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 4
    public function remove($key)
128
    {
129 4
        $key = (string) $key;
130 4
        if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
131 1
            return null;
132
        }
133
134 3
        $removed = $this->elements[$key];
135 3
        unset($this->elements[$key]);
136
137 3
        return $removed;
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143 144
    final public function contains($key)
144
    {
145 144
        $key = (string) $key;
146 144
        return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152 1
    final public function containsVersion(DeltaInterface $version)
153
    {
154 1
        $key = (string) $version->getId();
155 1
        return $this->contains($key);
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161 2 View Code Duplication
    final public function exists(Closure $p)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163 2
        foreach ($this->elements as $key => $element) {
164 2
            if ($p($key, $element)) {
165 1
                return true;
166
            }
167 2
        }
168
169 1
        return false;
170
    }
171
172
    /**
173
     * {@inheritDoc}
174
     */
175 59
    final public function get($key)
176
    {
177 59
        $key = (string) $key;
178 59
        return isset($this->elements[$key]) ? $this->elements[$key] : null;
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184 34
    final public function getKeys()
185
    {
186 34
        return array_keys($this->elements);
187
    }
188
189
    /**
190
     * {@inheritDoc}
191
     */
192 1
    final public function getValues()
193
    {
194 1
        return array_values($this->elements);
195
    }
196
197
    /**
198
     * {@inheritDoc}
199
     */
200 53
    final public function count()
201
    {
202 53
        return count($this->elements);
203
    }
204
205
    /**
206
     * {@inheritDoc}
207
     */
208 10
    public function replace(DeltaInterface $version)
209
    {
210 10
        $key = $version->getId()->toString();
211 10
        $replacedElement = $this->contains($key) ? $this->get($key) : null;
212 10
        $this->elements[$key] = $version;
213
214 10
        return $replacedElement;
215
    }
216
217
    /**
218
     * {@inheritDoc}
219
     */
220 143
    public function add(DeltaInterface $value)
221
    {
222 143
        $key = $value->getId()->toString();
223 143
        if ($this->contains($key)) {
224 1
            throw new AlreadyExistsException(sprintf(
225 1
                'Element with key "%s" already exists. Remove it first or use replace() if you want to overwrite it.',
226
                $key
227 1
            ));
228
        }
229 143
        $this->elements[$key] = $value;
230 143
        return true;
231
    }
232
233
    /**
234
     * {@inheritDoc}
235
     */
236 3
    final public function isEmpty()
237
    {
238 3
        return empty($this->elements);
239
    }
240
241
    /**
242
     * Required by interface IteratorAggregate.
243
     *
244
     * {@inheritDoc}
245
     */
246 54
    final public function getIterator()
247
    {
248 54
        return new ArrayIterator($this->elements);
249
    }
250
251
    /**
252
     * {@inheritDoc}
253
     */
254 34
    final public function map(Closure $func)
255
    {
256 34
        return array_map($func, $this->elements);
257
    }
258
259
    /**
260
     * {@inheritDoc}
261
     */
262 1
    final public function filter(Closure $p)
263
    {
264 1
        return new static(array_filter($this->elements, $p));
265
    }
266
267
    /**
268
     * {@inheritDoc}
269
     */
270 4 View Code Duplication
    final public function forAll(Closure $p)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
    {
272 4
        foreach ($this->elements as $key => $element) {
273 4
            if ( ! $p($key, $element)) {
274 1
                return false;
275
            }
276 4
        }
277
278 3
        return true;
279
    }
280
281
    /**
282
     * {@inheritDoc}
283
     */
284 14
    final public function partition(Closure $p)
285
    {
286 14
        $matches = $noMatches = array();
287
288 14
        foreach ($this->elements as $key => $element) {
289 14
            if ($p($key, $element)) {
290 12
                $matches[$key] = $element;
291 12
            } else {
292 13
                $noMatches[$key] = $element;
293
            }
294 14
        }
295
296 14
        return array(new static($matches), new static($noMatches));
297
    }
298
299
    /**
300
     * Returns a string representation of this object.
301
     *
302
     * @return string
303
     */
304 1
    public function __toString()
305
    {
306 1
        return __CLASS__ . '@' . spl_object_hash($this);
307
    }
308
309
    /**
310
     * {@inheritDoc}
311
     */
312 8
    public function clear()
313
    {
314 8
        $this->elements = [];
315 8
    }
316
317
    /**
318
     * @inheritDoc
319
     */
320 2
    final public function slice($offset, $length = null)
321
    {
322 2
        return array_slice($this->elements, $offset, $length, true);
323
    }
324
325
    /**
326
     * @inheritdoc
327
     */
328 10
    final public function merge(CollectionInterface $collection)
329
    {
330 10
        foreach ($collection as $version) {
331 9
            $this->replace($version);
332 10
        }
333
334 10
        return $this;
335
    }
336
337
    /**
338
     * @inheritdoc
339
     */
340 33
    final public function getPosition($key) {
341 33
        return array_search($key, $this->getKeys()) + 1;
342
    }
343
344
    /**
345
     * @inheritdoc
346
     */
347 34
    final public function getByPosition($position)
348
    {
349 34
        $index = $position - 1;
350 34
        $keys = $this->getKeys();
351 34
        if (!isset($keys[$index]) || !array_key_exists($index, $keys)) {
352 6
            return null;
353
        }
354 28
        return $this->get($keys[$index]);
355
    }
356
}
357