@@ -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 | } |
@@ -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 | |
@@ -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(){ |
@@ -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,10 +382,12 @@ 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; |
|
| 382 | - if ($position < 0) |
|
| 383 | - $position = 0; |
|
| 385 | + if ($position < 0) { |
|
| 386 | + $position = $length + $position; |
|
| 387 | + } |
|
| 388 | + if ($position < 0) { |
|
| 389 | + $position = 0; |
|
| 390 | + } |
|
| 384 | 391 | return ($position >= $length) |
| 385 | 392 | ? concat($list, $items) |
| 386 | 393 | : concatAll([take($position, $list), $items, remove($position, $list)]); |
@@ -447,8 +454,9 @@ discard block |
||
| 447 | 454 | function take() { |
| 448 | 455 | $take = function($count, $list) { |
| 449 | 456 | $length = length($list); |
| 450 | - if ($count > $length || $count < -$length) |
|
| 451 | - return []; |
|
| 457 | + if ($count > $length || $count < -$length) { |
|
| 458 | + return []; |
|
| 459 | + } |
|
| 452 | 460 | if(is_string($list)) { |
| 453 | 461 | return ($count >= 0) |
| 454 | 462 | ? substr($list, 0, $count) |
@@ -573,8 +581,9 @@ discard block |
||
| 573 | 581 | function remove() { |
| 574 | 582 | $remove = function($count, $list) { |
| 575 | 583 | $length = length($list); |
| 576 | - if ($count > $length || $count < -$length) |
|
| 577 | - return []; |
|
| 584 | + if ($count > $length || $count < -$length) { |
|
| 585 | + return []; |
|
| 586 | + } |
|
| 578 | 587 | return ($count > 0) |
| 579 | 588 | ? take($count - $length, $list) |
| 580 | 589 | : take($count + $length, $list); |
@@ -705,10 +714,12 @@ discard block |
||
| 705 | 714 | */ |
| 706 | 715 | function slices() { |
| 707 | 716 | $slices = function($size, $list) { |
| 708 | - if(empty($list)) |
|
| 709 | - return is_string($list) ? '' : []; |
|
| 710 | - if(length($list) <= $size) |
|
| 711 | - return [$list]; |
|
| 717 | + if(empty($list)) { |
|
| 718 | + return is_string($list) ? '' : []; |
|
| 719 | + } |
|
| 720 | + if(length($list) <= $size) { |
|
| 721 | + return [$list]; |
|
| 722 | + } |
|
| 712 | 723 | return prepend(take($size, $list), slices($size, remove($size, $list))); |
| 713 | 724 | }; |
| 714 | 725 | return apply(curry($slices), func_get_args()); |
@@ -754,8 +765,9 @@ discard block |
||
| 754 | 765 | function findIndex() { |
| 755 | 766 | $findIndex = function($predicate, $list) { |
| 756 | 767 | foreach ($list as $key => $value) { |
| 757 | - if ($predicate($value)) |
|
| 758 | - return $key; |
|
| 768 | + if ($predicate($value)) { |
|
| 769 | + return $key; |
|
| 770 | + } |
|
| 759 | 771 | } |
| 760 | 772 | return null; |
| 761 | 773 | }; |
@@ -780,8 +792,9 @@ discard block |
||
| 780 | 792 | function findLastIndex() { |
| 781 | 793 | $findLastIndex = function($predicate, $list) { |
| 782 | 794 | foreach (reverse(toPairs($list)) as $pair) { |
| 783 | - if($predicate($pair[1])) |
|
| 784 | - return $pair[0]; |
|
| 795 | + if($predicate($pair[1])) { |
|
| 796 | + return $pair[0]; |
|
| 797 | + } |
|
| 785 | 798 | } |
| 786 | 799 | return null; |
| 787 | 800 | }; |
@@ -972,8 +985,9 @@ discard block |
||
| 972 | 985 | $groupBy = function($fn, $list) { |
| 973 | 986 | return reduce(function($result, $item) use($fn) { |
| 974 | 987 | $index = $fn($item); |
| 975 | - if (! isset($result[$index])) |
|
| 976 | - $result[$index] = []; |
|
| 988 | + if (! isset($result[$index])) { |
|
| 989 | + $result[$index] = []; |
|
| 990 | + } |
|
| 977 | 991 | $result[$index][] = $item; |
| 978 | 992 | return $result; |
| 979 | 993 | }, [], $list); |
@@ -72,8 +72,9 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | function attributes() { |
| 74 | 74 | $attrs = function($object) { |
| 75 | - if (is_object($object)) |
|
| 76 | - return get_object_vars($object); |
|
| 75 | + if (is_object($object)) { |
|
| 76 | + return get_object_vars($object); |
|
| 77 | + } |
|
| 77 | 78 | return $object; |
| 78 | 79 | }; |
| 79 | 80 | return apply(curry($attrs), func_get_args()); |
@@ -208,8 +209,9 @@ discard block |
||
| 208 | 209 | function getPath() { |
| 209 | 210 | $getPath = function($path, $object){ |
| 210 | 211 | return reduce(function($result, $name) { |
| 211 | - if ($result !== null) |
|
| 212 | - $result = get($name, $result); |
|
| 212 | + if ($result !== null) { |
|
| 213 | + $result = get($name, $result); |
|
| 214 | + } |
|
| 213 | 215 | return $result; |
| 214 | 216 | }, $object, $path); |
| 215 | 217 | }; |
@@ -237,10 +239,11 @@ discard block |
||
| 237 | 239 | function set() { |
| 238 | 240 | $set = function($name, $value, $object) { |
| 239 | 241 | $object = clone_($object); |
| 240 | - if (is_object($object)) |
|
| 241 | - $object->{$name} = $value; |
|
| 242 | - else |
|
| 243 | - $object[$name] = $value; |
|
| 242 | + if (is_object($object)) { |
|
| 243 | + $object->{$name} = $value; |
|
| 244 | + } else { |
|
| 245 | + $object[$name] = $value; |
|
| 246 | + } |
|
| 244 | 247 | return $object; |
| 245 | 248 | }; |
| 246 | 249 | return apply(curry($set), func_get_args()); |
@@ -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 | - return _f('identity'); |
|
| 184 | + if(count($fns) < 1) { |
|
| 185 | + return _f('identity'); |
|
| 186 | + } |
|
| 184 | 187 | return curry(function () use ($fns) { |
| 185 | 188 | $result = _apply(array_shift($fns), func_get_args()); |
| 186 | 189 | foreach ($fns as $fn) { |
@@ -29,9 +29,15 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | function type() { |
| 31 | 31 | $type = function($data) { |
| 32 | - if ($data instanceof Error) return 'Error'; |
|
| 33 | - if ($data instanceof Stream) return "Stream({$data->type})"; |
|
| 34 | - if (is_callable($data)) return 'Function'; |
|
| 32 | + if ($data instanceof Error) { |
|
| 33 | + return 'Error'; |
|
| 34 | + } |
|
| 35 | + if ($data instanceof Stream) { |
|
| 36 | + return "Stream({$data->type})"; |
|
| 37 | + } |
|
| 38 | + if (is_callable($data)) { |
|
| 39 | + return 'Function'; |
|
| 40 | + } |
|
| 35 | 41 | switch (gettype($data)) { |
| 36 | 42 | case 'boolean': |
| 37 | 43 | return 'Boolean'; |
@@ -45,8 +51,9 @@ discard block |
||
| 45 | 51 | case 'resource': |
| 46 | 52 | return 'Resource'; |
| 47 | 53 | case 'array': |
| 48 | - if (allSatisfies('is_numeric', keys($data))) |
|
| 49 | - return 'List'; |
|
| 54 | + if (allSatisfies('is_numeric', keys($data))) { |
|
| 55 | + return 'List'; |
|
| 56 | + } |
|
| 50 | 57 | return 'Array'; |
| 51 | 58 | case 'object': |
| 52 | 59 | return 'Object'; |