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.
Completed
Push — master ( 23c92d...0cfe1b )
by Alexander
01:42
created

EnumerableGeneration::fromTraversable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 1
b 0
f 1
1
<?php
2
3
namespace YaLinqo;
4
5
use YaLinqo;
6
7
trait EnumerableGeneration
8
{
9
    /**
10
     * Cycles through the source sequence.
11
     * <p><b>Syntax</b>: cycle (source)
12
     * <p>Source keys are discarded.
13
     * @param array|\Iterator|\IteratorAggregate|Enumerable $source Source sequence.
14
     * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable.
15
     * @throws \UnexpectedValueException If source contains no elements (checked during enumeration).
16
     * @return Enumerable Endless list of items repeating the source sequence.
17
     * @package YaLinqo\Generation
18
     */
19
    public static function cycle ($source)
20
    {
21
        $source = self::from($source);
22
23
        return new self(function () use ($source) {
24
            $isEmpty = true;
25
            while (true) {
26
                foreach ($source as $v) {
27
                    yield $v;
28
                    $isEmpty = false;
29
                }
30
                if ($isEmpty)
31
                    throw new \UnexpectedValueException(Errors::NO_ELEMENTS);
32
            }
33
        });
34
    }
35
36
    /**
37
     * Returns an empty sequence.
38
     * <p><b>Syntax</b>: emptyEnum ()
39
     * @return Enumerable
40
     * @package YaLinqo\Generation
41
     */
42
    public static function emptyEnum ()
43
    {
44
        return new self(new \EmptyIterator, false);
45
    }
46
47
    /**
48
     * Converts source into Enumerable sequence.
49
     * <p><b>Syntax</b>: from (source)
50
     * <p>Result depends on the type of source:
51
     * <ul>
52
     * <li><b>array</b>: Enumerable from ArrayIterator;
53
     * <li><b>Enumerable</b>: Enumerable source itself;
54
     * <li><b>Iterator</b>: Enumerable from Iterator;
55
     * <li><b>IteratorAggregate</b>: Enumerable from Iterator returned from getIterator() method;
56
     * <li><b>Traversable</b>: Enumerable from the result of foreach over source.
57
     * </ul>
58
     * @param array|\Iterator|\IteratorAggregate|\Traversable|Enumerable $source Value to convert into Enumerable sequence.
59
     * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable.
60
     * @return Enumerable
61
     * @package YaLinqo\Generation
62
     */
63
    public static function from ($source)
64
    {
65
        $it = null;
66
        if ($source instanceof Enumerable)
67
            return $source;
68
        else if (is_array($source))
69
            $it = new \ArrayIterator($source);
70
        elseif ($source instanceof \IteratorAggregate)
71
            $it = $source->getIterator();
72
        elseif ($source instanceof \Traversable)
73
            $it = $source;
74
        if ($it !== null) {
75
            return new self($it, false);
76
        }
77
        throw new \InvalidArgumentException('source must be array or Traversable.');
78
    }
79
80
    /**
81
     * Generates a sequence by mimicking a for loop.
82
     * <p><b>Syntax</b>: generate (funcValue {(v, k) ==> value} [, seedValue [, funcKey {(v, k) ==> key} [, seedKey]]])
83
     * <p>If seedValue is null, the first value will be the result of calling funcValue on seedValue and seedKey. The same applies for seedKey.
84
     * @param callable $funcValue {(v, k) ==> value} State update function to run on value after every iteration of the generator loop. Default: value.
85
     * @param mixed $seedValue Initial state of the generator loop for values. Default: null.
86
     * @param callable|null $funcKey {(v, k) ==> key} State update function to run on key after every iteration of the generator loop. Default: increment.
87
     * @param mixed $seedKey Initial state of the generator loop ofr keys. Default: 0.
88
     * @return Enumerable
89
     * @package YaLinqo\Generation
90
     */
91
    public static function generate ($funcValue, $seedValue = null, $funcKey = null, $seedKey = null)
92
    {
93
        $funcValue = Utils::createLambda($funcValue, 'v,k');
94
        $funcKey = Utils::createLambda($funcKey, 'v,k', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a callable|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
        return new self(function () use ($funcValue, $funcKey, $seedValue, $seedKey) {
97
            $key = $seedKey === null ? ($funcKey ? $funcKey($seedValue, $seedKey) : 0) : $seedKey;
98
            $value = $seedValue === null ? $funcValue($seedValue, $seedKey) : $seedValue;
99
            yield $key => $value;
100
            while (true) {
101
                list($value, $key) = [
102
                    $funcValue($value, $key),
103
                    $funcKey ? $funcKey($value, $key) : $key + 1,
104
                ];
105
                yield $key => $value;
106
            }
107
        });
108
    }
109
110
    /**
111
     * Generates a sequence of integral numbers to infinity.
112
     * <p><b>Syntax</b>: toInfinity ([start [, step]])
113
     * @param int $start The first integer in the sequence. Default: 0.
114
     * @param int $step The difference between adjacent integers. Default: 1.
115
     * @return Enumerable
116
     * @package YaLinqo\Generation
117
     */
118
    public static function toInfinity ($start = 0, $step = 1)
119
    {
120
        return new self(function () use ($start, $step) {
121
            $value = $start - $step;
122
            while (true)
123
                yield $value += $step;
124
        });
125
    }
126
127
    /**
128
     * Searches subject for all matches to the regular expression given in pattern and enumerates them in the order specified by flags. After the first match is found, the subsequent searches are continued on from end of the last match.
129
     * <p><b>Syntax</b>: matches (subject, pattern [, flags])
130
     * @param string $subject The input string.
131
     * @param string $pattern The pattern to search for, as a string.
132
     * @param int $flags Can be a combination of the following flags: PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE. Default: PREG_SET_ORDER.
133
     * @return Enumerable
134
     * @see preg_match_all
135
     * @package YaLinqo\Generation
136
     */
137
    public static function matches ($subject, $pattern, $flags = PREG_SET_ORDER)
138
    {
139
        return new self(function () use ($subject, $pattern, $flags) {
140
            preg_match_all($pattern, $subject, $matches, $flags);
141
            return $matches !== false ? self::from($matches)->getIterator() : self::emptyEnum();
0 ignored issues
show
Bug introduced by
It seems like $matches can also be of type null; however, YaLinqo\EnumerableGeneration::from() does only seem to accept array|object<Traversable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
142
        });
143
    }
144
145
    /**
146
     * Generates a sequence of integral numbers to negative infinity.
147
     * <p><b>Syntax</b>: toNegativeInfinity ([start [, step]])
148
     * @param int $start The first integer in the sequence. Default: 0.
149
     * @param int $step The difference between adjacent integers. Default: 1.
150
     * @return Enumerable
151
     * @package YaLinqo\Generation
152
     */
153
    public static function toNegativeInfinity ($start = 0, $step = 1)
154
    {
155
        return self::toInfinity($start, -$step);
156
    }
157
158
    /**
159
     * Returns a sequence that contains a single element with a specified value.
160
     * <p><b>Syntax</b>: returnEnum (element)
161
     * @param mixed $element The single element in the resulting sequence.
162
     * @return Enumerable Observable sequence containing the single specified element.
163
     * @package YaLinqo\Generation
164
     */
165
    public static function returnEnum ($element)
166
    {
167
        return self::repeat($element, 1);
168
    }
169
170
    /**
171
     * Generates a sequence of integral numbers, beginning with start and containing count elements.
172
     * <p><b>Syntax</b>: range (start, count [, step])
173
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
174
     * <p>Example: range(3, 4, 2) = 3, 5, 7, 9.
175
     * @param int $start The value of the first integer in the sequence.
176
     * @param int $count The number of integers to generate.
177
     * @param int $step The difference between adjacent integers. Default: 1.
178
     * @return Enumerable A sequence that contains a range of integral numbers.
179
     * @package YaLinqo\Generation
180
     */
181
    public static function range ($start, $count, $step = 1)
182
    {
183
        if ($count <= 0)
184
            return self::emptyEnum();
185
        return new self(function () use ($start, $count, $step) {
186
            $value = $start - $step;
187
            while ($count-- > 0)
188
                yield $value += $step;
189
        });
190
    }
191
192
    /**
193
     * Generates a reversed sequence of integral numbers, beginning with start and containing count elements.
194
     * <p><b>Syntax</b>: rangeDown (start, count [, step])
195
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
196
     * <p>Example: rangeDown(9, 4, 2) = 9, 7, 5, 3.
197
     * @param int $start The value of the first integer in the sequence.
198
     * @param int $count The number of integers to generate.
199
     * @param int $step The difference between adjacent integers. Default: 1.
200
     * @return Enumerable A sequence that contains a range of integral numbers.
201
     * @package YaLinqo\Generation
202
     */
203
    public static function rangeDown ($start, $count, $step = 1)
204
    {
205
        return self::range($start, $count, -$step);
206
    }
207
208
    /**
209
     * Generates a sequence of integral numbers within a specified range from start to end.
210
     * <p><b>Syntax</b>: rangeTo (start, end [, step])
211
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
212
     * <p>Example: rangeTo(3, 9, 2) = 3, 5, 7, 9.
213
     * @param int $start The value of the first integer in the sequence.
214
     * @param int $end The value of the last integer in the sequence (not included).
215
     * @param int $step The difference between adjacent integers. Default: 1.
216
     * @throws \InvalidArgumentException If step is not a positive number.
217
     * @return Enumerable A sequence that contains a range of integral numbers.
218
     * @package YaLinqo\Generation
219
     */
220
    public static function rangeTo ($start, $end, $step = 1)
221
    {
222
        if ($step <= 0)
223
            throw new \InvalidArgumentException(Errors::STEP_NEGATIVE);
224
        return new self(function () use ($start, $end, $step) {
225
            if ($start <= $end) {
226
                for ($i = $start; $i < $end; $i += $step)
227
                    yield $i;
228
            }
229
            else {
230
                for ($i = $start; $i > $end; $i -= $step)
231
                    yield $i;
232
            }
233
        });
234
    }
235
236
    /**
237
     * Generates an sequence that contains one repeated value.
238
     * <p><b>Syntax</b>: repeat (element)
239
     * <p>Generates an endless sequence that contains one repeated value.
240
     * <p><b>Syntax</b>: repeat (element, count)
241
     * <p>Generates a sequence of specified length that contains one repeated value.
242
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
243
     * @param int $element The value to be repeated.
244
     * @param int $count The number of times to repeat the value in the generated sequence. Default: null.
245
     * @throws \InvalidArgumentException If count is less than 0.
246
     * @return Enumerable A sequence that contains a repeated value.
247
     * @package YaLinqo\Generation
248
     */
249
    public static function repeat ($element, $count = null)
250
    {
251
        if ($count < 0)
252
            throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO);
253
        return new self(function () use ($element, $count) {
254
            for ($i = 0; $i < $count || $count === null; $i++)
255
                yield $element;
256
        });
257
    }
258
259
    /**
260
     * Split the given string by a regular expression.
261
     * <p><b>Syntax</b>: split (subject, pattern [, flags])
262
     * @param string $subject The input string.
263
     * @param string $pattern The pattern to search for, as a string.
264
     * @param int $flags flags can be any combination of the following flags: PREG_SPLIT_NO_EMPTY, PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE. Default: 0.
265
     * @return Enumerable
266
     * @see preg_split
267
     * @package YaLinqo\Generation
268
     */
269
    public static function split ($subject, $pattern, $flags = 0)
270
    {
271
        return new self(
272
            new \ArrayIterator(preg_split($pattern, $subject, -1, $flags)),
273
            false
274
        );
275
    }
276
}