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.

EnumerableGeneration::emptyEnum()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * EnumerableGeneration trait of Enumerable class.
5
 * @author Alexander Prokhorov
6
 * @license Simplified BSD
7
 * @link https://github.com/Athari/YaLinqo YaLinqo on GitHub
8
 */
9
10
namespace YaLinqo;
11
12
/**
13
 * Trait of {@link Enumerable} containing generation methods.
14
 * @package YaLinqo
15
 */
16
trait EnumerableGeneration
17
{
18
    /**
19
     * Cycles through the source sequence.
20
     * <p><b>Syntax</b>: cycle (source)
21
     * <p>Source keys are discarded.
22
     * @param array|\Iterator|\IteratorAggregate|Enumerable $source Source sequence.
23
     * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable.
24
     * @throws \UnexpectedValueException If source contains no elements (checked during enumeration).
25
     * @return Enumerable Endless list of items repeating the source sequence.
26
     * @package YaLinqo\Generation
27
     */
28
    public static function cycle($source): Enumerable
29
    {
30
        $source = self::from($source);
31
32
        return new self(function() use ($source) {
33
            $isEmpty = true;
34
            while (true) {
35
                foreach ($source as $v) {
36
                    yield $v;
37
                    $isEmpty = false;
38
                }
39
                if ($isEmpty)
40
                    throw new \UnexpectedValueException(Errors::NO_ELEMENTS);
41
            }
42
        });
43
    }
44
45
    /**
46
     * Returns an empty sequence.
47
     * <p><b>Syntax</b>: emptyEnum ()
48
     * @return Enumerable
49
     * @package YaLinqo\Generation
50
     */
51
    public static function emptyEnum(): Enumerable
52
    {
53
        return new self(new \EmptyIterator, false);
54
    }
55
56
    /**
57
     * Converts source into Enumerable sequence.
58
     * <p><b>Syntax</b>: from (source)
59
     * <p>Result depends on the type of source:
60
     * <ul>
61
     * <li><b>array</b>: Enumerable from ArrayIterator;
62
     * <li><b>Enumerable</b>: Enumerable source itself;
63
     * <li><b>Iterator</b>: Enumerable from Iterator;
64
     * <li><b>IteratorAggregate</b>: Enumerable from Iterator returned from getIterator() method;
65
     * <li><b>Traversable</b>: Enumerable from the result of foreach over source.
66
     * </ul>
67
     * @param array|\Iterator|\IteratorAggregate|\Traversable|Enumerable $source Value to convert into Enumerable sequence.
68
     * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable.
69
     * @return Enumerable
70
     * @package YaLinqo\Generation
71
     */
72
    public static function from($source): Enumerable
73
    {
74
        $it = null;
75
        if ($source instanceof Enumerable)
76
            return $source;
77
        elseif (is_array($source))
78
            $it = new \ArrayIterator($source);
79
        elseif ($source instanceof \IteratorAggregate)
80
            $it = $source->getIterator();
81
        elseif ($source instanceof \Traversable)
82
            $it = $source;
83
        if ($it !== null) {
84
            return new self($it, false);
85
        }
86
        throw new \InvalidArgumentException('source must be array or Traversable.');
87
    }
88
89
    /**
90
     * Generates a sequence by mimicking a for loop.
91
     * <p><b>Syntax</b>: generate (funcValue {(v, k) ==> value} [, seedValue [, funcKey {(v, k) ==> key} [, seedKey]]])
92
     * <p>If seedValue is null, the first value will be the result of calling funcValue on seedValue and seedKey. The same applies for seedKey.
93
     * @param callable $funcValue {(v, k) ==> value} State update function to run on value after every iteration of the generator loop. Default: value.
94
     * @param mixed $seedValue Initial state of the generator loop for values. Default: null.
95
     * @param callable|null $funcKey {(v, k) ==> key} State update function to run on key after every iteration of the generator loop. Default: increment.
96
     * @param mixed $seedKey Initial state of the generator loop ofr keys. Default: 0.
97
     * @return Enumerable
98
     * @package YaLinqo\Generation
99
     */
100
    public static function generate($funcValue, $seedValue = null, $funcKey = null, $seedKey = null): Enumerable
101
    {
102
        $funcValue = Utils::createLambda($funcValue, 'v,k');
103
        $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...
104
105
        return new self(function() use ($funcValue, $funcKey, $seedValue, $seedKey) {
106
            $key = $seedKey === null ? ($funcKey ? $funcKey($seedValue, $seedKey) : 0) : $seedKey;
107
            $value = $seedValue === null ? $funcValue($seedValue, $seedKey) : $seedValue;
108
            yield $key => $value;
109
            while (true) {
110
                list($value, $key) = [
111
                    $funcValue($value, $key),
112
                    $funcKey ? $funcKey($value, $key) : $key + 1,
113
                ];
114
                yield $key => $value;
115
            }
116
        });
117
    }
118
119
    /**
120
     * Generates a sequence of integral numbers to infinity.
121
     * <p><b>Syntax</b>: toInfinity ([start [, step]])
122
     * @param int $start The first integer in the sequence. Default: 0.
123
     * @param int $step The difference between adjacent integers. Default: 1.
124
     * @return Enumerable
125
     * @package YaLinqo\Generation
126
     */
127
    public static function toInfinity(int $start = 0, int $step = 1): Enumerable
128
    {
129
        return new self(function() use ($start, $step) {
130
            $value = $start - $step;
131
            while (true)
132
                yield $value += $step;
133
        });
134
    }
135
136
    /**
137
     * 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.
138
     * <p><b>Syntax</b>: matches (subject, pattern [, flags])
139
     * @param string $subject The input string.
140
     * @param string $pattern The pattern to search for, as a string.
141
     * @param int $flags Can be a combination of the following flags: PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE. Default: PREG_SET_ORDER.
142
     * @return Enumerable
143
     * @see preg_match_all
144
     * @package YaLinqo\Generation
145
     */
146
    public static function matches(string $subject, string $pattern, int $flags = PREG_SET_ORDER): Enumerable
147
    {
148
        return new self(function() use ($subject, $pattern, $flags) {
149
            preg_match_all($pattern, $subject, $matches, $flags);
150
            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...
151
        });
152
    }
153
154
    /**
155
     * Generates a sequence of integral numbers to negative infinity.
156
     * <p><b>Syntax</b>: toNegativeInfinity ([start [, step]])
157
     * @param int $start The first integer in the sequence. Default: 0.
158
     * @param int $step The difference between adjacent integers. Default: 1.
159
     * @return Enumerable
160
     * @package YaLinqo\Generation
161
     */
162
    public static function toNegativeInfinity(int $start = 0, int $step = 1): Enumerable
163
    {
164
        return self::toInfinity($start, -$step);
165
    }
166
167
    /**
168
     * Returns a sequence that contains a single element with a specified value.
169
     * <p><b>Syntax</b>: returnEnum (element)
170
     * @param mixed $element The single element in the resulting sequence.
171
     * @return Enumerable Observable sequence containing the single specified element.
172
     * @package YaLinqo\Generation
173
     */
174
    public static function returnEnum($element): Enumerable
175
    {
176
        return self::repeat($element, 1);
177
    }
178
179
    /**
180
     * Generates a sequence of integral numbers, beginning with start and containing count elements.
181
     * <p><b>Syntax</b>: range (start, count [, step])
182
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
183
     * <p>Example: range(3, 4, 2) = 3, 5, 7, 9.
184
     * @param int $start The value of the first integer in the sequence.
185
     * @param int $count The number of integers to generate.
186
     * @param int $step The difference between adjacent integers. Default: 1.
187
     * @return Enumerable A sequence that contains a range of integral numbers.
188
     * @package YaLinqo\Generation
189
     */
190
    public static function range(int $start, int $count, int $step = 1): Enumerable
191
    {
192
        if ($count <= 0)
193
            return self::emptyEnum();
194
        return new self(function() use ($start, $count, $step) {
195
            $value = $start - $step;
196
            while ($count-- > 0)
197
                yield $value += $step;
198
        });
199
    }
200
201
    /**
202
     * Generates a reversed sequence of integral numbers, beginning with start and containing count elements.
203
     * <p><b>Syntax</b>: rangeDown (start, count [, step])
204
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
205
     * <p>Example: rangeDown(9, 4, 2) = 9, 7, 5, 3.
206
     * @param int $start The value of the first integer in the sequence.
207
     * @param int $count The number of integers to generate.
208
     * @param int $step The difference between adjacent integers. Default: 1.
209
     * @return Enumerable A sequence that contains a range of integral numbers.
210
     * @package YaLinqo\Generation
211
     */
212
    public static function rangeDown(int $start, int $count, int $step = 1): Enumerable
213
    {
214
        return self::range($start, $count, -$step);
215
    }
216
217
    /**
218
     * Generates a sequence of integral numbers within a specified range from start to end.
219
     * <p><b>Syntax</b>: rangeTo (start, end [, step])
220
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
221
     * <p>Example: rangeTo(3, 9, 2) = 3, 5, 7, 9.
222
     * @param int $start The value of the first integer in the sequence.
223
     * @param int $end The value of the last integer in the sequence (not included).
224
     * @param int $step The difference between adjacent integers. Default: 1.
225
     * @throws \InvalidArgumentException If step is not a positive number.
226
     * @return Enumerable A sequence that contains a range of integral numbers.
227
     * @package YaLinqo\Generation
228
     */
229
    public static function rangeTo(int $start, int $end, $step = 1): Enumerable
230
    {
231
        if ($step <= 0)
232
            throw new \InvalidArgumentException(Errors::STEP_NEGATIVE);
233
        return new self(function() use ($start, $end, $step) {
234
            if ($start <= $end) {
235
                for ($i = $start; $i < $end; $i += $step)
236
                    yield $i;
237
            }
238
            else {
239
                for ($i = $start; $i > $end; $i -= $step)
240
                    yield $i;
241
            }
242
        });
243
    }
244
245
    /**
246
     * Generates an sequence that contains one repeated value.
247
     * <p><b>Syntax</b>: repeat (element)
248
     * <p>Generates an endless sequence that contains one repeated value.
249
     * <p><b>Syntax</b>: repeat (element, count)
250
     * <p>Generates a sequence of specified length that contains one repeated value.
251
     * <p>Keys in the generated sequence are sequental: 0, 1, 2 etc.
252
     * @param int $element The value to be repeated.
253
     * @param int $count The number of times to repeat the value in the generated sequence. Default: null.
254
     * @throws \InvalidArgumentException If count is less than 0.
255
     * @return Enumerable A sequence that contains a repeated value.
256
     * @package YaLinqo\Generation
257
     */
258
    public static function repeat($element, $count = null): Enumerable
259
    {
260
        if ($count < 0)
261
            throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO);
262
        return new self(function() use ($element, $count) {
263
            for ($i = 0; $i < $count || $count === null; $i++)
264
                yield $element;
265
        });
266
    }
267
268
    /**
269
     * Split the given string by a regular expression.
270
     * <p><b>Syntax</b>: split (subject, pattern [, flags])
271
     * @param string $subject The input string.
272
     * @param string $pattern The pattern to search for, as a string.
273
     * @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.
274
     * @return Enumerable
275
     * @see preg_split
276
     * @package YaLinqo\Generation
277
     */
278
    public static function split(string $subject, string $pattern, int $flags = 0): Enumerable
279
    {
280
        return new self(
281
            new \ArrayIterator(preg_split($pattern, $subject, -1, $flags)),
282
            false
283
        );
284
    }
285
}