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.

Iterator::product()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Gielfeldt\Iterators;
4
5
class Iterator
6
{
7
    public static function instanceOf($iterator, $class)
8
    {
9
        if ($iterator instanceof $class) {
10
            return true;
11
        }
12
        if ($iterator instanceof \OuterIterator) {
13
            return self::instanceOf($iterator->getInnerIterator(), $class);
14
        }
15
        return false;
16
    }
17
18
    public static function getInnerIterators(\Traversable $iterator, $include_self = false)
19
    {
20
        $result = $include_self ? [$iterator] : [];
21
        if ($iterator instanceof \OuterIterator) {
22
            return array_merge($result, self::getInnerIterators($iterator->getInnerIterator(), true));
23
        }
24
        return $result;
25
    }
26
27
    public static function getInnermostIterator(\Traversable $iterator)
28
    {
29
        $iterators = self::getInnerIterators($iterator, true);
30
        return $iterators ? end($iterators) : false;
31
    }
32
33
    public static function nth(\Traversable $iterator, int $offset)
34
    {
35
        $iterator = new \IteratorIterator($iterator);
36
        $iterator->rewind();
37
        while ($iterator->valid() && $offset-- > 0) {
38
            $iterator->next();
39
        }
40
        return $iterator->current();
41
    }
42
43
    /**
44
     * @return string
45
     */
46 View Code Duplication
    public static function reduce(\Traversable $iterator, callable $callback, $initial = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $callback = \Closure::fromCallable($callback);
49
        iterator_apply(
50
            $iterator,
51
            function ($iterator) use (&$initial, $callback) {
52
                $initial = $callback($initial, $iterator->current(), $iterator->key());
53
                return true;
54
            },
55
            [$iterator]
56
        );
57
        return $initial;
58
    }
59
60
    public static function sum(\Traversable $iterator)
61
    {
62
        return self::reduce(
63
            $iterator,
64
            function ($carry, $value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
                return $carry + $value;
66
            },
67
            0
68
        );
69
    }
70
71
    public static function product(\Traversable $iterator)
72
    {
73
        return self::reduce(
74
            $iterator,
75
            function ($carry, $value) {
76
                return $carry * $value;
77
            },
78
            1
79
        );
80
    }
81
82
    public static function average(\Traversable $iterator)
83
    {
84
        return self::sum($iterator) / count(new CountableIterator($iterator));
85
    }
86
87
    public static function min(\Traversable $iterator)
88
    {
89
        return self::reduce(
90
            $iterator,
91
            function ($carry, $value) {
92
                return $carry < $value ? $carry : $value;
93
            },
94
            INF
95
        );
96
    }
97
98
    public static function max(\Traversable $iterator)
99
    {
100
        return self::reduce(
101
            $iterator,
102
            function ($carry, $value) {
103
                return $carry > $value ? $carry : $value;
104
            },
105
            -INF
106
        );
107
    }
108
109
    public static function concatenate(\Traversable $iterator)
110
    {
111
        return self::reduce(
112
            $iterator,
113
            function ($carry, $value) {
114
                return $carry . $value;
115
            },
116
            ''
117
        );
118
    }
119
120
    public static function implode($separator, \Traversable $iterator)
121
    {
122
        $result = self::reduce(
123
            $iterator,
124 1
            function ($carry, $value) use ($separator) {
125 1
                return $carry . $value . $separator;
126 1
            },
127 1
            ''
128
        );
129 1
        $result = mb_substr($result, 0, -mb_strlen($separator));
130 1
        return $result;
131
    }
132
133 1
    public static function iterator_to_array_deep(\Traversable $iterator, $use_keys = true)
134
    {
135 1
        $result = [];
136 1
        foreach ($iterator as $key => $value) {
137 1
            $value = $value instanceof \Traversable ? self::iterator_to_array_deep($value, $use_keys) : $value;
138 1
            if ($use_keys) {
139 1
                $result[$key] = $value;
140
            } else {
141 1
                $result[] = $value;
142
            }
143
        }
144 1
        return $result;
145
    }
146
}
147