Completed
Push — master ( 747a6a...c6c655 )
by Ítalo
02:55
created

LazyKeyedIterableTrait   C

Complexity

Total Complexity 38

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 19

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 38
c 1
b 0
f 0
lcom 0
cbo 19
dl 0
loc 185
rs 5.775

28 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 9 2
A toValuesArray() 0 9 2
A toKeysArray() 0 9 2
A toVector() 0 4 1
A toImmVector() 0 4 1
A toMap() 0 4 1
A toImmMap() 0 4 1
A toSet() 0 4 1
A toImmSet() 0 4 1
A lazy() 0 4 1
A values() 0 4 1
A keys() 0 4 1
A map() 0 4 1
A mapWithKey() 0 4 1
A filter() 0 4 1
A filterWithKey() 0 4 1
A zip() 0 8 2
A take() 0 4 1
A takeWhile() 0 4 1
A skip() 0 4 1
A skipWhile() 0 4 1
A slice() 0 4 1
A concat() 0 8 2
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
1
<?php
2
3
namespace Collections\Iterator;
4
5
use Collections\ArrayList;
6
use Collections\Dictionary;
7
use Collections\Immutable\ImmArrayList;
8
use Collections\Immutable\ImmDictionary;
9
use Collections\Immutable\ImmSet;
10
use Collections\Set;
11
12
trait LazyKeyedIterableTrait
13
{
14
    public function toArray()
15
    {
16
        $arr = [];
17
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
18
            $arr[$k] = $v;
19
        }
20
21
        return $arr;
22
    }
23
24
    public function toValuesArray()
25
    {
26
        $arr = [];
27
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
28
            $arr[] = $v;
29
        }
30
31
        return $arr;
32
    }
33
34
    public function toKeysArray()
35
    {
36
        $arr = [];
37
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
38
            $arr[] = $k;
39
        }
40
41
        return $arr;
42
    }
43
44
    public function toVector()
45
    {
46
        return new ArrayList($this);
47
    }
48
49
    public function toImmVector()
50
    {
51
        return new ImmArrayList($this);
52
    }
53
54
    public function toMap()
55
    {
56
        return new Dictionary($this);
57
    }
58
59
    public function toImmMap()
60
    {
61
        return new ImmDictionary($this);
62
    }
63
64
    public function toSet()
65
    {
66
        return new Set($this);
67
    }
68
69
    public function toImmSet()
70
    {
71
        return new ImmSet($this);
72
    }
73
74
    public function lazy()
75
    {
76
        return $this;
77
    }
78
79
    public function values()
80
    {
81
        return new LazyValuesIterable($this);
82
    }
83
84
    public function keys()
85
    {
86
        return new LazyKeysIterable($this);
87
    }
88
89
    public function map($callback)
90
    {
91
        return new LazyMapKeyedIterable($this, $callback);
92
    }
93
94
    public function mapWithKey($callback)
95
    {
96
        return new LazyMapWithKeyIterable($this, $callback);
97
    }
98
99
    public function filter($callback)
100
    {
101
        return new LazyFilterKeyedIterable($this, $callback);
102
    }
103
104
    public function filterWithKey($callback)
105
    {
106
        return new LazyFilterWithKeyIterable($this, $callback);
107
    }
108
109
    public function zip($iterable)
110
    {
111
        if (is_array($iterable)) {
112
            $iterable = new ImmDictionary($iterable);
113
        }
114
115
        return new LazyZipKeyedIterable($this, $iterable);
116
    }
117
118
    public function take($n)
119
    {
120
        return new LazyTakeKeyedIterable($this, $n);
121
    }
122
123
    public function takeWhile($fn)
124
    {
125
        return new LazyTakeWhileKeyedIterable($this, $fn);
126
    }
127
128
    public function skip($n)
129
    {
130
        return new LazySkipKeyedIterable($this, $n);
131
    }
132
133
    public function skipWhile($fn)
134
    {
135
        return new LazySkipWhileKeyedIterable($this, $fn);
136
    }
137
138
    public function slice($start, $len)
139
    {
140
        return new LazySliceKeyedIterable($this, $start, $len);
141
    }
142
143
    public function concat($iterable)
144
    {
145
        if (is_array($iterable)) {
146
            $iterable = new ImmDictionary($iterable);
147
        }
148
149
        return new LazyConcatIterable($this, $iterable);
150
    }
151
152
    public function first()
153
    {
154
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
155
            return $v;
156
        }
157
158
        return null;
159
    }
160
161
    public function firstKey()
162
    {
163
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
164
            return $k;
165
        }
166
167
        return null;
168
    }
169
170
    public function last()
171
    {
172
        $v = null;
173
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
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...
174
        }
175
176
        return $v;
177
    }
178
179
    public function lastKey()
180
    {
181
        $k = null;
182
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
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...
183
        }
184
185
        return $k;
186
    }
187
188
    public function each(callable $callable)
189
    {
190
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyKeyedIterableTrait> is not traversable.
Loading history...
191
            $callable($v, $k);
192
        }
193
194
        return $this;
195
    }
196
}