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

LazyIterableTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 0
cbo 15
dl 0
loc 127
rs 9.1666

20 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 9 2
A toValuesArray() 0 4 1
A toVector() 0 4 1
A toImmVector() 0 4 1
A toSet() 0 4 1
A toImmSet() 0 4 1
A lazy() 0 4 1
A values() 0 4 1
A map() 0 4 1
A filter() 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 last() 0 8 2
A each() 0 8 2
1
<?php
2
3
namespace Collections\Iterator;
4
5
use Collections\ArrayList;
6
use Collections\Immutable\ImmArrayList;
7
use Collections\Immutable\ImmDictionary;
8
use Collections\Immutable\ImmSet;
9
use Collections\Set;
10
11
trait LazyIterableTrait
12
{
13
    public function toArray()
14
    {
15
        $arr = array();
16
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyIterableTrait> is not traversable.
Loading history...
17
            $arr[] = $v;
18
        }
19
20
        return $arr;
21
    }
22
23
    public function toValuesArray()
24
    {
25
        return $this->toArray();
26
    }
27
28
    public function toVector()
29
    {
30
        return new ArrayList($this);
31
    }
32
33
    public function toImmVector()
34
    {
35
        return new ImmArrayList($this);
36
    }
37
38
    public function toSet()
39
    {
40
        return new Set($this);
41
    }
42
43
    public function toImmSet()
44
    {
45
        return new ImmSet($this);
46
    }
47
48
    public function lazy()
49
    {
50
        return $this;
51
    }
52
53
    public function values()
54
    {
55
        return new LazyValuesIterable($this);
56
    }
57
58
    public function map(callable $callback)
59
    {
60
        return new LazyMapIterable($this, $callback);
61
    }
62
63
    public function filter(callable $callback)
64
    {
65
        return new LazyFilterIterable($this, $callback);
66
    }
67
68
    public function zip($iterable)
69
    {
70
        if (is_array($iterable)) {
71
            $iterable = new ImmDictionary($iterable);
72
        }
73
74
        return new LazyZipIterable($this, $iterable);
75
    }
76
77
    public function take($size = 1)
78
    {
79
        return new LazyTakeIterable($this, $size);
80
    }
81
82
    public function takeWhile($fn)
83
    {
84
        return new LazyTakeWhileIterable($this, $fn);
85
    }
86
87
    public function skip($n)
88
    {
89
        return new LazySkipIterable($this, $n);
90
    }
91
92
    public function skipWhile($fn)
93
    {
94
        return new LazySkipWhileIterable($this, $fn);
95
    }
96
97
    public function slice($start, $len)
98
    {
99
        return new LazySliceIterable($this, $start, $len);
100
    }
101
102
    public function concat($iterable)
103
    {
104
        if (is_array($iterable)) {
105
            $iterable = new ImmDictionary($iterable);
106
        }
107
108
        return new LazyConcatIterable($this, $iterable);
109
    }
110
111
    public function first()
112
    {
113
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyIterableTrait> is not traversable.
Loading history...
114
            return $v;
115
        }
116
117
        return null;
118
    }
119
120
    public function last()
121
    {
122
        $v = null;
123
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyIterableTrait> 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...
124
        }
125
126
        return $v;
127
    }
128
129
    public function each(callable $callable)
130
    {
131
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Iterator\LazyIterableTrait> is not traversable.
Loading history...
132
            $callable($v, $k);
133
        }
134
135
        return $this;
136
    }
137
}
138