Completed
Push — master ( 1e5638...495e53 )
by Ítalo
03:13
created

StrictKeyedIterableTrait::recurse()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 38.115

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 17
ccs 2
cts 9
cp 0.2222
rs 7.7777
cc 8
eloc 8
nc 5
nop 2
crap 38.115
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
    public function map(callable $callback)
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 View Code Duplication
    public function zip($iterable)
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...
68
    {
69
        $res = new static();
70
        $it = $iterable->getIterator();
71
        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...
72
            if (!$it->valid()) {
73
                break;
74
            }
75
            $res[$k] = new Pair($v, $it->current());
76
            $it->next();
77
        }
78
79
        return $res;
80
    }
81
82
    public function take($size = 1)
83
    {
84
        $res = new static();
85
        if ($size <= 0) {
86
            return $res;
87
        }
88
        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...
89
            $res[$k] = $v;
90
            if (--$size === 0) {
91
                break;
92
            }
93
        }
94
95
        return $res;
96
    }
97
98 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...
99
    {
100
        $res = new static();
101
        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...
102
            if (!$fn($v)) {
103
                break;
104
            }
105
            $res[$k] = $v;
106
        }
107
108
        return $res;
109
    }
110
111 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...
112
    {
113
        $res = new static();
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 ($n <= 0) {
116
                $res[$k] = $v;
117
            } else {
118
                --$n;
119
            }
120
        }
121
122
        return $res;
123
    }
124
125
    public function skipWhile($fn)
126
    {
127
        $res = new static();
128
        $skip = true;
129
        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...
130
            if ($skip) {
131
                if ($fn($v)) {
132
                    continue;
133
                }
134
                $skip = false;
135
            }
136
            $res[$k] = $v;
137
        }
138
139
        return $res;
140
    }
141
142 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...
143
    {
144
        $res = new static();
145
        if ($lenght <= 0) {
146
            return $res;
147
        }
148
        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...
149
            if ($start !== 0) {
150
                --$start;
151
                continue;
152
            }
153
            $res[$k] = $v;
154
            if (--$lenght === 0) {
155
                break;
156
            }
157
        }
158
159
        return $res;
160
    }
161
162 1
    public function concat($iterable)
163
    {
164 1
        if (!is_array($iterable) && !$iterable instanceof \Traversable) {
165
            throw new \InvalidArgumentException('The items must be an array or Traversable');
166 1
        }
167 1
168 1
        $this->setAll($this->recurse($this, $iterable));
0 ignored issues
show
Bug introduced by
It seems like setAll() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
169
170 1
        return $this;
171 1
    }
172 1
173 1
    private function recurse($array, $array1)
174
    {
175 1
        foreach ($array1 as $key => $value) {
176
            // create new key in $array, if it is empty or not an array
177
            if (!isset($array[$key]) || (isset($array[$key]) && (!is_array($array[$key]) && !$array[$key] instanceof \Traversable))) {
178
                $array[$key] = [];
179
            }
180
181
            // overwrite the value in the base array
182
            if (is_array($value) || $value instanceof \Traversable) {
183
                $value = $this->recurse($array[$key], $value);
184
            }
185
            $array[$key] = $value;
186
        }
187
188
        return $array;
189
    }
190
191
    public function first()
192
    {
193
        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...
194
            return $v;
195
        }
196
197
        return null;
198
    }
199
200
    public function firstKey()
201
    {
202
        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...
203
            return $k;
204
        }
205
206
        return null;
207
    }
208
209
    public function last()
210
    {
211
        $v = null;
212
        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...
213
        }
214
215
        return $v;
216
    }
217
218
    public function lastKey()
219
    {
220
        $k = null;
221
        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...
222
        }
223
224
        return $k;
225
    }
226
227
    /**
228
     * {@inheritDoc}
229
     * @return $this
230
     */
231
    public function each(callable $callable)
232
    {
233
        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...
234
            $callable($v, $k);
235
        }
236
237
        return $this;
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function exists(callable $fn)
244
    {
245
        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...
246
            if ($fn($key, $element)) {
247
                return true;
248
            }
249
        }
250
251
        return false;
252
    }
253
254 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...
255
    {
256
        /** @var MapInterface $results */
257
        $results = new static();
258
        $this->each(function (Iterable $subArray) use ($results) {
259
            $subArray->each(function ($item, $key) use ($results) {
260
                $results->add($key, $item);
261
            });
262
        });
263
264
        return $results;
265
    }
266
}
267