Completed
Push — master ( 801ae3...f2d109 )
by Ítalo
04:18
created

StrictKeyedIterableTrait::skip()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\ArrayList;
6
use Collections\Iterable;
7
use Collections\MapInterface;
8
9
trait StrictKeyedIterableTrait
10
{
11
    use CommonMutableContainerTrait;
12
13
    public function keys()
14
    {
15
        $res = new ArrayList();
16
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
17
            $res[] = $k;
18
        }
19
20
        return $res;
21
    }
22
23 View Code Duplication
    public function map(callable $callback)
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...
24
    {
25
        $res = new static();
26
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
27
            $res[$k] = $callback($v);
28
        }
29
30
        return $res;
31
    }
32
33 View Code Duplication
    public function mapWithKey($callback)
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...
34
    {
35
        $res = new static();
36
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
37
            $res[$k] = $callback($k, $v);
38
        }
39
40
        return $res;
41
    }
42
43 View Code Duplication
    public function filter(callable $callback)
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...
44
    {
45
        $res = new static();
46
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
47
            if ($callback($v)) {
48
                $res[$k] = $v;
49
            }
50
        }
51
52
        return $res;
53
    }
54
55 View Code Duplication
    public function filterWithKey($callback)
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...
56
    {
57
        $res = new static();
58
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
59
            if ($callback($k, $v)) {
60
                $res[$k] = $v;
61
            }
62
        }
63
64
        return $res;
65
    }
66
67
    public function take($size = 1)
68
    {
69
        $res = new static();
70
        if ($size <= 0) {
71
            return $res;
72
        }
73
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
74
            $res[$k] = $v;
75
            if (--$size === 0) {
76
                break;
77
            }
78
        }
79
80
        return $res;
81
    }
82
83 View Code Duplication
    public function takeWhile($fn)
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...
84
    {
85
        $res = new static();
86
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
87
            if (!$fn($v)) {
88
                break;
89
            }
90
            $res[$k] = $v;
91
        }
92
93
        return $res;
94
    }
95
96 View Code Duplication
    public function skip($n)
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...
97
    {
98
        $res = new static();
99
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
100
            if ($n <= 0) {
101
                $res[$k] = $v;
102
            } else {
103
                --$n;
104
            }
105
        }
106
107
        return $res;
108
    }
109
110
    public function skipWhile($fn)
111
    {
112
        $res = new static();
113
        $skip = true;
114
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
115
            if ($skip) {
116
                if ($fn($v)) {
117
                    continue;
118
                }
119
                $skip = false;
120
            }
121
            $res[$k] = $v;
122
        }
123
124
        return $res;
125
    }
126
127 View Code Duplication
    public function slice($start, $lenght)
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...
128
    {
129
        $res = new static();
130
        if ($lenght <= 0) {
131
            return $res;
132
        }
133
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
134
            if ($start !== 0) {
135
                --$start;
136
                continue;
137
            }
138
            $res[$k] = $v;
139
            if (--$lenght === 0) {
140
                break;
141
            }
142
        }
143
144
        return $res;
145
    }
146
147
    public function first()
148
    {
149
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
150
            return $v;
151
        }
152
153
        return null;
154
    }
155
156
    public function firstKey()
157
    {
158
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
159
            return $k;
160
        }
161
162 1
        return null;
163
    }
164 1
165
    public function last()
166 1
    {
167 1
        $v = null;
168 1
        foreach ($this as $v) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
169
        }
170 1
171 1
        return $v;
172 1
    }
173 1
174
    public function lastKey()
175 1
    {
176
        $k = null;
177
        foreach ($this as $k => $_) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
178
        }
179
180
        return $k;
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     * @return $this
186
     */
187
    public function each(callable $callable)
188
    {
189
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
190
            $callable($v, $k);
191
        }
192
193
        return $this;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function exists(callable $fn)
200
    {
201
        foreach ($this as $key => $element) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
202
            if ($fn($key, $element)) {
203
                return true;
204
            }
205
        }
206
207
        return false;
208
    }
209
210 View Code Duplication
    public function concatAll()
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...
211
    {
212
        /** @var MapInterface $results */
213
        $results = new static();
214
        $this->each(function (Iterable $subArray) use ($results) {
215
            $subArray->each(function ($item, $key) use ($results) {
216
                $results->add($key, $item);
217
            });
218
        });
219
220
        return $results;
221
    }
222
223 View Code Duplication
    protected function concatRecurse($array, $array1)
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...
224
    {
225
        $merged = $array;
226
227
        foreach ($array1 as $key => $value) {
228
            $isValid = function ($value) {
229
                return (is_array($value) || $value instanceof \Traversable);
230
            };
231
232
            if (($isValid($value) && isset($merged[$key])) && $isValid($merged[$key])) {
233
                $merged[$key] = $this->concatRecurse($merged[$key], $value);
234
            } else {
235
                $merged[$key] = $value;
236
            }
237
        }
238
239
        return $merged;
240
    }
241
}
242