GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( fd060d...258593 )
by Baptiste
03:01 queued 10s
created

PrimitiveTest   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 412
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 198
c 1
b 0
f 0
dl 0
loc 412
rs 8.96
wmc 43

43 Methods

Rating   Name   Duplication   Size   Complexity  
A testThrowWhenTryingToAccessIndexOfUnknownValue() 0 5 1
A testClear() 0 8 1
A testPad() 0 11 1
A testInterface() 0 5 1
A testThrowWhenTryingToAccessFirstElementOnEmptySequence() 0 5 1
A testIndicesOnEmptySequence() 0 9 1
A testGet() 0 3 1
A testType() 0 3 1
A testSize() 0 4 1
A testForeach() 0 12 1
A testFilter() 0 8 1
A testDropEnd() 0 8 1
A testAppend() 0 10 1
A testIndices() 0 9 1
A testToMapOf() 0 10 1
A testDrop() 0 8 1
A testTake() 0 8 1
A testEquals() 0 4 1
A testMap() 0 8 1
A testThrowWhenTryingToModifyTheTypeWhenMapping() 0 6 1
A testEmpty() 0 4 1
A testThrowWhenTryingToGroupEmptySequence() 0 5 1
A testDistinct() 0 8 1
A testReduce() 0 5 1
A testThrowWhenTryingToAccessLastElementOnEmptySequence() 0 5 1
A testIndexOf() 0 6 1
A testDiff() 0 10 1
A testSort() 0 8 1
A testIterator() 0 5 1
A testContains() 0 6 1
A testTakeEnd() 0 8 1
A testGroupBy() 0 13 1
A testSplitAt() 0 13 1
A testSlice() 0 8 1
A testThrowWhenIndexNotFound() 0 5 1
A testFirst() 0 3 1
A testIntersect() 0 10 1
A testPartition() 0 13 1
A testReverse() 0 8 1
A testToSequenceOf() 0 12 1
A testToSetOf() 0 12 1
A testLast() 0 3 1
A testAdd() 0 8 1

How to fix   Complexity   

Complex Class

