Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Removing redundant code that repeats actions for different number of parameters passed #33

Closed
wants to merge 1 commit into from

3 participants

@topsitemakers

On several places the Dispatcher and Loader methods repeat the same action for different number of parameters passed. Finally, default handler works for any number of parameters.

This code seems to be redundant as the framework works as expected without those lines.

Have a look please and merge if works for you as well.

Thanks.

@topsitemakers topsitemakers Removing redundant code that repeats actions for different number of …
…parameters passed. Using default fallback instead for all cases.
24ed25c
@mikecao
Owner

The code is like that for performance. Calling call_user_func_array is much slower than invoking the function directly, on average 3 times slower. The same is true with invoking ReflectionClass. It checks up to 5 parameters before trying the defaults, which should cover a majority of cases.

@warmwaffles

I agree with @mikecao. I've seen this done in other projects other than PHP for the exact same reason. It does obfuscate a bit, but the performance gain is worth it.

@warmwaffles

I agree with @mikecao. I've seen this done in other projects other than PHP for the exact same reason. It does obfuscate a bit, but the performance gain is worth it.

@topsitemakers

Thanks for clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 9, 2013
  1. @topsitemakers

    Removing redundant code that repeats actions for different number of …

    topsitemakers authored
    …parameters passed. Using default fallback instead for all cases.
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 63 deletions.
  1. +2 −46 flight/core/Dispatcher.php
  2. +2 −17 flight/core/Loader.php
View
48 flight/core/Dispatcher.php
@@ -152,22 +152,7 @@ public static function execute($callback, array &$params = array()) {
* @return mixed Function results
*/
public static function callFunction($func, array &$params = array()) {
- switch (count($params)) {
- case 0:
- return $func();
- case 1:
- return $func($params[0]);
- case 2:
- return $func($params[0], $params[1]);
- case 3:
- return $func($params[0], $params[1], $params[2]);
- case 4:
- return $func($params[0], $params[1], $params[2], $params[3]);
- case 5:
- return $func($params[0], $params[1], $params[2], $params[3], $params[4]);
- default:
- return call_user_func_array($func, $params);
- }
+ return call_user_func_array($func, $params);
}
/**
@@ -179,37 +164,8 @@ public static function callFunction($func, array &$params = array()) {
*/
public static function invokeMethod($func, array &$params = array()) {
list($class, $method) = $func;
-
$instance = is_object($class);
-
- switch (count($params)) {
- case 0:
- return ($instance) ?
- $class->$method() :
- $class::$method();
- case 1:
- return ($instance) ?
- $class->$method($params[0]) :
- $class::$method($params[0]);
- case 2:
- return ($instance) ?
- $class->$method($params[0], $params[1]) :
- $class::$method($params[0], $params[1]);
- case 3:
- return ($instance) ?
- $class->$method($params[0], $params[1], $params[2]) :
- $class::$method($params[0], $params[1], $params[2]);
- case 4:
- return ($instance) ?
- $class->$method($params[0], $params[1], $params[2], $params[3]) :
- $class::$method($params[0], $params[1], $params[2], $params[3]);
- case 5:
- return ($instance) ?
- $class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) :
- $class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
- default:
- return call_user_func_array($func, $params);
- }
+ return call_user_func_array($func, $params);
}
/**
View
19 flight/core/Loader.php
@@ -111,23 +111,8 @@ public function getInstance($class, array $params = array()) {
* @return object Class instance
*/
public function newInstance($class, array $params = array()) {
- switch (count($params)) {
- case 0:
- return new $class();
- case 1:
- return new $class($params[0]);
- case 2:
- return new $class($params[0], $params[1]);
- case 3:
- return new $class($params[0], $params[1], $params[2]);
- case 4:
- return new $class($params[0], $params[1], $params[2], $params[3]);
- case 5:
- return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
- default:
- $refClass = new \ReflectionClass($class);
- return $refClass->newInstanceArgs($params);
- }
+ $refClass = new \ReflectionClass($class);
+ return $refClass->newInstanceArgs($params);
}
/**
Something went wrong with that request. Please try again.