ArraySegregator::shift()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Collections\Interactors;
6
7
class ArraySegregator extends AbstractInteractor
8
{
9
    /**
10
     * Pops one or more elements from the end of the elements.
11
     *
12
     * @return void
13
     */
14 2
    public function pop(): void
15
    {
16 2
        array_pop($this->elements);
17 2
    }
18
19
    /**
20
     * Shifts the first element from the array.
21
     *
22
     * @return void
23
     */
24 1
    public function shift(): void
25
    {
26 1
        array_shift($this->elements);
27 1
    }
28
29
    /**
30
     * Applies the given function to each element in the collection and returns
31
     * a new collection with the elements returned by the function.
32
     *
33
     * @param \Closure $func
34
     *
35
     * @return array
36
     */
37 1
    public function map(\Closure $func): array
38
    {
39 1
        return array_map($func, $this->elements);
40
    }
41
42
    /**
43
     * Returns all the elements of this collection that satisfy the predicate p.
44
     * The order of the elements is preserved.
45
     *
46
     * @param \Closure $p The predicate used for filtering.
47
     *
48
     * @return array A collection with the results of the filter operation.
49
     */
50 1
    public function filter(\Closure $p): array
51
    {
52 1
        return array_filter($this->elements, $p);
53
    }
54
55
    /**
56
     * Partitions this collection in two collections according to a predicate.
57
     * Keys are preserved in the resulting collections.
58
     *
59
     * @param \Closure $p The predicate on which to partition.
60
     *
61
     * @return array[]    An array with two elements. The first element contains the collection
62
     *                    of elements where the predicate returned TRUE, the second element
63
     *                    contains the collection of elements where the predicate returned FALSE.
64
     */
65 1
    public function partition(\Closure $p): array
66
    {
67 1
        $matches = $noMatches = [];
68
69 1
        foreach ($this->elements as $key => $element) {
70 1
            if ($p($key, $element)) {
71 1
                $matches[$key] = $element;
72
            } else {
73 1
                $noMatches[$key] = $element;
74
            }
75
        }
76
77 1
        return [$matches, $noMatches];
78
    }
79
80
    /**
81
     * Extracts a slice of $length elements starting at position $offset from the Collection.
82
     *
83
     * If $length is null it returns all elements from $offset to the end of the Collection.
84
     * Keys have to be preserved by this method. Calling this method will only return the
85
     * selected slice and NOT change the elements contained in the collection slice is called on.
86
     *
87
     * @param int|null $limit   The maximum number of elements to return, or null for no limit.
88
     * @param int      $offset  The offset to start from.
89
     *
90
     * @return array
91
     */
92 1
    public function take(int $limit = null, int $offset = 0): array
93
    {
94 1
        return array_slice($this->elements, $offset, $limit, true);
95
    }
96
}
97