Complex classes like PrimitiveTest 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.

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 PrimitiveTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Sequence;
5
6
use Innmind\Immutable\{
7
    Sequence\Primitive,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Sequence\Primitive was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
    Sequence\Implementation,
9
    Map,
10
    Sequence,
11
    Str,
12
    Set,
13
    Exception\OutOfBoundException,
14
    Exception\CannotGroupEmptyStructure,
15
    Exception\ElementNotFound,
16
};
17
use function Innmind\Immutable\unwrap;
18
use PHPUnit\Framework\TestCase;
19
20
class PrimitiveTest extends TestCase
21
{
22
    public function testInterface()
23
    {
24
        $this->assertInstanceOf(
25
            Implementation::class,
26
            new Primitive('mixed'),
27
        );
28
    }
29
30
    public function testType()
31
    {
32
        $this->assertSame('int', (new Primitive('int'))->type());
33
    }
34
35
    public function testSize()
36
    {
37
        $this->assertSame(2, (new Primitive('int', 1, 1))->size());
38
        $this->assertSame(2, (new Primitive('int', 1, 1))->count());
39
    }
40
41
    public function testIterator()
42
    {
43
        $this->assertSame(
44
            [1, 2, 3],
45
            \iterator_to_array((new Primitive('int', 1, 2, 3))->iterator()),
46
        );
47
    }
48
49
    public function testGet()
50
    {
51
        $this->assertSame(42, (new Primitive('int', 1, 42, 3))->get(1));
52
    }
53
54
    public function testThrowWhenIndexNotFound()
55
    {
56
        $this->expectException(OutOfBoundException::class);
57
58
        (new Primitive('int'))->get(0);
59
    }
60
61
    public function testDiff()
62
    {
63
        $a = new Primitive('int', 1, 2);
64
        $b = new Primitive('int', 2, 3);
65
        $c = $a->diff($b);
66
67
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
68
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
69
        $this->assertInstanceOf(Primitive::class, $c);
70
        $this->assertSame([1], \iterator_to_array($c->iterator()));
71
    }
72
73
    public function testDistinct()
74
    {
75
        $a = new Primitive('int', 1, 2, 1);
76
        $b = $a->distinct();
77
78
        $this->assertSame([1, 2, 1], \iterator_to_array($a->iterator()));
79
        $this->assertInstanceOf(Primitive::class, $b);
80
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
81
    }
82
83
    public function testDrop()
84
    {
85
        $a = new Primitive('int', 1, 2, 3, 4);
86
        $b = $a->drop(2);
87
88
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
89
        $this->assertInstanceOf(Primitive::class, $b);
90
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
91
    }
92
93
    public function testDropEnd()
94
    {
95
        $a = new Primitive('int', 1, 2, 3, 4);
96
        $b = $a->dropEnd(2);
97
98
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
99
        $this->assertInstanceOf(Primitive::class, $b);
100
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
101
    }
102
103
    public function testEquals()
104
    {
105
        $this->assertTrue((new Primitive('int', 1, 2))->equals(new Primitive('int', 1, 2)));
106
        $this->assertFalse((new Primitive('int', 1, 2))->equals(new Primitive('int', 2)));
107
    }
108
109
    public function testFilter()
110
    {
111
        $a = new Primitive('int', 1, 2, 3, 4);
112
        $b = $a->filter(fn($i) => $i % 2 === 0);
113
114
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
115
        $this->assertInstanceOf(Primitive::class, $b);
116
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
117
    }
118
119
    public function testForeach()
120
    {
121
        $sequence = new Primitive('int', 1, 2, 3, 4);
122
        $calls = 0;
123
        $sum = 0;
124
125
        $this->assertNull($sequence->foreach(function($i) use (&$calls, &$sum) {
126
            ++$calls;
127
            $sum += $i;
128
        }));
129
        $this->assertSame(4, $calls);
130
        $this->assertSame(10, $sum);
131
    }
132
133
    public function testThrowWhenTryingToGroupEmptySequence()
134
    {
135
        $this->expectException(CannotGroupEmptyStructure::class);
136
137
        (new Primitive('int'))->groupBy(fn($i) => $i);
138
    }
139
140
    public function testGroupBy()
141
    {
142
        $sequence = new Primitive('int', 1, 2, 3, 4);
143
        $groups = $sequence->groupBy(fn($i) => $i % 2);
144
145
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
146
        $this->assertInstanceOf(Map::class, $groups);
147
        $this->assertTrue($groups->isOfType('int', Sequence::class));
148
        $this->assertCount(2, $groups);
149
        $this->assertTrue($groups->get(0)->isOfType('int'));
150
        $this->assertTrue($groups->get(1)->isOfType('int'));
151
        $this->assertSame([2, 4], unwrap($groups->get(0)));
152
        $this->assertSame([1, 3], unwrap($groups->get(1)));
153
    }
154
155
    public function testThrowWhenTryingToAccessFirstElementOnEmptySequence()
156
    {
157
        $this->expectException(OutOfBoundException::class);
158
159
        (new Primitive('int'))->first();
160
    }
161
162
    public function testThrowWhenTryingToAccessLastElementOnEmptySequence()
163
    {
164
        $this->expectException(OutOfBoundException::class);
165
166
        (new Primitive('int'))->last();
167
    }
168
169
    public function testFirst()
170
    {
171
        $this->assertSame(2, (new Primitive('int', 2, 3, 4))->first());
172
    }
173
174
    public function testLast()
175
    {
176
        $this->assertSame(4, (new Primitive('int', 2, 3, 4))->last());
177
    }
178
179
    public function testContains()
180
    {
181
        $sequence = new Primitive('int', 1, 2, 3);
182
183
        $this->assertTrue($sequence->contains(2));
184
        $this->assertFalse($sequence->contains(4));
185
    }
186
187
    public function testIndexOf()
188
    {
189
        $sequence = new Primitive('int', 1, 2, 4);
190
191
        $this->assertSame(1, $sequence->indexOf(2));
192
        $this->assertSame(2, $sequence->indexOf(4));
193
    }
194
195
    public function testThrowWhenTryingToAccessIndexOfUnknownValue()
196
    {
197
        $this->expectException(ElementNotFound::class);
198
199
        (new Primitive('int'))->indexOf(1);
200
    }
201
202
    public function testIndices()
203
    {
204
        $a = new Primitive('string', '1', '2');
205
        $b = $a->indices();
206
207
        $this->assertSame(['1', '2'], \iterator_to_array($a->iterator()));
208
        $this->assertInstanceOf(Primitive::class, $b);
209
        $this->assertSame('int', $b->type());
210
        $this->assertSame([0, 1], \iterator_to_array($b->iterator()));
211
    }
212
213
    public function testIndicesOnEmptySequence()
214
    {
215
        $a = new Primitive('string');
216
        $b = $a->indices();
217
218
        $this->assertSame([], \iterator_to_array($a->iterator()));
219
        $this->assertInstanceOf(Primitive::class, $b);
220
        $this->assertSame('int', $b->type());
221
        $this->assertSame([], \iterator_to_array($b->iterator()));
222
    }
223
224
    public function testMap()
225
    {
226
        $a = new Primitive('int', 1, 2, 3);
227
        $b = $a->map(fn($i) => $i * 2);
228
229
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
230
        $this->assertInstanceOf(Primitive::class, $b);
231
        $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator()));
