Completed
Push — develop ( 9e8aa9...73717b )
by Baptiste
01:42
created

Map   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
dl 0
loc 279
ccs 86
cts 86
cp 1
rs 9.2
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A reduce() 0 3 1
A merge() 0 3 1
A join() 0 3 1
A valueType() 0 3 1
A valid() 0 3 1
A values() 0 3 1
A clear() 0 3 1
A rewind() 0 3 1
A offsetSet() 0 3 1
A load() 0 7 2
A map() 0 3 1
A partition() 0 3 1
A groupBy() 0 3 1
A size() 0 3 1
A loadedMap() 0 8 1
A offsetUnset() 0 3 1
A put() 0 3 1
A next() 0 3 1
A keyType() 0 3 1
A contains() 0 3 1
A foreach() 0 3 1
A __construct() 0 5 1
A filter() 0 3 1
A offsetExists() 0 3 1
A equals() 0 3 1
A offsetGet() 0 3 1
A key() 0 3 1
A current() 0 3 1
A keys() 0 3 1
A of() 0 14 1
A get() 0 3 1
A remove() 0 3 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 18.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Lazy;
5
6
use Innmind\Compose\Lazy;
7
use Innmind\Immutable\{
8
    MapInterface,
9
    Map as Base,
10
    Str,
11
    Sequence,
12
    Pair,
13
    SetInterface,
14
    StreamInterface,
15
    Exception\LogicException
16
};
17
18
final class Map implements MapInterface
1 ignored issue
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
19
{
20
    private $keyType;
21
    private $valueType;
22
    private $values;
23
    private $loaded;
24
25 26
    public function __construct(string $keyType, string $valueType)
26
    {
27 26
        $this->keyType = new Str($keyType);
28 26
        $this->valueType = new Str($valueType);
29 26
        $this->values = new Base('mixed', 'mixed');
30 26
    }
31
32 26
    public static function of(string $keyType, string $valueType, Pair ...$values): self
33
    {
34 26
        $self = new self($keyType, $valueType);
35 26
        $self->values = Sequence::of(...$values)->reduce(
36 26
            $self->values,
37 26
            static function(Base $map, Pair $pair): Base {
38 26
                return $map->put(
39 26
                    $pair->key(),
40 26
                    $pair->value()
41
                );
42 26
            }
43
        );
44
45 26
        return $self;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 12
    public function keyType(): Str
52
    {
53 12
        return $this->keyType;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 12
    public function valueType(): Str
60
    {
61 12
        return $this->valueType;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function size(): int
68
    {
69 1
        return $this->values->size();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 4
    public function count(): int
76
    {
77 4
        return $this->values->count();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    public function current()
84
    {
85 1
        return $this->load($this->values->current());
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function key()
92
    {
93 1
        return $this->load($this->values->key());
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function next()
100
    {
101 1
        $this->values->next();
102 1
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function rewind()
108
    {
109 1
        $this->values->rewind();
110 1
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function valid()
116
    {
117 1
        return $this->values->valid();
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    public function offsetExists($offset): bool
124
    {
125 1
        return $this->contains($offset);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 1
    public function offsetGet($offset)
132
    {
133 1
        return $this->get($offset);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function offsetSet($offset, $value)
140
    {
141 1
        throw new LogicException('You can\'t modify a map');
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function offsetUnset($offset)
148
    {
149 1
        throw new LogicException('You can\'t modify a map');
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 3
    public function put($key, $value): MapInterface
156
    {
157 3
        return $this->loadedMap()->put($key, $value);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 15
    public function get($key)
164
    {
165 15
        return $this->loadedMap()->get($key);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 3
    public function contains($key): bool
172
    {
173 3
        return $this->loadedMap()->contains($key);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 20
    public function clear(): MapInterface
180
    {
181 20
        return new Base((string) $this->keyType, (string) $this->valueType);
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 1
    public function equals(MapInterface $map): bool
188
    {
189 1
        return $this->loadedMap()->equals($map);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195 1
    public function filter(callable $predicate): MapInterface
196
    {
197 1
        return $this->loadedMap()->filter($predicate);
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 1
    public function foreach(callable $function): MapInterface
204
    {
205 1
        return $this->loadedMap()->foreach($function);
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 1
    public function groupBy(callable $discriminator): MapInterface
212
    {
213 1
        return $this->loadedMap()->groupBy($discriminator);
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 1
    public function keys(): SetInterface
220
    {
221 1
        return $this->loadedMap()->keys();
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 1
    public function values(): StreamInterface
228
    {
229 1
        return $this->loadedMap()->values();
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 1
    public function map(callable $function): MapInterface
236
    {
237 1
        return $this->loadedMap()->map($function);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243 1
    public function join(string $separator): Str
244
    {
245 1
        return $this->loadedMap()->join($separator);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 1
    public function remove($key): MapInterface
252
    {
253 1
        return $this->loadedMap()->remove($key);
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259 1
    public function merge(MapInterface $map): MapInterface
260
    {
261 1
        return $this->loadedMap()->merge($map);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 1
    public function partition(callable $predicate): MapInterface
268
    {
269 1
        return $this->loadedMap()->partition($predicate);
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275 1
    public function reduce($carry, callable $reducer)
276
    {
277 1
        return $this->loadedMap()->reduce($carry, $reducer);
278
    }
279
280 21
    private function load($value)
281
    {
282 21
        if ($value instanceof Lazy) {
283 19
            return $value->load();
284
        }
285
286 3
        return $value;
287
    }
288
289 20
    private function loadedMap(): Base
290
    {
291 20
        return $this->loaded ?? $this->loaded = $this->values->reduce(
292 20
            $this->clear(),
293 20
            function(Base $map, $key, $value): Base {
294 20
                return $map->put(
295 20
                    $this->load($key),
296 20
                    $this->load($value)
297
                );
298 20
            }
299
        );
300
    }
301
}
302