From 39bc94eb2585ed54acbb333fed7a2e754fdcf90c Mon Sep 17 00:00:00 2001 From: widmogrod Date: Wed, 20 Dec 2017 17:21:18 +0100 Subject: [PATCH] replace call_user_func to native function invocation --- example/ComplexErrorDrivenDevelopmentTest.php | 8 ++++---- example/Free2MonadTest.php | 4 ++-- src/Functional/functions.php | 18 +++++++++--------- src/Functional/miscellaneous.php | 2 +- src/Functional/predicates.php | 2 +- src/Helpful/FunctorLaws.php | 5 +---- src/Monad/Either/Left.php | 2 +- src/Monad/Either/Right.php | 4 ++-- src/Monad/Free2/Pure.php | 4 ++-- src/Monad/IO.php | 6 +++--- src/Monad/IO/errors.php | 2 +- src/Monad/IO/functions.php | 2 +- src/Monad/Identity.php | 2 +- src/Monad/Maybe/Just.php | 2 +- src/Monad/Maybe/Nothing.php | 2 +- src/Monad/Maybe/functions.php | 2 +- src/Monad/Reader.php | 4 ++-- src/Monad/Reader/functions.php | 2 +- src/Monad/State.php | 4 ++-- src/Monad/State/functions.php | 4 ++-- src/Monad/Writer.php | 4 ++-- 21 files changed, 41 insertions(+), 44 deletions(-) diff --git a/example/ComplexErrorDrivenDevelopmentTest.php b/example/ComplexErrorDrivenDevelopmentTest.php index 98fd491..e224c7d 100644 --- a/example/ComplexErrorDrivenDevelopmentTest.php +++ b/example/ComplexErrorDrivenDevelopmentTest.php @@ -48,12 +48,12 @@ function updateDatabase(array $request) function updateDatabaseStep(array $request) { - return call_user_func(E\tryCatch( + return E\tryCatch( f\tee('updateDatabase'), function (\Exception $e) { return $e->getMessage(); } - ), $request); + )($request); } // sendMessage :: Either a b -> Either c d @@ -78,12 +78,12 @@ function returnFailure($data) function handleRequest(array $request) { - return call_user_func(f\pipeline( + return f\pipeline( 'validateInput', f\map('canonizeEmail'), f\bind('updateDatabaseStep'), 'sendMessage' - ), $request); + )($request); } class ComplexErrorDrivenDevelopmentTest extends \PHPUnit_Framework_TestCase diff --git a/example/Free2MonadTest.php b/example/Free2MonadTest.php index 2ffc370..567265e 100644 --- a/example/Free2MonadTest.php +++ b/example/Free2MonadTest.php @@ -167,12 +167,12 @@ function echo_chaining_() function echo_composition_() { - return call_user_func(f\pipeline( + return f\pipeline( getLine_, f\bind(putStrLn_), f\bind(exitSuccess_), f\bind(putStrLn_) // In interpretation of IO Monad this place will never be reached - )); + )(); } class Free2MonadTest extends \PHPUnit_Framework_TestCase diff --git a/src/Functional/functions.php b/src/Functional/functions.php index 8e5711a..d6772c0 100644 --- a/src/Functional/functions.php +++ b/src/Functional/functions.php @@ -27,7 +27,7 @@ const applicator = 'Widmogrod\Functional\applicator'; function applicator($x, callable $f = null) { return curryN(2, function ($y, callable $f) { - return call_user_func($f, $y); + return $f($y); })(...func_get_args()); } @@ -47,7 +47,7 @@ const invoke = 'Widmogrod\Functional\invoke'; function invoke($method, $object = null) { return curryN(2, function ($method, $object) { - return call_user_func([$object, $method]); + return $object->$method(); })(...func_get_args()); } @@ -126,7 +126,7 @@ const tee = 'Widmogrod\Functional\tee'; function tee(callable $function = null, $value = null) { return curryN(2, function (callable $function, $value) { - call_user_func($function, $value); + $function($value); return $value; })(...func_get_args()); @@ -282,7 +282,7 @@ function filter(callable $predicate, Foldable $list = null) { return curryN(2, function (callable $predicate, Foldable $list) { return reduce(function (Listt $list, $x) use ($predicate) { - return call_user_func($predicate, $x) + return $predicate($x) ? append($list, fromValue($x)) : $list; }, Listt::mempty(), $list); @@ -346,9 +346,9 @@ function tryCatch(callable $function, callable $catchFunction, $value) { return curryN(3, function (callable $function, callable $catchFunction, $value) { try { - return call_user_func($function, $value); + return $function($value); } catch (\Exception $e) { - return call_user_func($catchFunction, $e); + return $catchFunction($e); } })(...func_get_args()); } @@ -404,7 +404,7 @@ function liftM2( ) { return $ma->bind(function ($a) use ($mb, $transformation) { return $mb->bind(function ($b) use ($a, $transformation) { - return call_user_func($transformation, $a, $b); + return $transformation($a, $b); }); }); } @@ -437,7 +437,7 @@ function liftA2( ) { return $fa->map(function ($a) use ($transformation) { return function ($b) use ($a, $transformation) { - return call_user_func($transformation, $a, $b); + return $transformation($a, $b); }; })->ap($fb); })(...func_get_args()); @@ -621,7 +621,7 @@ function match(array $patterns, $value = null) $givenType = is_object($value) ? get_class($value) : gettype($value); foreach ($patterns as $className => $fn) { if ($value instanceof $className) { - return call_user_func($fn, $value); + return $fn($value); } } diff --git a/src/Functional/miscellaneous.php b/src/Functional/miscellaneous.php index 7a75f3d..f25c4cd 100644 --- a/src/Functional/miscellaneous.php +++ b/src/Functional/miscellaneous.php @@ -94,7 +94,7 @@ function pipeline(callable $a, callable $b) return function ($value = null) use (&$list) { return array_reduce($list, function ($accumulator, callable $a) { - return call_user_func($a, $accumulator); + return $a($accumulator); }, $value); }; } diff --git a/src/Functional/predicates.php b/src/Functional/predicates.php index 883ce9b..7e0cdc1 100644 --- a/src/Functional/predicates.php +++ b/src/Functional/predicates.php @@ -33,6 +33,6 @@ const orr = 'Widmogrod\Functional\orr'; function orr(callable $predicateA, callable $predicateB = null, $value = null) { return curryN(3, function (callable $a, callable $b, $value) { - return call_user_func($a, $value) || call_user_func($b, $value); + return $a($value) || $b($value); })(...func_get_args()); } diff --git a/src/Helpful/FunctorLaws.php b/src/Helpful/FunctorLaws.php index c782b4a..db7f6a5 100644 --- a/src/Helpful/FunctorLaws.php +++ b/src/Helpful/FunctorLaws.php @@ -31,10 +31,7 @@ class FunctorLaws // composition: fmap (f . g) == fmap f . fmap g $assertEqual( f\map(f\compose($f, $g), $x), - call_user_func( - f\compose(f\map($f), f\map($g)), - $x - ), + f\compose(f\map($f), f\map($g))($x), 'composition' ); } diff --git a/src/Monad/Either/Left.php b/src/Monad/Either/Left.php index 2b285be..845abd1 100644 --- a/src/Monad/Either/Left.php +++ b/src/Monad/Either/Left.php @@ -42,6 +42,6 @@ class Left implements Either */ public function either(callable $left, callable $right) { - return call_user_func($left, $this->value); + return $left($this->value); } } diff --git a/src/Monad/Either/Right.php b/src/Monad/Either/Right.php index 003b1bc..945d14d 100644 --- a/src/Monad/Either/Right.php +++ b/src/Monad/Either/Right.php @@ -33,7 +33,7 @@ class Right implements Either */ public function bind(callable $transformation) { - return call_user_func($transformation, $this->value); + return $transformation($this->value); } /** @@ -41,6 +41,6 @@ class Right implements Either */ public function either(callable $left, callable $right) { - return call_user_func($right, $this->value); + return $right($this->value); } } diff --git a/src/Monad/Free2/Pure.php b/src/Monad/Free2/Pure.php index 7f8cc51..990f6e2 100644 --- a/src/Monad/Free2/Pure.php +++ b/src/Monad/Free2/Pure.php @@ -24,7 +24,7 @@ class Pure implements MonadFree */ public function bind(callable $function) { - return call_user_func($function, $this->value); + return $function($this->value); } /** @@ -32,7 +32,7 @@ class Pure implements MonadFree */ public function map(callable $function) { - return self::of(call_user_func($function, $this->value)); + return self::of($function($this->value)); } /** diff --git a/src/Monad/IO.php b/src/Monad/IO.php index 9956ff4..81a8fa9 100644 --- a/src/Monad/IO.php +++ b/src/Monad/IO.php @@ -43,7 +43,7 @@ class IO implements // But this do not make things lazy, to cheat little bit // IO monad is returned and inside of it is little switch return static::of(function () use ($function) { - $m = call_user_func($function, $this->run()); + $m = $function($this->run()); return $m instanceof IO ? $m->run() @@ -58,7 +58,7 @@ class IO implements { return $this->bind(function ($value) use ($function) { return static::of(function () use ($function, $value) { - return call_user_func($function, $value); + return $function($value); }); }); } @@ -79,7 +79,7 @@ class IO implements public function reduce(callable $function, $accumulator) { return static::of(function () use ($function, $accumulator) { - return call_user_func($function, $accumulator, $this->run()); + return $function($accumulator, $this->run()); }); } } diff --git a/src/Monad/IO/errors.php b/src/Monad/IO/errors.php index 83c44e1..f45d8ac 100644 --- a/src/Monad/IO/errors.php +++ b/src/Monad/IO/errors.php @@ -55,7 +55,7 @@ function tryCatch(M\IO $io = null, callable $catchFunction = null) try { return $io->run(); } catch (\Exception $e) { - return call_user_func($catchFunction, $e); + return $catchFunction($e); } }); }), func_get_args()); diff --git a/src/Monad/IO/functions.php b/src/Monad/IO/functions.php index e646472..c7f2ded 100644 --- a/src/Monad/IO/functions.php +++ b/src/Monad/IO/functions.php @@ -41,7 +41,7 @@ function until(callable $predicate, callable $do, $base, M\IO $ioValue) $isFulfilled = $predicate($value); $base = $isFulfilled ? $base - : call_user_func($do, $value, $base); + : $do($value, $base); } while (!$isFulfilled); return $base; diff --git a/src/Monad/Identity.php b/src/Monad/Identity.php index 4c84128..9d8935d 100644 --- a/src/Monad/Identity.php +++ b/src/Monad/Identity.php @@ -35,7 +35,7 @@ class Identity implements */ public function bind(callable $transformation) { - return call_user_func($transformation, $this->value); + return $transformation($this->value); } /** diff --git a/src/Monad/Maybe/Just.php b/src/Monad/Maybe/Just.php index 9a4e4e9..c69437e 100644 --- a/src/Monad/Maybe/Just.php +++ b/src/Monad/Maybe/Just.php @@ -33,7 +33,7 @@ class Just implements Maybe */ public function bind(callable $transformation) { - return call_user_func($transformation, $this->value); + return $transformation($this->value); } /** diff --git a/src/Monad/Maybe/Nothing.php b/src/Monad/Maybe/Nothing.php index cfa7849..a3ea7ac 100644 --- a/src/Monad/Maybe/Nothing.php +++ b/src/Monad/Maybe/Nothing.php @@ -69,7 +69,7 @@ class Nothing implements Maybe */ public function orElse(callable $fn) { - return call_user_func($fn); + return $fn(); } /** diff --git a/src/Monad/Maybe/functions.php b/src/Monad/Maybe/functions.php index 8a61470..6e05a7a 100644 --- a/src/Monad/Maybe/functions.php +++ b/src/Monad/Maybe/functions.php @@ -58,7 +58,7 @@ function maybe($default, callable $fn = null, Maybe $maybe = null) return $default; } - return call_user_func($fn, $maybe->extract()); + return $fn($maybe->extract()); }), func_get_args()); } diff --git a/src/Monad/Reader.php b/src/Monad/Reader.php index 7f45c35..8053952 100644 --- a/src/Monad/Reader.php +++ b/src/Monad/Reader.php @@ -22,7 +22,7 @@ class Reader implements FantasyLand\Monad public function bind(callable $function) { return self::of(function ($env) use ($function) { - return call_user_func($function, $this->runReader($env))->runReader($env); + return $function($this->runReader($env))->runReader($env); }); } @@ -36,7 +36,7 @@ class Reader implements FantasyLand\Monad public function map(callable $function) { return self::of(function ($env) use ($function) { - return call_user_func($function, $this->runReader($env)); + return $function($this->runReader($env)); }); } diff --git a/src/Monad/Reader/functions.php b/src/Monad/Reader/functions.php index 4704806..55d949f 100644 --- a/src/Monad/Reader/functions.php +++ b/src/Monad/Reader/functions.php @@ -34,7 +34,7 @@ const reader = 'Widmogrod\Monad\Reader\reader'; function reader(callable $readerFunction) { return M\Reader::of(function ($reader) use ($readerFunction) { - return call_user_func($readerFunction, $reader); + return $readerFunction($reader); }); } diff --git a/src/Monad/State.php b/src/Monad/State.php index ca1a12e..7bf5077 100644 --- a/src/Monad/State.php +++ b/src/Monad/State.php @@ -37,7 +37,7 @@ class State implements Monad { return self::of(function ($state) use ($function) { [$value, $newState] = $this->runState($state); - $m = call_user_func($function, $value); + $m = $function($value); return $m instanceof State ? $m->runState($newState) @@ -53,7 +53,7 @@ class State implements Monad return self::of(function ($state) use ($function) { [$value, $newState] = $this->runState($state); - return [call_user_func($function, $value), $newState]; + return [$function($value), $newState]; }); } diff --git a/src/Monad/State/functions.php b/src/Monad/State/functions.php index 7b66d74..18388dd 100644 --- a/src/Monad/State/functions.php +++ b/src/Monad/State/functions.php @@ -68,7 +68,7 @@ const state = 'Widmogrod\Monad\State\state'; function state(callable $stateFunction) { return M\State::of(function ($state) use ($stateFunction) { - return call_user_func($stateFunction, $state); + return $stateFunction($state); }); } @@ -86,7 +86,7 @@ const gets = 'Widmogrod\Monad\State\gets'; function gets(callable $transformation) { return M\State::of(function ($state) use ($transformation) { - return [call_user_func($transformation, $state), $state]; + return [$transformation($state), $state]; }); } diff --git a/src/Monad/Writer.php b/src/Monad/Writer.php index ffcf5ab..c5309c3 100644 --- a/src/Monad/Writer.php +++ b/src/Monad/Writer.php @@ -11,7 +11,7 @@ class Writer implements FantasyLand\Monad public static function of($value, FantasyLand\Monoid $side = null) { - return new static($value, is_null($side) ? S::mempty() : $side); + return new static($value, $side === null ? S::mempty() : $side); } /** @@ -32,7 +32,7 @@ class Writer implements FantasyLand\Monad public function bind(callable $function) { - [$value, $side] = call_user_func($function, $this->value)->runWriter(); + [$value, $side] = $function($this->value)->runWriter(); return new static($value, $this->side->concat($side)); } -- 2.11.0