@@ -23,14 +23,16 @@ discard block |
||
23 | 23 | $it = $this->getIterator(); |
24 | 24 | |
25 | 25 | if ($it instanceof \ArrayAccess) { |
26 | - if (!$it->offsetExists($key)) |
|
27 | - throw new \UnexpectedValueException(Errors::NO_KEY); |
|
26 | + if (!$it->offsetExists($key)) { |
|
27 | + throw new \UnexpectedValueException(Errors::NO_KEY); |
|
28 | + } |
|
28 | 29 | return $it->offsetGet($key); |
29 | 30 | } |
30 | 31 | |
31 | 32 | foreach ($it as $k => $v) { |
32 | - if ($k === $key) |
|
33 | - return $v; |
|
33 | + if ($k === $key) { |
|
34 | + return $v; |
|
35 | + } |
|
34 | 36 | } |
35 | 37 | throw new \UnexpectedValueException(Errors::NO_KEY); |
36 | 38 | } |
@@ -49,12 +51,14 @@ discard block |
||
49 | 51 | /** @var $it \Iterator|\ArrayAccess */ |
50 | 52 | $it = $this->getIterator(); |
51 | 53 | |
52 | - if ($it instanceof \ArrayAccess) |
|
53 | - return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
|
54 | + if ($it instanceof \ArrayAccess) { |
|
55 | + return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
|
56 | + } |
|
54 | 57 | |
55 | 58 | foreach ($it as $k => $v) { |
56 | - if ($k === $key) |
|
57 | - return $v; |
|
59 | + if ($k === $key) { |
|
60 | + return $v; |
|
61 | + } |
|
58 | 62 | } |
59 | 63 | return $default; |
60 | 64 | } |
@@ -77,8 +81,9 @@ discard block |
||
77 | 81 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
78 | 82 | |
79 | 83 | foreach ($this as $k => $v) { |
80 | - if ($predicate($v, $k)) |
|
81 | - return $v; |
|
84 | + if ($predicate($v, $k)) { |
|
85 | + return $v; |
|
86 | + } |
|
82 | 87 | } |
83 | 88 | throw new \UnexpectedValueException(Errors::NO_MATCHES); |
84 | 89 | } |
@@ -100,8 +105,9 @@ discard block |
||
100 | 105 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
101 | 106 | |
102 | 107 | foreach ($this as $k => $v) { |
103 | - if ($predicate($v, $k)) |
|
104 | - return $v; |
|
108 | + if ($predicate($v, $k)) { |
|
109 | + return $v; |
|
110 | + } |
|
105 | 111 | } |
106 | 112 | return $default; |
107 | 113 | } |
@@ -123,8 +129,9 @@ discard block |
||
123 | 129 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
124 | 130 | |
125 | 131 | foreach ($this as $k => $v) { |
126 | - if ($predicate($v, $k)) |
|
127 | - return $v; |
|
132 | + if ($predicate($v, $k)) { |
|
133 | + return $v; |
|
134 | + } |
|
128 | 135 | } |
129 | 136 | return $fallback(); |
130 | 137 | } |
@@ -154,8 +161,9 @@ discard block |
||
154 | 161 | $value = $v; |
155 | 162 | } |
156 | 163 | } |
157 | - if (!$found) |
|
158 | - throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
164 | + if (!$found) { |
|
165 | + throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
166 | + } |
|
159 | 167 | return $value; |
160 | 168 | } |
161 | 169 | |
@@ -234,14 +242,16 @@ discard block |
||
234 | 242 | $value = null; |
235 | 243 | foreach ($this as $k => $v) { |
236 | 244 | if ($predicate($v, $k)) { |
237 | - if ($found) |
|
238 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
245 | + if ($found) { |
|
246 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
247 | + } |
|
239 | 248 | $found = true; |
240 | 249 | $value = $v; |
241 | 250 | } |
242 | 251 | } |
243 | - if (!$found) |
|
244 | - throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
252 | + if (!$found) { |
|
253 | + throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
254 | + } |
|
245 | 255 | return $value; |
246 | 256 | } |
247 | 257 | |
@@ -266,8 +276,9 @@ discard block |
||
266 | 276 | $value = null; |
267 | 277 | foreach ($this as $k => $v) { |
268 | 278 | if ($predicate($v, $k)) { |
269 | - if ($found) |
|
270 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
279 | + if ($found) { |
|
280 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
281 | + } |
|
271 | 282 | $found = true; |
272 | 283 | $value = $v; |
273 | 284 | } |
@@ -296,8 +307,9 @@ discard block |
||
296 | 307 | $value = null; |
297 | 308 | foreach ($this as $k => $v) { |
298 | 309 | if ($predicate($v, $k)) { |
299 | - if ($found) |
|
300 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
310 | + if ($found) { |
|
311 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
312 | + } |
|
301 | 313 | $found = true; |
302 | 314 | $value = $v; |
303 | 315 | } |
@@ -316,8 +328,9 @@ discard block |
||
316 | 328 | public function indexOf ($value) |
317 | 329 | { |
318 | 330 | foreach ($this as $k => $v) { |
319 | - if ($v === $value) |
|
320 | - return $k; |
|
331 | + if ($v === $value) { |
|
332 | + return $k; |
|
333 | + } |
|
321 | 334 | } |
322 | 335 | return null; // not -1 |
323 | 336 | } |
@@ -334,8 +347,9 @@ discard block |
||
334 | 347 | { |
335 | 348 | $key = null; |
336 | 349 | foreach ($this as $k => $v) { |
337 | - if ($v === $value) |
|
338 | - $key = $k; |
|
350 | + if ($v === $value) { |
|
351 | + $key = $k; |
|
352 | + } |
|
339 | 353 | } |
340 | 354 | return $key; // not -1 |
341 | 355 | } |
@@ -353,8 +367,9 @@ discard block |
||
353 | 367 | $predicate = Utils::createLambda($predicate, 'v,k'); |
354 | 368 | |
355 | 369 | foreach ($this as $k => $v) { |
356 | - if ($predicate($v, $k)) |
|
357 | - return $k; |
|
370 | + if ($predicate($v, $k)) { |
|
371 | + return $k; |
|
372 | + } |
|
358 | 373 | } |
359 | 374 | return null; // not -1 |
360 | 375 | } |
@@ -373,8 +388,9 @@ discard block |
||
373 | 388 | |
374 | 389 | $key = null; |
375 | 390 | foreach ($this as $k => $v) { |
376 | - if ($predicate($v, $k)) |
|
377 | - $key = $k; |
|
391 | + if ($predicate($v, $k)) { |
|
392 | + $key = $k; |
|
393 | + } |
|
378 | 394 | } |
379 | 395 | return $key; // not -1 |
380 | 396 | } |
@@ -393,8 +409,9 @@ discard block |
||
393 | 409 | return new self(function () use ($count) { |
394 | 410 | $it = $this->getIterator(); |
395 | 411 | $it->rewind(); |
396 | - for ($i = 0; $i < $count && $it->valid(); ++$i) |
|
397 | - $it->next(); |
|
412 | + for ($i = 0; $i < $count && $it->valid(); ++$i) { |
|
413 | + $it->next(); |
|
414 | + } |
|
398 | 415 | while ($it->valid()) { |
399 | 416 | yield $it->key() => $it->current(); |
400 | 417 | $it->next(); |
@@ -418,10 +435,12 @@ discard block |
||
418 | 435 | return new self(function () use ($predicate) { |
419 | 436 | $yielding = false; |
420 | 437 | foreach ($this as $k => $v) { |
421 | - if (!$yielding && !$predicate($v, $k)) |
|
422 | - $yielding = true; |
|
423 | - if ($yielding) |
|
424 | - yield $k => $v; |
|
438 | + if (!$yielding && !$predicate($v, $k)) { |
|
439 | + $yielding = true; |
|
440 | + } |
|
441 | + if ($yielding) { |
|
442 | + yield $k => $v; |
|
443 | + } |
|
425 | 444 | } |
426 | 445 | }); |
427 | 446 | } |
@@ -437,14 +456,16 @@ discard block |
||
437 | 456 | */ |
438 | 457 | public function take ($count) |
439 | 458 | { |
440 | - if ($count <= 0) |
|
441 | - return new self(new \EmptyIterator, false); |
|
459 | + if ($count <= 0) { |
|
460 | + return new self(new \EmptyIterator, false); |
|
461 | + } |
|
442 | 462 | |
443 | 463 | return new self(function () use ($count) { |
444 | 464 | foreach ($this as $k => $v) { |
445 | 465 | yield $k => $v; |
446 | - if (--$count == 0) |
|
447 | - break; |
|
466 | + if (--$count == 0) { |
|
467 | + break; |
|
468 | + } |
|
448 | 469 | } |
449 | 470 | }); |
450 | 471 | } |
@@ -464,8 +485,9 @@ discard block |
||
464 | 485 | |
465 | 486 | return new self(function () use ($predicate) { |
466 | 487 | foreach ($this as $k => $v) { |
467 | - if (!$predicate($v, $k)) |
|
468 | - break; |
|
488 | + if (!$predicate($v, $k)) { |
|
489 | + break; |
|
490 | + } |
|
469 | 491 | yield $k => $v; |
470 | 492 | } |
471 | 493 | }); |
@@ -27,8 +27,9 @@ discard block |
||
27 | 27 | yield $v; |
28 | 28 | $isEmpty = false; |
29 | 29 | } |
30 | - if ($isEmpty) |
|
31 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
30 | + if ($isEmpty) { |
|
31 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
32 | + } |
|
32 | 33 | } |
33 | 34 | }); |
34 | 35 | } |
@@ -63,14 +64,15 @@ discard block |
||
63 | 64 | public static function from ($source) |
64 | 65 | { |
65 | 66 | $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; |
|
67 | + if ($source instanceof Enumerable) { |
|
68 | + return $source; |
|
69 | + } else if (is_array($source)) { |
|
70 | + $it = new \ArrayIterator($source); |
|
71 | + } elseif ($source instanceof \IteratorAggregate) { |
|
72 | + $it = $source->getIterator(); |
|
73 | + } elseif ($source instanceof \Traversable) { |
|
74 | + $it = $source; |
|
75 | + } |
|
74 | 76 | if ($it !== null) { |
75 | 77 | return new self($it, false); |
76 | 78 | } |
@@ -119,8 +121,9 @@ discard block |
||
119 | 121 | { |
120 | 122 | return new self(function () use ($start, $step) { |
121 | 123 | $value = $start - $step; |
122 | - while (true) |
|
123 | - yield $value += $step; |
|
124 | + while (true) { |
|
125 | + yield $value += $step; |
|
126 | + } |
|
124 | 127 | }); |
125 | 128 | } |
126 | 129 | |
@@ -180,12 +183,14 @@ discard block |
||
180 | 183 | */ |
181 | 184 | public static function range ($start, $count, $step = 1) |
182 | 185 | { |
183 | - if ($count <= 0) |
|
184 | - return self::emptyEnum(); |
|
186 | + if ($count <= 0) { |
|
187 | + return self::emptyEnum(); |
|
188 | + } |
|
185 | 189 | return new self(function () use ($start, $count, $step) { |
186 | 190 | $value = $start - $step; |
187 | - while ($count-- > 0) |
|
188 | - yield $value += $step; |
|
191 | + while ($count-- > 0) { |
|
192 | + yield $value += $step; |
|
193 | + } |
|
189 | 194 | }); |
190 | 195 | } |
191 | 196 | |
@@ -219,16 +224,18 @@ discard block |
||
219 | 224 | */ |
220 | 225 | public static function rangeTo ($start, $end, $step = 1) |
221 | 226 | { |
222 | - if ($step <= 0) |
|
223 | - throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
|
227 | + if ($step <= 0) { |
|
228 | + throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
|
229 | + } |
|
224 | 230 | return new self(function () use ($start, $end, $step) { |
225 | 231 | 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 | + for ($i = $start; $i < $end; $i += $step) { |
|
233 | + yield $i; |
|
234 | + } |
|
235 | + } else { |
|
236 | + for ($i = $start; $i > $end; $i -= $step) { |
|
237 | + yield $i; |
|
238 | + } |
|
232 | 239 | } |
233 | 240 | }); |
234 | 241 | } |
@@ -248,11 +255,13 @@ discard block |
||
248 | 255 | */ |
249 | 256 | public static function repeat ($element, $count = null) |
250 | 257 | { |
251 | - if ($count < 0) |
|
252 | - throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
|
258 | + if ($count < 0) { |
|
259 | + throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
|
260 | + } |
|
253 | 261 | return new self(function () use ($element, $count) { |
254 | - for ($i = 0; $i < $count || $count === null; $i++) |
|
255 | - yield $element; |
|
262 | + for ($i = 0; $i < $count || $count === null; $i++) { |
|
263 | + yield $element; |
|
264 | + } |
|
256 | 265 | }); |
257 | 266 | } |
258 | 267 |
@@ -58,16 +58,20 @@ discard block |
||
58 | 58 | public static function createLambda ($closure, $closureArgs, $default = null) |
59 | 59 | { |
60 | 60 | if ($closure === null) { |
61 | - if ($default === null) |
|
62 | - throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
61 | + if ($default === null) { |
|
62 | + throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
63 | + } |
|
63 | 64 | return $default; |
64 | 65 | } |
65 | - if ($closure instanceof \Closure) |
|
66 | - return $closure; |
|
67 | - if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) |
|
68 | - return $function; |
|
69 | - if (is_callable($closure)) |
|
70 | - return $closure; |
|
66 | + if ($closure instanceof \Closure) { |
|
67 | + return $closure; |
|
68 | + } |
|
69 | + if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) { |
|
70 | + return $function; |
|
71 | + } |
|
72 | + if (is_callable($closure)) { |
|
73 | + return $closure; |
|
74 | + } |
|
71 | 75 | throw new \InvalidArgumentException(self::ERROR_CLOSURE_NOT_CALLABLE); |
72 | 76 | } |
73 | 77 | |
@@ -85,8 +89,7 @@ discard block |
||
85 | 89 | if ($closure === null) { |
86 | 90 | $isReversed = false; |
87 | 91 | return $sortOrder === SORT_DESC ? Functions::$compareStrictReversed : Functions::$compareStrict; |
88 | - } |
|
89 | - elseif (is_int($closure)) { |
|
92 | + } elseif (is_int($closure)) { |
|
90 | 93 | switch ($closure) { |
91 | 94 | case SORT_REGULAR: |
92 | 95 | return Functions::$compareStrict; |
@@ -118,14 +121,16 @@ discard block |
||
118 | 121 | */ |
119 | 122 | public static function lambdaToSortFlagsAndOrder ($closure, &$sortOrder) |
120 | 123 | { |
121 | - if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) |
|
122 | - $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
123 | - if (is_int($closure)) |
|
124 | - return $closure; |
|
125 | - elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) |
|
126 | - return self::$compareFunctionToSortFlags[$closure]; |
|
127 | - else |
|
128 | - return null; |
|
124 | + if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) { |
|
125 | + $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
126 | + } |
|
127 | + if (is_int($closure)) { |
|
128 | + return $closure; |
|
129 | + } elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) { |
|
130 | + return self::$compareFunctionToSortFlags[$closure]; |
|
131 | + } else { |
|
132 | + return null; |
|
133 | + } |
|
129 | 134 | } |
130 | 135 | |
131 | 136 | /** |
@@ -139,24 +144,26 @@ discard block |
||
139 | 144 | { |
140 | 145 | $posDollar = strpos($closure, '$'); |
141 | 146 | if ($posDollar !== false) { |
142 | - if (isset(self::$lambdaCache[$closure][$closureArgs])) |
|
143 | - return self::$lambdaCache[$closure][$closureArgs]; |
|
147 | + if (isset(self::$lambdaCache[$closure][$closureArgs])) { |
|
148 | + return self::$lambdaCache[$closure][$closureArgs]; |
|
149 | + } |
|
144 | 150 | $posArrow = strpos($closure, '==>', $posDollar); |
145 | 151 | if ($posArrow !== false) { |
146 | 152 | $args = trim(substr($closure, 0, $posArrow), "() \r\n\t"); |
147 | 153 | $code = substr($closure, $posArrow + 3); |
148 | - } |
|
149 | - else { |
|
154 | + } else { |
|
150 | 155 | $args = '$' . str_replace(',', '=null,$', $closureArgs) . '=null'; |
151 | 156 | $code = $closure; |
152 | 157 | } |
153 | 158 | $code = trim($code, " \r\n\t"); |
154 | - if (strlen($code) > 0 && $code[0] != '{') |
|
155 | - $code = "return {$code};"; |
|
159 | + if (strlen($code) > 0 && $code[0] != '{') { |
|
160 | + $code = "return {$code};"; |
|
161 | + } |
|
156 | 162 | $fun = @create_function($args, $code); |
157 | 163 | // @codeCoverageIgnoreStart |
158 | - if (!$fun) |
|
159 | - throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
164 | + if (!$fun) { |
|
165 | + throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
166 | + } |
|
160 | 167 | // @codeCoverageIgnoreEnd |
161 | 168 | self::$lambdaCache[$closure][$closureArgs] = $fun; |
162 | 169 | return $fun; |