Passed
Push — master ( c27fc8...18e353 )
by Smoren
02:07
created

reduce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smoren\Sequence\Functions;
4
5
use Smoren\Sequence\Structs\IndexedArray;
6
use Smoren\Sequence\Structs\Range;
7
8
/**
9
 * Creates iterable range.
10
 *
11
 * @param int $start start value
12
 * @param int<0, max>|null $size size of elements
13
 * @param int $step range step
14
 *
15
 * @return Range<int> iterable range
16
 */
17
function xrange(int $start, ?int $size = null, int $step = 1): Range
18
{
19 9
    if($size === null) {
20 2
        [$start, $size] = [0, $start];
21
    }
22
23 9
    return new Range($start, $size, $step);
0 ignored issues
show
Bug introduced by
$start of type integer is incompatible with the type Smoren\Sequence\Structs\T expected by parameter $start of Smoren\Sequence\Structs\Range::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
    return new Range(/** @scrutinizer ignore-type */ $start, $size, $step);
Loading history...
Bug introduced by
$step of type integer is incompatible with the type Smoren\Sequence\Structs\T expected by parameter $step of Smoren\Sequence\Structs\Range::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
    return new Range($start, $size, /** @scrutinizer ignore-type */ $step);
Loading history...
24
}
25
26
/**
27
 * Maps iterable collection and returns IndexedArray of mapped values.
28
 *
29
 * @template TInput
30
 * @template TOutput
31
 *
32
 * @param iterable<TInput> $collection
33
 * @param callable(TInput $item): TOutput $mapper
34
 *
35
 * @return IndexedArray<TOutput>
36
 */
37
function map(iterable $collection, callable $mapper): IndexedArray
38
{
39 5
    $result = new IndexedArray();
40
41 5
    foreach($collection as $item) {
42 4
        $result[] = $mapper($item);
43
    }
44
45 5
    return $result;
46
}
47
48
/**
49
 * Filters iterable collection and returns IndexedArray of filtered items.
50
 *
51
 * @template T
52
 *
53
 * @param iterable<T> $collection
54
 * @param callable(T $item): T $filter
55
 *
56
 * @return IndexedArray<T>
57
 */
58
function filter(iterable $collection, callable $filter): IndexedArray
59
{
60 6
    $result = new IndexedArray();
61
62 6
    foreach($collection as $item) {
63 5
        if($filter($item)) {
64 4
            $result[] = $item;
65
        }
66
    }
67
68 6
    return $result;
69
}
70
71
/**
72
 * Reduces iterable collection.
73
 *
74
 * @template TInput
75
 * @template TOutput
76
 *
77
 * @param iterable<TInput> $collection
78
 * @param callable(TOutput|null $carry, TInput $item): TOutput $reducer
79
 * @param TOutput|null $initialValue
0 ignored issues
show
Bug introduced by
The type Smoren\Sequence\Functions\TOutput 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...
80
 *
81
 * @return TOutput|null
82
 */
83
function reduce(iterable $collection, callable $reducer, $initialValue = null)
84
{
85 6
    $carry = $initialValue;
86
87 6
    foreach($collection as $item) {
88 4
        $carry = $reducer($carry, $item);
89
    }
90
91 6
    return $carry;
92
}
93