232
    }
233
234
    public function testThrowWhenTryingToModifyTheTypeWhenMapping()
235
    {
236
        $this->expectException(\TypeError::class);
237
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
238
239
        (new Primitive('int', 1))->map(fn($i) => (string) $i);
240
    }
241
242
    public function testPad()
243
    {
244
        $a = new Primitive('int', 1, 2);
245
        $b = $a->pad(4, 0);
246
        $c = $a->pad(1, 0);
247
248
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
249
        $this->assertInstanceOf(Primitive::class, $b);
250
        $this->assertInstanceOf(Primitive::class, $c);
251
        $this->assertSame([1, 2, 0, 0], \iterator_to_array($b->iterator()));
252
        $this->assertSame([1, 2], \iterator_to_array($c->iterator()));
253
    }
254
255
    public function testPartition()
256
    {
257
        $sequence = new Primitive('int', 1, 2, 3, 4);
258
        $partition = $sequence->partition(fn($i) => $i % 2 === 0);
259
260
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
261
        $this->assertInstanceOf(Map::class, $partition);
262
        $this->assertTrue($partition->isOfType('bool', Sequence::class));
263
        $this->assertCount(2, $partition);
264
        $this->assertTrue($partition->get(true)->isOfType('int'));
265
        $this->assertTrue($partition->get(false)->isOfType('int'));
266
        $this->assertSame([2, 4], unwrap($partition->get(true)));
267
        $this->assertSame([1, 3], unwrap($partition->get(false)));
268
    }
269
270
    public function testSlice()
271
    {
272
        $a = new Primitive('int', 2, 3, 4, 5);
273
        $b = $a->slice(1, 3);
274
275
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($a->iterator()));
276
        $this->assertInstanceOf(Primitive::class, $b);
277
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
278
    }
279
280
    public function testSplitAt()
281
    {
282
        $sequence = new Primitive('int', 2, 3, 4, 5);
283
        $parts = $sequence->splitAt(2);
284
285
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($sequence->iterator()));
286
        $this->assertInstanceOf(Sequence::class, $parts);
287
        $this->assertTrue($parts->isOfType(Sequence::class));
288
        $this->assertCount(2, $parts);
289
        $this->assertTrue($parts->first()->isOfType('int'));
290
        $this->assertTrue($parts->last()->isOfType('int'));
291
        $this->assertSame([2, 3], unwrap($parts->first()));
292
        $this->assertSame([4, 5], unwrap($parts->last()));
293
    }
294
295
    public function testTake()
296
    {
297
        $a = new Primitive('int', 2, 3, 4);
298
        $b = $a->take(2);
299
300
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
301
        $this->assertInstanceOf(Primitive::class, $b);
302
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
303
    }
304
305
    public function testTakeEnd()
