From 1f53eb67ea27f43d4c0ec3dbdac12cc363922c8d Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Wed, 14 Dec 2016 00:36:56 -0500 Subject: [PATCH] Using variadic instead of call_user_func_array --- src/Console/Shell.php | 6 ++--- .../Component/RequestHandlerComponent.php | 2 +- src/Controller/Controller.php | 4 ++-- src/Datasource/QueryTrait.php | 2 +- src/Event/Decorator/AbstractDecorator.php | 14 ++--------- src/Event/EventManager.php | 23 ++----------------- src/Filesystem/Folder.php | 4 ++-- src/Http/ServerRequest.php | 4 ++-- src/Mailer/Mailer.php | 4 ++-- src/ORM/Association.php | 2 +- src/ORM/TableRegistry.php | 2 +- src/Validation/Validation.php | 2 +- src/Validation/ValidationRule.php | 2 +- .../Component/RequestHandlerComponentTest.php | 2 +- 14 files changed, 22 insertions(+), 51 deletions(-) diff --git a/src/Console/Shell.php b/src/Console/Shell.php index 19bdc66050f..bdf99058580 100644 --- a/src/Console/Shell.php +++ b/src/Console/Shell.php @@ -443,13 +443,13 @@ public function runCommand($argv, $autoMethod = false, $extra = []) array_shift($this->args); $this->startup(); - return call_user_func_array([$this, $method], $this->args); + return $this->$method(...$this->args); } if ($isMethod && isset($subcommands[$command])) { $this->startup(); - return call_user_func_array([$this, $method], $this->args); + return $this->$method(...$this->args); } if ($this->hasTask($command) && isset($subcommands[$command])) { @@ -463,7 +463,7 @@ public function runCommand($argv, $autoMethod = false, $extra = []) $this->command = 'main'; $this->startup(); - return call_user_func_array([$this, 'main'], $this->args); + return $this->main(...$this->args); } $this->out($this->OptionParser->help($command)); diff --git a/src/Controller/Component/RequestHandlerComponent.php b/src/Controller/Component/RequestHandlerComponent.php index 5b7d534b4f0..67cb5df228f 100644 --- a/src/Controller/Component/RequestHandlerComponent.php +++ b/src/Controller/Component/RequestHandlerComponent.php @@ -206,7 +206,7 @@ public function startup(Event $event) throw new RuntimeException(sprintf("Invalid callable for '%s' type.", $type)); } if ($this->requestedWith($type)) { - $input = call_user_func_array([$request, 'input'], $handler); + $input = $request->input(...$handler); $request->data = (array)$input; } } diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index 3d71d425e0a..0a55c9a20c5 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -432,7 +432,7 @@ public function invokeAction() } $callable = [$this, $request->param('action')]; - return call_user_func_array($callable, $request->param('pass')); + return $callable(...$request->param('pass')); } /** @@ -577,7 +577,7 @@ public function setAction($action, ...$args) { $this->request = $this->request->withParam('action', $action); - return call_user_func_array([&$this, $action], $args); + return $this->$action(...$args); } /** diff --git a/src/Datasource/QueryTrait.php b/src/Datasource/QueryTrait.php index 7336ef07d79..04bca1ddb66 100644 --- a/src/Datasource/QueryTrait.php +++ b/src/Datasource/QueryTrait.php @@ -456,7 +456,7 @@ public function __call($method, $arguments) if (in_array($method, get_class_methods($resultSetClass))) { $results = $this->all(); - return call_user_func_array([$results, $method], $arguments); + return $results->$method(...$arguments); } throw new BadMethodCallException( sprintf('Unknown method "%s"', $method) diff --git a/src/Event/Decorator/AbstractDecorator.php b/src/Event/Decorator/AbstractDecorator.php index a4aabd6e742..8b2724264f4 100644 --- a/src/Event/Decorator/AbstractDecorator.php +++ b/src/Event/Decorator/AbstractDecorator.php @@ -66,17 +66,7 @@ public function __invoke() protected function _call($args) { $callable = $this->_callable; - switch (count($args)) { - case 0: - return $callable(); - case 1: - return $callable($args[0]); - case 2: - return $callable($args[0], $args[1]); - case 3: - return $callable($args[0], $args[1], $args[2]); - default: - return call_user_func_array($callable, $args); - } + + return $callable(...$args); } } diff --git a/src/Event/EventManager.php b/src/Event/EventManager.php index e0499a3c5e8..25ffa00a340 100644 --- a/src/Event/EventManager.php +++ b/src/Event/EventManager.php @@ -403,9 +403,6 @@ public function dispatch($event) /** * Calls a listener. * - * Direct callback invocation is up to 30% faster than using call_user_func_array. - * Optimize the common cases to provide improved performance. - * * @param callable $listener The listener to trigger. * @param \Cake\Event\Event $event Event instance. * @return mixed The result of the $listener function. @@ -413,24 +410,8 @@ public function dispatch($event) protected function _callListener(callable $listener, Event $event) { $data = $event->data(); - $length = count($data); - if ($length) { - $data = array_values($data); - } - switch ($length) { - case 0: - return $listener($event); - case 1: - return $listener($event, $data[0]); - case 2: - return $listener($event, $data[0], $data[1]); - case 3: - return $listener($event, $data[0], $data[1], $data[2]); - default: - array_unshift($data, $event); - - return call_user_func_array($listener, $data); - } + + return $listener($event, ...array_values($data)); } /** diff --git a/src/Filesystem/Folder.php b/src/Filesystem/Folder.php index 4bf6146d208..173bcde9d6f 100644 --- a/src/Filesystem/Folder.php +++ b/src/Filesystem/Folder.php @@ -238,11 +238,11 @@ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = f } if ($dirs) { - $dirs = call_user_func_array('array_merge', $dirs); + $dirs = array_merge(...array_values($dirs)); } if ($files) { - $files = call_user_func_array('array_merge', $files); + $files = array_merge(...array_values($files)); } return [$dirs, $files]; diff --git a/src/Http/ServerRequest.php b/src/Http/ServerRequest.php index ab2801862cc..9d99a258f30 100644 --- a/src/Http/ServerRequest.php +++ b/src/Http/ServerRequest.php @@ -587,7 +587,7 @@ public function __call($name, $params) array_unshift($params, $type); - return call_user_func_array([$this, 'is'], $params); + return $this->is(...$params); } throw new BadMethodCallException(sprintf('Method %s does not exist', $name)); } @@ -683,7 +683,7 @@ protected function _is($type, $args) if (is_callable($detect)) { array_unshift($args, $this); - return call_user_func_array($detect, $args); + return $detect(...$args); } if (isset($detect['env']) && $this->_environmentDetector($detect)) { return true; diff --git a/src/Mailer/Mailer.php b/src/Mailer/Mailer.php index 78fb81e553e..4745472d290 100644 --- a/src/Mailer/Mailer.php +++ b/src/Mailer/Mailer.php @@ -202,7 +202,7 @@ public function viewBuilder() */ public function __call($method, $args) { - call_user_func_array([$this->_email, $method], $args); + $this->_email->$method(...$args); return $this; } @@ -245,7 +245,7 @@ public function send($action, $args = [], $headers = []) $this->_email->viewBuilder()->template($action); } - call_user_func_array([$this, $action], $args); + $this->$action(...$args); $result = $this->_email->send(); $this->reset(); diff --git a/src/ORM/Association.php b/src/ORM/Association.php index e6a23ae997a..809b386038c 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -1309,7 +1309,7 @@ public function __isset($property) */ public function __call($method, $argument) { - return call_user_func_array([$this->target(), $method], $argument); + return $this->target()->$method(...$argument); } /** diff --git a/src/ORM/TableRegistry.php b/src/ORM/TableRegistry.php index 87916501a3a..e5ecb4d55d5 100644 --- a/src/ORM/TableRegistry.php +++ b/src/ORM/TableRegistry.php @@ -163,6 +163,6 @@ public static function clear() */ public static function __callStatic($name, $arguments) { - return call_user_func_array([static::locator(), $name], $arguments); + return static::locator()->$name(...$arguments); } } diff --git a/src/Validation/Validation.php b/src/Validation/Validation.php index 1ef822a8515..da0fee41837 100644 --- a/src/Validation/Validation.php +++ b/src/Validation/Validation.php @@ -928,7 +928,7 @@ public static function userDefined($check, $object, $method, $args = null) E_USER_DEPRECATED ); - return call_user_func_array([$object, $method], [$check, $args]); + return $object->$method($check, $args); } /** diff --git a/src/Validation/ValidationRule.php b/src/Validation/ValidationRule.php index 6b8c1155790..6f002710e28 100644 --- a/src/Validation/ValidationRule.php +++ b/src/Validation/ValidationRule.php @@ -135,7 +135,7 @@ public function process($value, array $providers, array $context = []) if ($this->_pass) { $args = array_merge([$value], $this->_pass, [$context]); - $result = call_user_func_array($callable, $args); + $result = $callable(...$args); } else { $result = $callable($value, $context); } diff --git a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php index f14d42cff39..5fc80bf3a92 100644 --- a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php +++ b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php @@ -307,7 +307,7 @@ public function testInitializeContentTypeAndExtensionMismatch() $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller)); $this->assertNull($this->RequestHandler->ext); - call_user_func_array(['Cake\Routing\Router', 'extensions'], [$extensions, false]); + Router::extensions($extensions, false); } /**