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

StrictKeyedIterableTrait   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 233
Duplicated Lines 48.93 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 7.25%

Importance

Changes 10
Bugs 3 Features 2
Metric Value
wmc 51
c 10
b 3
f 2
lcom 0
cbo 4
dl 114
loc 233
ccs 10
cts 138
cp 0.0725
rs 8.3206

18 Methods

Rating   Name   Duplication   Size   Complexity  
A keys() 0 9 2
A map() 9 9 2
A mapWithKey() 9 9 2
A filter() 11 11 3
A filterWithKey() 11 11 3
A take() 0 15 4
A takeWhile() 12 12 3
A skip() 13 13 3
A skipWhile() 0 16 4
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
    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