Completed
Push — master ( 01780a...8fa51e )
by Ítalo
09:25
created

StrictIterableTrait   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 43.36%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 44
c 6
b 1
f 2
lcom 0
cbo 3
dl 0
loc 249
ccs 49
cts 113
cp 0.4336
rs 8.3396

16 Methods

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

How to fix   Complexity   

Complex Class

Complex classes like StrictIterableTrait 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 StrictIterableTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\Iterable;
6
use Collections\VectorInterface;
7
8
trait StrictIterableTrait
9
{
10
    use CommonMutableContainerTrait;
11
12
    /**
13
     * {@inheritDoc}
14
     * @return $this
15
     */
16 1
    public function map(callable $callable)
17
    {
18 1
        $res = new static();
19 1
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
20 1
            $res[] = $callable($v);
21 1
        }
22
23 1
        return $res;
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     * @return $this
29
     */
30 2
    public function mapWithKey($callback)
31
    {
32 2
        $res = new static();
33 2
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
34 2
            $res[$k] = $callback($k, $v);
35 2
        }
36 2
37 2
        return $res;
38
    }
39 2
40
    /**
41
     * {@inheritDoc}
42 2
     * @return $this
43 2
     */
44
    public function filter(callable $callable)
45
    {
46
        $res = new static();
47
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
48
            if ($callable($v)) {
49
                $res[] = $v;
50
            }
51
        }
52
53
        return $res;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     * @return $this
59
     */
60
    public function filterWithKey($callback)
61 1
    {
62
        $res = new static();
63 1
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
64
            if ($callback($k, $v)) {
65 1
                $res[$k] = $v;
66
            }
67
        }
68
69 1
        return $res;
70 1
    }
71 1
72 1
    /**
73
     * {@inheritDoc}
74 1
     * @return $this
75
     */
76 1
    public function zip(Iterable $iterable)
77
    {
78
        $res = new static();
79
        $it = $iterable->getIterator();
80
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
81
            if (!$it->valid()) {
82
                break;
83
            }
84
            $res[] = new Pair($v, $it->current());
85
            $it->next();
86
        }
87
88
        return $res;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     * @return $this
94
     */
95
    public function take($size = 1)
96
    {
97
        $res = new static();
98
99
        if ($size <= 0) {
100
            return $res;
101
        }
102
103
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
104
            $res[] = $v;
105
            if (--$size === 0) {
106
                break;
107
            }
108
        }
109
110
        return $res;
111
    }
112
113
    public function takeWhile(callable $callable)
114
    {
115
        $res = new static();
116
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
117
            if (!$callable($v)) {
118
                break;
119
            }
120
            $res[] = $v;
121
        }
122
123
        return $res;
124
    }
125
126
    public function skip($n)
127
    {
128
        $res = new static();
129
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
130
            if ($n <= 0) {
131
                $res[] = $v;
132
            } else {
133
                --$n;
134
            }
135
        }
136
137
        return $res;
138
    }
139
140
    public function skipWhile(callable $callable)
141
    {
142
        $res = new static();
143 1
        $skip = true;
144
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
145 1
            if ($skip) {
146
                if ($callable($v)) {
147 1
                    continue;
148 1
                }
149 1
                $skip = false;
150
            }
151 1
            $res[] = $v;
152 1
        }
153 1
154 1
        return $res;
155
    }
156 1
157
    public function slice($start, $length)
158
    {
159
        $res = new static();
160
        if ($length <= 0) {
161
            return $res;
162
        }
163 2
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
164
            if ($start !== 0) {
165 2
                --$start;
166 1
                continue;
167 1
            }
168
            $res[] = $v;
169 1
            if (--$length === 0) {
170
                break;
171
            }
172
        }
173
174
        return $res;
175
    }
176 2
177
    public function concat(\Traversable $iterable)
178 2
    {
179
        $res = [];
180 2
181
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
182
            $res[] = $v;
183
        }
184
185
        foreach ($iterable as $v) {
186
            $res[] = $v;
187
        }
188
        $this->container = $res;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
189
190
        return $this;
191
    }
192
193
    /**
194
     * {@inheritDoc}
195
     * @return $this
196
     */
197
    public function first()
198
    {
199 1
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
200
            return $v;
201 1
        }
202 1
203 1
        return null;
204
    }
205 1
206
    /**
207 1
     * {@inheritDoc}
208
     * @return $this
209
     */
210
    public function last()
211
    {
212
        $result = $this->toArray();
213
214
        return array_pop($result);
215
    }
216
217
    /**
218
     * {@inheritDoc}
219
     * @return $this
220
     */
221
    public function each(callable $callable)
222
    {
223
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
224
            $callable($v);
225
        }
226
227
        return $this;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function exists(callable $fn)
234
    {
235
        foreach ($this as $element) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictIterableTrait> is not traversable.
Loading history...
236
            if ($fn($element)) {
237
                return true;
238
            }
239
        }
240
241
        return false;
242
    }
243
244
    public function concatAll()
245
    {
246
        /** @var VectorInterface $results */
247
        $results = new static();
248
        $this->each(function (Iterable $subArray) use ($results) {
249
            $subArray->each(function ($item) use ($results) {
250
                $results->add($item);
251
            });
252
        });
253
254
        return $results;
255
    }
256
}
257