Browse Source

Using variadic instead of call_user_func_array

Juan Basso 9 years ago
parent
commit
1f53eb67ea

+ 3 - 3
src/Console/Shell.php

@@ -443,13 +443,13 @@ class Shell
             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 @@ class Shell
             $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));

+ 1 - 1
src/Controller/Component/RequestHandlerComponent.php

@@ -206,7 +206,7 @@ class RequestHandlerComponent extends Component
                 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;
             }
         }

+ 2 - 2
src/Controller/Controller.php

@@ -432,7 +432,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
         }
         $callable = [$this, $request->param('action')];
 
-        return call_user_func_array($callable, $request->param('pass'));
+        return $callable(...$request->param('pass'));
     }
 
     /**
@@ -577,7 +577,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
     {
         $this->request = $this->request->withParam('action', $action);
 
-        return call_user_func_array([&$this, $action], $args);
+        return $this->$action(...$args);
     }
 
     /**

+ 1 - 1
src/Datasource/QueryTrait.php

@@ -456,7 +456,7 @@ trait QueryTrait
         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)

+ 2 - 12
src/Event/Decorator/AbstractDecorator.php

@@ -66,17 +66,7 @@ abstract class AbstractDecorator
     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);
     }
 }

+ 2 - 21
src/Event/EventManager.php

@@ -403,9 +403,6 @@ class EventManager
     /**
      * 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 @@ class EventManager
     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));
     }
 
     /**

+ 2 - 2
src/Filesystem/Folder.php

@@ -238,11 +238,11 @@ class Folder
         }
 
         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];

+ 2 - 2
src/Http/ServerRequest.php

@@ -587,7 +587,7 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
 
             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 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
         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;

+ 2 - 2
src/Mailer/Mailer.php

@@ -202,7 +202,7 @@ abstract class Mailer implements EventListenerInterface
      */
     public function __call($method, $args)
     {
-        call_user_func_array([$this->_email, $method], $args);
+        $this->_email->$method(...$args);
 
         return $this;
     }
@@ -245,7 +245,7 @@ abstract class Mailer implements EventListenerInterface
             $this->_email->viewBuilder()->template($action);
         }
 
-        call_user_func_array([$this, $action], $args);
+        $this->$action(...$args);
 
         $result = $this->_email->send();
         $this->reset();

+ 1 - 1
src/ORM/Association.php

@@ -1309,7 +1309,7 @@ abstract class Association
      */
     public function __call($method, $argument)
     {
-        return call_user_func_array([$this->target(), $method], $argument);
+        return $this->target()->$method(...$argument);
     }
 
     /**

+ 1 - 1
src/ORM/TableRegistry.php

@@ -163,6 +163,6 @@ class TableRegistry
      */
     public static function __callStatic($name, $arguments)
     {
-        return call_user_func_array([static::locator(), $name], $arguments);
+        return static::locator()->$name(...$arguments);
     }
 }

+ 1 - 1
src/Validation/Validation.php

@@ -928,7 +928,7 @@ class Validation
             E_USER_DEPRECATED
         );
 
-        return call_user_func_array([$object, $method], [$check, $args]);
+        return $object->$method($check, $args);
     }
 
     /**

+ 1 - 1
src/Validation/ValidationRule.php

@@ -135,7 +135,7 @@ class ValidationRule
 
         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);
         }

+ 1 - 1
tests/TestCase/Controller/Component/RequestHandlerComponentTest.php

@@ -307,7 +307,7 @@ class RequestHandlerComponentTest extends TestCase
         $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);
     }
 
     /**