@@ -91,8 +91,9 @@ |
||
91 | 91 | */ |
92 | 92 | function pipe() { |
93 | 93 | $fns = func_get_args(); |
94 | - if(count($fns) < 1) |
|
95 | - throw new InvalidArgument("pipe() requires at least one argument"); |
|
94 | + if(count($fns) < 1) { |
|
95 | + throw new InvalidArgument("pipe() requires at least one argument"); |
|
96 | + } |
|
96 | 97 | return curry(function () use ($fns) { |
97 | 98 | $result = _apply(array_shift($fns), func_get_args()); |
98 | 99 | foreach ($fns as $fn) { |
@@ -131,8 +131,9 @@ discard block |
||
131 | 131 | * @return mixed |
132 | 132 | */ |
133 | 133 | function head($array) { |
134 | - if(is_string($array)) |
|
135 | - return substr($array, 0, 1); |
|
134 | + if(is_string($array)) { |
|
135 | + return substr($array, 0, 1); |
|
136 | + } |
|
136 | 137 | return (count($array) > 0) |
137 | 138 | ? $array[0] |
138 | 139 | : null; |
@@ -153,8 +154,9 @@ discard block |
||
153 | 154 | * @return mixed |
154 | 155 | */ |
155 | 156 | function last($array) { |
156 | - if(is_string($array)) |
|
157 | - return substr($array, -1); |
|
157 | + if(is_string($array)) { |
|
158 | + return substr($array, -1); |
|
159 | + } |
|
158 | 160 | return (count($array) > 0) |
159 | 161 | ? $array[count($array) - 1] |
160 | 162 | : null; |
@@ -176,10 +178,11 @@ discard block |
||
176 | 178 | * @return array |
177 | 179 | */ |
178 | 180 | function init($array) { |
179 | - if(is_string($array)) |
|
180 | - return (strlen($array) > 1) |
|
181 | + if(is_string($array)) { |
|
182 | + return (strlen($array) > 1) |
|
181 | 183 | ? substr($array, 0, strlen($array) - 1) |
182 | 184 | : ''; |
185 | + } |
|
183 | 186 | return (count($array) > 1) |
184 | 187 | ? array_slice($array, 0, count($array) - 1) |
185 | 188 | : []; |
@@ -201,10 +204,11 @@ discard block |
||
201 | 204 | * @return array |
202 | 205 | */ |
203 | 206 | function tail($array) { |
204 | - if(is_string($array)) |
|
205 | - return (strlen($array) > 1) |
|
207 | + if(is_string($array)) { |
|
208 | + return (strlen($array) > 1) |
|
206 | 209 | ? substr($array, 1) |
207 | 210 | : ''; |
211 | + } |
|
208 | 212 | return (count($array) > 1) |
209 | 213 | ? array_slice($array, 1) |
210 | 214 | : []; |
@@ -302,8 +306,9 @@ discard block |
||
302 | 306 | */ |
303 | 307 | function concat() { |
304 | 308 | $concat = function($array1, $array2) { |
305 | - if (is_string($array1)) |
|
306 | - return $array1 . $array2; |
|
309 | + if (is_string($array1)) { |
|
310 | + return $array1 . $array2; |
|
311 | + } |
|
307 | 312 | return array_merge($array1, $array2); |
308 | 313 | }; |
309 | 314 | return apply(curry($concat), func_get_args()); |
@@ -324,8 +329,9 @@ discard block |
||
324 | 329 | */ |
325 | 330 | function append() { |
326 | 331 | $append = function ($item, $array) { |
327 | - if (is_string($array)) |
|
328 | - return $array . $item; |
|
332 | + if (is_string($array)) { |
|
333 | + return $array . $item; |
|
334 | + } |
|
329 | 335 | return array_merge($array, [$item]); |
330 | 336 | }; |
331 | 337 | return apply(curry($append), func_get_args()); |
@@ -346,8 +352,9 @@ discard block |
||
346 | 352 | */ |
347 | 353 | function prepend() { |
348 | 354 | $prepend = function ($item, $array) { |
349 | - if (is_string($array)) |
|
350 | - return $item . $array; |
|
355 | + if (is_string($array)) { |
|
356 | + return $item . $array; |
|
357 | + } |
|
351 | 358 | return array_merge([$item], $array); |
352 | 359 | }; |
353 | 360 | return apply(curry($prepend), func_get_args()); |
@@ -474,10 +481,12 @@ discard block |
||
474 | 481 | */ |
475 | 482 | function slices() { |
476 | 483 | $slices = function($size, $array) { |
477 | - if(empty($array)) |
|
478 | - return is_string($array) ? '' : []; |
|
479 | - if(length($array) <= $size) |
|
480 | - return [$array]; |
|
484 | + if(empty($array)) { |
|
485 | + return is_string($array) ? '' : []; |
|
486 | + } |
|
487 | + if(length($array) <= $size) { |
|
488 | + return [$array]; |
|
489 | + } |
|
481 | 490 | return prepend(take($size, $array), slices($size, remove($size, $array))); |
482 | 491 | }; |
483 | 492 | return apply(curry($slices), func_get_args()); |
@@ -500,8 +509,9 @@ discard block |
||
500 | 509 | */ |
501 | 510 | function contains() { |
502 | 511 | $contains = function($item, $array) { |
503 | - if(is_string($array)) |
|
504 | - return false !== strpos($array, $item); |
|
512 | + if(is_string($array)) { |
|
513 | + return false !== strpos($array, $item); |
|
514 | + } |
|
505 | 515 | return in_array($item, $array); |
506 | 516 | }; |
507 | 517 | return apply(curry($contains), func_get_args()); |
@@ -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 |