Passed
Push — master ( 9fe25d...e6dd30 )
by Christian
02:33
created

MutableCollection::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCollection;
6
7
final class MutableCollection extends AbstractCollection
8
{
9
    /**
10
     * @inheritDoc
11
     */
12
    final public function map(callable $fn): CollectionInterface
13
    {
14
        $this->items = array_map($fn, $this->all());
15
16
        return $this;
17
    }
18
19
    /**
20
     * @inheritDoc
21
     */
22
    final public function filter(callable $fn): CollectionInterface
23
    {
24
        $this->items = array_filter($this->all(), $fn, ARRAY_FILTER_USE_BOTH);
25
26
        return $this;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    final public function each(callable $fn): CollectionInterface
33
    {
34
        foreach ($this->all() as $key => $item) {
35
            $this->items[$key] = $fn($item, $key);
36
        }
37
38
        return $this;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    final public function reverse(): CollectionInterface
45
    {
46
        $this->items = array_reverse($this->all());
47
        return $this;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    final public function unique(): CollectionInterface
54
    {
55
        $this->items = array_unique($this->all());
56
57
        return $this;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    final public function diff(CollectionInterface $collection, callable $comparator = null): CollectionInterface
64
    {
65
        $this->items = array_udiff($this->all(), $collection->all(), $comparator ?? Helper::getObjectSafeComparator());
66
67
        return $this;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    final public function merge(CollectionInterface $collection): CollectionInterface
74
    {
75
        $this->items = array_merge($this->all(), $collection->all());
76
77
        return $this;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    final public function intersect(CollectionInterface $collection): CollectionInterface
84
    {
85
        $this->items = array_intersect($this->all(), $collection->all());
86
87
        return $this;
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    final public function sort(callable $comparator = null): CollectionInterface
94
    {
95
        uasort($this->items, $comparator ?? Helper::getObjectSafeComparator());
96
97
        return $this;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    final public function kSort(callable $comparator = null): CollectionInterface
104
    {
105
        uksort($this->items, $comparator ?? Helper::getStringComparator());
106
107
        return $this;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    final public function reIndex(): CollectionInterface
114
    {
115
        $this->items = $this->values();
116
        return $this;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    final public function chunk(int $size, callable $fn): CollectionInterface
123
    {
124
        foreach (array_chunk($this->all(), $size, true) as $chunk) {
125
            foreach ($chunk as $index => $value) {
126
                $this->items[$index] = $fn($value, $index);
127
            }
128
        }
129
130
        return $this;
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    final public function unset(...$offsets): CollectionInterface
137
    {
138
        foreach ($offsets as $offset) {
139
            unset($this->items[$offset]);
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    final public function set($offset, $item): CollectionInterface
149
    {
150
        $this->items[$offset] = $item;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    final public function push($item): CollectionInterface
159
    {
160
        $this->items[] = $item;
161
162
        return $this;
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    final public function unshift(...$items): CollectionInterface
169
    {
170
        array_unshift($this->items, ...$items);
171
172
        return $this;
173
    }
174
}
175