Completed
Push — master ( 431209...0f7fe1 )
by Ítalo
03:12
created

StrictKeyedIterableTrait   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 248
Duplicated Lines 51.61 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 6.94%

Importance

Changes 9
Bugs 3 Features 2
Metric Value
wmc 54
c 9
b 3
f 2
lcom 0
cbo 4
dl 128
loc 248
ccs 10
cts 144
cp 0.0694
rs 6.8539

19 Methods

Rating   Name   Duplication   Size   Complexity  
A keys() 0 9 2
A take() 0 15 4
A skipWhile() 0 16 4
A map() 9 9 2
A mapWithKey() 9 9 2
A filter() 11 11 3
A filterWithKey() 11 11 3
A zip() 14 14 3
A takeWhile() 12 12 3
A skip() 13 13 3
B slice() 19 19 5
A first() 0 8 2
A firstKey() 0 8 2
A last() 0 8 2
A lastKey() 0 8 2
A each() 0 8 2
A exists() 0 10 3
A concatAll() 12 12 1
B concatRecurse() 18 18 6

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like StrictKeyedIterableTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StrictKeyedIterableTrait, and based on these observations, apply Extract Interface, too.

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 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 first()
163
    {
164 1
        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...
165
            return $v;
166 1
        }
167 1
168 1
        return null;
169
    }
170 1
171 1
    public function firstKey()
172 1
    {
173 1
        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...
174
            return $k;
175 1
        }
176
177
        return null;
178
    }
179
180
    public function last()
181
    {
182
        $v = null;
183
        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...
184
        }
185
186
        return $v;
187
    }
188
189
    public function lastKey()
190
    {
191
        $k = null;
192
        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...
193
        }
194
195
        return $k;
196
    }
197
198
    /**
199
     * {@inheritDoc}
200
     * @return $this
201
     */
202
    public function each(callable $callable)
203
    {
204
        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...
205
            $callable($v, $k);
206
        }
207
208
        return $this;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function exists(callable $fn)
215
    {
216
        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...
217
            if ($fn($key, $element)) {
218
                return true;
219
            }
220
        }
221
222
        return false;
223
    }
224
225 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...
226
    {
227
        /** @var MapInterface $results */
228
        $results = new static();
229
        $this->each(function (Iterable $subArray) use ($results) {
230
            $subArray->each(function ($item, $key) use ($results) {
231
                $results->add($key, $item);
232
            });
233
        });
234
235
        return $results;
236
    }
237
238 View Code Duplication
    private function concatRecurse($array, $array1)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
239
    {
240
        $merged = $array;
241
242
        foreach ($array1 as $key => $value) {
243
            $isValid = function ($value) {
244
                return (is_array($value) || $value instanceof \Traversable);
245
            };
246
247
            if (($isValid($value) && isset($merged[$key])) && $isValid($merged[$key])) {
248
                $merged[$key] = $this->concatRecurse($merged[$key], $value);
249
            } else {
250
                $merged[$key] = $value;
251
            }
252
        }
253
254
        return $merged;
255
    }
256
}
257