306
    {
307
        $a = new Primitive('int', 2, 3, 4);
308
        $b = $a->takeEnd(2);
309
310
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
311
        $this->assertInstanceOf(Primitive::class, $b);
312
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
313
    }
314
315
    public function testAppend()
316
    {
317
        $a = new Primitive('int', 1, 2);
318
        $b = new Primitive('int', 3, 4);
319
        $c = $a->append($b);
320
321
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
322
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
323
        $this->assertInstanceOf(Primitive::class, $c);
324
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($c->iterator()));
325
    }
326
327
    public function testIntersect()
328
    {
329
        $a = new Primitive('int', 1, 2);
330
        $b = new Primitive('int', 2, 3);
331
        $c = $a->intersect($b);
332
333
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
334
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
335
        $this->assertInstanceOf(Primitive::class, $c);
336
        $this->assertSame([2], \iterator_to_array($c->iterator()));
337
    }
338
339
    public function testAdd()
340
    {
341
        $a = new Primitive('int', 1);
342
        $b = ($a)(2);
343
344
        $this->assertSame([1], \iterator_to_array($a->iterator()));
345
        $this->assertInstanceOf(Primitive::class, $b);
346
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
347
    }
348
349
    public function testSort()
350
    {
351
        $a = new Primitive('int', 1, 4, 3, 2);
352
        $b = $a->sort(fn($a, $b) => $a > $b);
353
354
        $this->assertSame([1, 4, 3, 2], \iterator_to_array($a->iterator()));
355
        $this->assertInstanceOf(Primitive::class, $b);
356
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($b->iterator()));
357
    }
358
359
    public function testReduce()
360
    {
361
        $sequence = new Primitive('int', 1, 2, 3, 4);
362
363
        $this->assertSame(10, $sequence->reduce(0, fn($sum, $i) => $sum + $i));
364
    }
365
366
    public function testClear()
367
    {
368
        $a = new Primitive('int', 1, 2);
369
        $b = $a->clear();
370
371
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
372
        $this->assertInstanceOf(Primitive::class, $b);
373
        $this->assertSame([], \iterator_to_array($b->iterator()));
374
    }
375
376
    public function testReverse()
377
    {
378
        $a = new Primitive('int', 1, 2, 3, 4);
379
        $b = $a->reverse();
380
381
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
382
        $this->assertInstanceOf(Primitive::class, $b);
383
        $this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator()));
384
    }
385
386
    public function testEmpty()
387
    {
388
        $this->assertTrue((new Primitive('int'))->empty());
389
        $this->assertFalse((new Primitive('int', 1))->empty());
390
    }
391
392
    public function testToSequenceOf()
393
    {
394
        $sequence = new Primitive('int', 1, 2, 3);
395
        $sequence = $sequence->toSequenceOf('string|int', function($i) {
396
            yield (string) $i;
397
            yield $i;
398
        });
399
400
        $this->assertInstanceOf(Sequence::class, $sequence);
401
        $this->assertSame(
402
            ['1', 1, '2', 2, '3', 3],
403
            unwrap($sequence),
404
        );
405
    }
406
407
    public function testToSetOf()
408
    {
409
        $sequence = new Primitive('int', 1, 2, 3);
410
        $set = $sequence->toSetOf('string|int', function($i) {
411
            yield (string) $i;
412
            yield $i;
413
        });
414
415
        $this->assertInstanceOf(Set::class, $set);
416
        $this->assertSame(
417
            ['1', 1, '2', 2, '3', 3],
418
            unwrap($set),
419
        );
420
    }
421
422
    public function testToMapOf()
423
    {
424
        $sequence = new Primitive('int', 1, 2, 3);
425
        $map = $sequence->toMapOf('string', 'int', fn($i) => yield (string) $i => $i);
426
427
        $this->assertInstanceOf(Map::class, $map);
428
        $this->assertCount(3, $map);
429
        $this->assertSame(1, $map->get('1'));
430
        $this->assertSame(2, $map->get('2'));
431
        $this->assertSame(3, $map->get('3'));
432
    }
433
}
434