@@ -37,8 +37,9 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | protected function __construct ($message, Error $error = null) |
| 39 | 39 | { |
| 40 | - if (null != $error) |
|
| 41 | - $message = $error->message() . ' -> ' . $message; |
|
| 40 | + if (null != $error) { |
|
| 41 | + $message = $error->message() . ' -> ' . $message; |
|
| 42 | + } |
|
| 42 | 43 | $this->message = $message; |
| 43 | 44 | } |
| 44 | 45 | |
@@ -84,8 +84,9 @@ discard block |
||
| 84 | 84 | public static function of ($data) |
| 85 | 85 | { |
| 86 | 86 | $data = func_get_args(); |
| 87 | - if (count($data) == 1) |
|
| 88 | - $data = $data[0]; |
|
| 87 | + if (count($data) == 1) { |
|
| 88 | + $data = $data[0]; |
|
| 89 | + } |
|
| 89 | 90 | return new Stream($data, [], type($data)); |
| 90 | 91 | } |
| 91 | 92 | |
@@ -122,8 +123,9 @@ discard block |
||
| 122 | 123 | */ |
| 123 | 124 | protected static function execute ($operations, $data) |
| 124 | 125 | { |
| 125 | - if (length($operations) == 0) |
|
| 126 | - return $data; |
|
| 126 | + if (length($operations) == 0) { |
|
| 127 | + return $data; |
|
| 128 | + } |
|
| 127 | 129 | $operations = apply('Tarsana\\Functional\\pipe', map(function($operation){ |
| 128 | 130 | if ($operation['name'] == 'apply') { |
| 129 | 131 | return $operation['args']; |
@@ -234,8 +236,9 @@ discard block |
||
| 234 | 236 | */ |
| 235 | 237 | public function get () |
| 236 | 238 | { |
| 237 | - if ($this->type == 'Error') |
|
| 238 | - return $this->data; |
|
| 239 | + if ($this->type == 'Error') { |
|
| 240 | + return $this->data; |
|
| 241 | + } |
|
| 239 | 242 | return Stream::execute($this->operations, $this->data); |
| 240 | 243 | } |
| 241 | 244 | |
@@ -421,11 +424,13 @@ discard block |
||
| 421 | 424 | { |
| 422 | 425 | $args = tail(func_get_args()); |
| 423 | 426 | return Stream::apply('apply', function($data) use($method, $args) { |
| 424 | - if (is_callable([$data, $method])) |
|
| 425 | - return call_user_func_array([$data, $method], $args); |
|
| 427 | + if (is_callable([$data, $method])) { |
|
| 428 | + return call_user_func_array([$data, $method], $args); |
|
| 429 | + } |
|
| 426 | 430 | $text = toString($data); |
| 427 | - if (method_exists($data, $method)) |
|
| 428 | - return Error::of("Method '{$method}' of {$text} is not accessible"); |
|
| 431 | + if (method_exists($data, $method)) { |
|
| 432 | + return Error::of("Method '{$method}' of {$text} is not accessible"); |
|
| 433 | + } |
|
| 429 | 434 | return Error::of("Method '{$method}' of {$text} is not found"); |
| 430 | 435 | }, $this); |
| 431 | 436 | } |
@@ -480,8 +485,9 @@ discard block |
||
| 480 | 485 | return $data; |
| 481 | 486 | } |
| 482 | 487 | $text = toString($data); |
| 483 | - if (method_exists($data, $method)) |
|
| 484 | - return Error::of("Method '{$method}' of {$text} is not accessible"); |
|
| 488 | + if (method_exists($data, $method)) { |
|
| 489 | + return Error::of("Method '{$method}' of {$text} is not accessible"); |
|
| 490 | + } |
|
| 485 | 491 | return Error::of("Method '{$method}' of {$text} is not found"); |
| 486 | 492 | }, $this); |
| 487 | 493 | } |
@@ -27,22 +27,42 @@ |
||
| 27 | 27 | * @return string |
| 28 | 28 | */ |
| 29 | 29 | function type($data) { |
| 30 | - if (null === $data) return 'Null'; |
|
| 31 | - if (true === $data || false === $data) return 'Boolean'; |
|
| 32 | - if ($data instanceof Error) return 'Error'; |
|
| 33 | - if ($data instanceof Stream) return 'Stream'; |
|
| 34 | - if (is_callable($data)) return 'Function'; |
|
| 35 | - if (is_resource($data)) return 'Resource'; |
|
| 36 | - if (is_string($data)) return 'String'; |
|
| 37 | - if (is_integer($data) || is_float($data)) return 'Number'; |
|
| 30 | + if (null === $data) { |
|
| 31 | + return 'Null'; |
|
| 32 | + } |
|
| 33 | + if (true === $data || false === $data) { |
|
| 34 | + return 'Boolean'; |
|
| 35 | + } |
|
| 36 | + if ($data instanceof Error) { |
|
| 37 | + return 'Error'; |
|
| 38 | + } |
|
| 39 | + if ($data instanceof Stream) { |
|
| 40 | + return 'Stream'; |
|
| 41 | + } |
|
| 42 | + if (is_callable($data)) { |
|
| 43 | + return 'Function'; |
|
| 44 | + } |
|
| 45 | + if (is_resource($data)) { |
|
| 46 | + return 'Resource'; |
|
| 47 | + } |
|
| 48 | + if (is_string($data)) { |
|
| 49 | + return 'String'; |
|
| 50 | + } |
|
| 51 | + if (is_integer($data) || is_float($data)) { |
|
| 52 | + return 'Number'; |
|
| 53 | + } |
|
| 38 | 54 | if (is_array($data)) { |
| 39 | - if (all('is_numeric', array_keys($data))) |
|
| 40 | - return 'List'; |
|
| 41 | - if (all('is_string', array_keys($data))) |
|
| 42 | - return 'ArrayObject'; |
|
| 55 | + if (all('is_numeric', array_keys($data))) { |
|
| 56 | + return 'List'; |
|
| 57 | + } |
|
| 58 | + if (all('is_string', array_keys($data))) { |
|
| 59 | + return 'ArrayObject'; |
|
| 60 | + } |
|
| 43 | 61 | return 'Array'; |
| 44 | 62 | } |
| 45 | - if (is_object($data)) return 'Object'; |
|
| 63 | + if (is_object($data)) { |
|
| 64 | + return 'Object'; |
|
| 65 | + } |
|
| 46 | 66 | return 'Unknown'; |
| 47 | 67 | } |
| 48 | 68 | |
@@ -67,13 +67,16 @@ discard block |
||
| 67 | 67 | // Extracts the type of a block |
| 68 | 68 | // Object -> String |
| 69 | 69 | function typeOf($data) { |
| 70 | - if (isset($data->ctx->type)) |
|
| 71 | - return $data->ctx->type; |
|
| 72 | - if (F\length(tags('var', $data)) > 0) |
|
| 73 | - return 'attr'; |
|
| 74 | - if (F\length(tags('return', $data)) > 0) |
|
| 75 | - return 'method'; |
|
| 76 | -} |
|
| 70 | + if (isset($data->ctx->type)) { |
|
| 71 | + return $data->ctx->type; |
|
| 72 | + } |
|
| 73 | + if (F\length(tags('var', $data)) > 0) { |
|
| 74 | + return 'attr'; |
|
| 75 | + } |
|
| 76 | + if (F\length(tags('return', $data)) > 0) { |
|
| 77 | + return 'method'; |
|
| 78 | + } |
|
| 79 | + } |
|
| 77 | 80 | |
| 78 | 81 | // Extract keywords |
| 79 | 82 | // Object -> [String] |
@@ -82,8 +85,9 @@ discard block |
||
| 82 | 85 | return []; |
| 83 | 86 | } |
| 84 | 87 | $size = strpos($data->code, '('); |
| 85 | - if ($size === false) |
|
| 86 | - $size = strlen($data->code); |
|
| 88 | + if ($size === false) { |
|
| 89 | + $size = strlen($data->code); |
|
| 90 | + } |
|
| 87 | 91 | $keywords = F\pipe( |
| 88 | 92 | F\take($size), |
| 89 | 93 | F\split(' '), |
@@ -124,8 +128,9 @@ discard block |
||
| 124 | 128 | // Get a markdown code block |
| 125 | 129 | // String -> String -> String |
| 126 | 130 | function code($lang, $text) { |
| 127 | - if(trim($text) == '') |
|
| 128 | - return ''; |
|
| 131 | + if(trim($text) == '') { |
|
| 132 | + return ''; |
|
| 133 | + } |
|
| 129 | 134 | return "```{$lang}\n{$text}\n```"; |
| 130 | 135 | } |
| 131 | 136 | |
@@ -32,8 +32,9 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | function attributes() { |
| 34 | 34 | $attrs = function($object) { |
| 35 | - if (is_object($object)) |
|
| 36 | - return get_object_vars($object); |
|
| 35 | + if (is_object($object)) { |
|
| 36 | + return get_object_vars($object); |
|
| 37 | + } |
|
| 37 | 38 | return $object; |
| 38 | 39 | }; |
| 39 | 40 | return apply(curry($attrs), func_get_args()); |
@@ -97,8 +97,9 @@ |
||
| 97 | 97 | function equals() { |
| 98 | 98 | $equals = function($a, $b) { |
| 99 | 99 | $type = type($a); |
| 100 | - if ($type != type($b)) |
|
| 101 | - return false; |
|
| 100 | + if ($type != type($b)) { |
|
| 101 | + return false; |
|
| 102 | + } |
|
| 102 | 103 | switch ($type) { |
| 103 | 104 | case 'Null': |
| 104 | 105 | case 'Boolean': |
@@ -11,8 +11,9 @@ |
||
| 11 | 11 | private function __construct(){} |
| 12 | 12 | public static function get() |
| 13 | 13 | { |
| 14 | - if(static::$instance === null) |
|
| 15 | - static::$instance = new Placeholder; |
|
| 14 | + if(static::$instance === null) { |
|
| 15 | + static::$instance = new Placeholder; |
|
| 16 | + } |
|
| 16 | 17 | return static::$instance; |
| 17 | 18 | } |
| 18 | 19 | public function __toString(){ |
@@ -59,8 +59,9 @@ discard block |
||
| 59 | 59 | $addArgument = function($currentBoundArgs, $arg) use($fnArgsCount) { |
| 60 | 60 | $currentBoundArgsCount = count($currentBoundArgs); |
| 61 | 61 | $placeholderPosition = 0; |
| 62 | - while($placeholderPosition < $currentBoundArgsCount && !_is_placeholder($currentBoundArgs[$placeholderPosition])) |
|
| 63 | - $placeholderPosition ++; |
|
| 62 | + while($placeholderPosition < $currentBoundArgsCount && !_is_placeholder($currentBoundArgs[$placeholderPosition])) { |
|
| 63 | + $placeholderPosition ++; |
|
| 64 | + } |
|
| 64 | 65 | if ($currentBoundArgsCount < $fnArgsCount || $placeholderPosition == $currentBoundArgsCount) { |
| 65 | 66 | $currentBoundArgs[] = $arg; |
| 66 | 67 | } else { // There is a placeholder and number of bound args > $fnArgsCount |
@@ -87,8 +88,9 @@ discard block |
||
| 87 | 88 | $boundArgs = _merge_args($argsCount, $boundArgs, func_get_args()); |
| 88 | 89 | $numberOfPlaceholders = count(array_filter($boundArgs, _f('_is_placeholder'))); |
| 89 | 90 | $numberOfGivenArgs = count($boundArgs) - $numberOfPlaceholders; |
| 90 | - if ($numberOfGivenArgs >= $argsCount) |
|
| 91 | - return call_user_func_array($fn, $boundArgs); |
|
| 91 | + if ($numberOfGivenArgs >= $argsCount) { |
|
| 92 | + return call_user_func_array($fn, $boundArgs); |
|
| 93 | + } |
|
| 92 | 94 | return _curried_function($fn, $argsCount, $boundArgs); |
| 93 | 95 | }; |
| 94 | 96 | } |
@@ -179,8 +181,9 @@ discard block |
||
| 179 | 181 | */ |
| 180 | 182 | function pipe() { |
| 181 | 183 | $fns = func_get_args(); |
| 182 | - if(count($fns) < 1) |
|
| 183 | - throw new InvalidArgument("pipe() requires at least one argument"); |
|
| 184 | + if(count($fns) < 1) { |
|
| 185 | + throw new InvalidArgument("pipe() requires at least one argument"); |
|
| 186 | + } |
|
| 184 | 187 | return curry(function () use ($fns) { |
| 185 | 188 | $result = _apply(array_shift($fns), func_get_args()); |
| 186 | 189 | foreach ($fns as $fn) { |
@@ -128,8 +128,9 @@ discard block |
||
| 128 | 128 | * @return mixed |
| 129 | 129 | */ |
| 130 | 130 | function head($list) { |
| 131 | - if(is_string($list)) |
|
| 132 | - return substr($list, 0, 1); |
|
| 131 | + if(is_string($list)) { |
|
| 132 | + return substr($list, 0, 1); |
|
| 133 | + } |
|
| 133 | 134 | return (count($list) > 0) |
| 134 | 135 | ? $list[0] |
| 135 | 136 | : null; |
@@ -150,8 +151,9 @@ discard block |
||
| 150 | 151 | * @return mixed |
| 151 | 152 | */ |
| 152 | 153 | function last($list) { |
| 153 | - if(is_string($list)) |
|
| 154 | - return substr($list, -1); |
|
| 154 | + if(is_string($list)) { |
|
| 155 | + return substr($list, -1); |
|
| 156 | + } |
|
| 155 | 157 | return (count($list) > 0) |
| 156 | 158 | ? $list[count($list) - 1] |
| 157 | 159 | : null; |
@@ -173,10 +175,11 @@ discard block |
||
| 173 | 175 | * @return array |
| 174 | 176 | */ |
| 175 | 177 | function init($list) { |
| 176 | - if(is_string($list)) |
|
| 177 | - return (strlen($list) > 1) |
|
| 178 | + if(is_string($list)) { |
|
| 179 | + return (strlen($list) > 1) |
|
| 178 | 180 | ? substr($list, 0, strlen($list) - 1) |
| 179 | 181 | : ''; |
| 182 | + } |
|
| 180 | 183 | return (count($list) > 1) |
| 181 | 184 | ? array_slice($list, 0, count($list) - 1) |
| 182 | 185 | : []; |
@@ -198,10 +201,11 @@ discard block |
||
| 198 | 201 | * @return array |
| 199 | 202 | */ |
| 200 | 203 | function tail($list) { |
| 201 | - if(is_string($list)) |
|
| 202 | - return (strlen($list) > 1) |
|
| 204 | + if(is_string($list)) { |
|
| 205 | + return (strlen($list) > 1) |
|
| 203 | 206 | ? substr($list, 1) |
| 204 | 207 | : ''; |
| 208 | + } |
|
| 205 | 209 | return (count($list) > 1) |
| 206 | 210 | ? array_slice($list, 1) |
| 207 | 211 | : []; |
@@ -302,8 +306,9 @@ discard block |
||
| 302 | 306 | $t1 = toString($list1); |
| 303 | 307 | $t2 = toString($list2); |
| 304 | 308 | // echo "Concating {$t1} and {$t2}", PHP_EOL; |
| 305 | - if (is_string($list1) && is_string($list2)) |
|
| 306 | - return $list1 . $list2; |
|
| 309 | + if (is_string($list1) && is_string($list2)) { |
|
| 310 | + return $list1 . $list2; |
|
| 311 | + } |
|
| 307 | 312 | return array_merge($list1, $list2); |
| 308 | 313 | }; |
| 309 | 314 | return apply(curry($concat), func_get_args()); |
@@ -377,8 +382,9 @@ discard block |
||
| 377 | 382 | function insertAll() { |
| 378 | 383 | $insertAll = function($position, $items, $list) { |
| 379 | 384 | $length = length($list); |
| 380 | - if ($position < 0) |
|
| 381 | - $position = $length + $position; |
|
| 385 | + if ($position < 0) { |
|
| 386 | + $position = $length + $position; |
|
| 387 | + } |
|
| 382 | 388 | return ($position >= $length) |
| 383 | 389 | ? concat($list, $items) |
| 384 | 390 | : concatAll([take($position, $list), $items, remove($position, $list)]); |
@@ -445,8 +451,9 @@ discard block |
||
| 445 | 451 | function take() { |
| 446 | 452 | $take = function($count, $list) { |
| 447 | 453 | $length = length($list); |
| 448 | - if ($count > $length || $count < -$length) |
|
| 449 | - return []; |
|
| 454 | + if ($count > $length || $count < -$length) { |
|
| 455 | + return []; |
|
| 456 | + } |
|
| 450 | 457 | if(is_string($list)) { |
| 451 | 458 | return ($count >= 0) |
| 452 | 459 | ? substr($list, 0, $count) |
@@ -571,8 +578,9 @@ discard block |
||
| 571 | 578 | function remove() { |
| 572 | 579 | $remove = function($count, $list) { |
| 573 | 580 | $length = length($list); |
| 574 | - if ($count > $length || $count < -$length) |
|
| 575 | - return []; |
|
| 581 | + if ($count > $length || $count < -$length) { |
|
| 582 | + return []; |
|
| 583 | + } |
|
| 576 | 584 | return ($count > 0) |
| 577 | 585 | ? take($count - $length, $list) |
| 578 | 586 | : take($count + $length, $list); |
@@ -703,10 +711,12 @@ discard block |
||
| 703 | 711 | */ |
| 704 | 712 | function slices() { |
| 705 | 713 | $slices = function($size, $list) { |
| 706 | - if(empty($list)) |
|
| 707 | - return is_string($list) ? '' : []; |
|
| 708 | - if(length($list) <= $size) |
|
| 709 | - return [$list]; |
|
| 714 | + if(empty($list)) { |
|
| 715 | + return is_string($list) ? '' : []; |
|
| 716 | + } |
|
| 717 | + if(length($list) <= $size) { |
|
| 718 | + return [$list]; |
|
| 719 | + } |
|
| 710 | 720 | return prepend(take($size, $list), slices($size, remove($size, $list))); |
| 711 | 721 | }; |
| 712 | 722 | return apply(curry($slices), func_get_args()); |
@@ -752,8 +762,9 @@ discard block |
||
| 752 | 762 | function findIndex() { |
| 753 | 763 | $findIndex = function($predicate, $list) { |
| 754 | 764 | foreach ($list as $key => $value) { |
| 755 | - if ($predicate($value)) |
|
| 756 | - return $key; |
|
| 765 | + if ($predicate($value)) { |
|
| 766 | + return $key; |
|
| 767 | + } |
|
| 757 | 768 | } |
| 758 | 769 | return null; |
| 759 | 770 | }; |
@@ -778,8 +789,9 @@ discard block |
||
| 778 | 789 | function findLastIndex() { |
| 779 | 790 | $findLastIndex = function($predicate, $list) { |
| 780 | 791 | foreach (reverse(toPairs($list)) as $pair) { |
| 781 | - if($predicate($pair[1])) |
|
| 782 | - return $pair[0]; |
|
| 792 | + if($predicate($pair[1])) { |
|
| 793 | + return $pair[0]; |
|
| 794 | + } |
|
| 783 | 795 | } |
| 784 | 796 | return null; |
| 785 | 797 | }; |
@@ -968,8 +980,9 @@ discard block |
||
| 968 | 980 | $groupBy = function($fn, $list) { |
| 969 | 981 | return reduce(function($result, $item) use($fn) { |
| 970 | 982 | $index = $fn($item); |
| 971 | - if (! isset($result[$index])) |
|
| 972 | - $result[$index] = []; |
|
| 983 | + if (! isset($result[$index])) { |
|
| 984 | + $result[$index] = []; |
|
| 985 | + } |
|
| 973 | 986 | $result[$index][] = $item; |
| 974 | 987 | return $result; |
| 975 | 988 | }, [], $list); |