Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
joomlatools/joomlatools-framework#395: Use argument unpacking instead…
Browse files Browse the repository at this point in the history
… of call_user_func
  • Loading branch information
ercanozkaya committed Feb 9, 2021
1 parent 20b9d90 commit 96870b7
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 181 deletions.
23 changes: 2 additions & 21 deletions code/database/rowset/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,27 +832,8 @@ public function __call($method, $arguments)
}
}

if($row = $this->getIterator()->current())
{
// Call_user_func_array is ~3 times slower than direct method calls.
switch (count($arguments))
{
case 0 :
$result = $row->$method();
break;
case 1 :
$result = $row->$method($arguments[0]);
break;
case 2 :
$result = $row->$method($arguments[0], $arguments[1]);
break;
case 3 :
$result = $row->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array($row, $method), $arguments);
}
if($row = $this->getIterator()->current()) {
$result = $row->$method(...$arguments);
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion code/event/subscriber/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function registerSubscriber($identifier, array $config = array())

if (!isset($this->__subscribers[(string)$identifier]))
{
$listeners = call_user_func(array($class, 'getEventListeners'));/*$class::getEventListeners();*/
$listeners = $class::getEventListeners();

if (!empty($listeners))
{
Expand Down
25 changes: 2 additions & 23 deletions code/kodekit.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,9 @@ public static function isCache()
*/
public static function __callStatic($method, $arguments)
{
if(self::$__object_manager instanceof Library\ObjectManager)
{
// Call_user_func_array is ~3 times slower than direct method calls.
switch (count($arguments))
{
case 0 :
$result = self::$__object_manager->$method();
break;
case 1 :
$result = self::$__object_manager->$method($arguments[0]);
break;
case 2 :
$result = self::$__object_manager->$method($arguments[0], $arguments[1]);
break;
case 3 :
$result = self::$__object_manager->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array(self::$__object_manager, $method), $arguments);
}
if(self::$__object_manager instanceof Library\ObjectManager) {
return self::$__object_manager->$method(...$arguments);
}
else throw new \BadMethodCallException('Cannot call method: $s. Kodekit has not been instantiated', $method);

return $result;
}
}
27 changes: 2 additions & 25 deletions code/model/composite/decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,31 +458,8 @@ public function __call($method, $arguments)
$entity = $this->fetch();

//Call the method if it exists
if (!method_exists($model, $method) && is_callable(array($entity, $method)))
{
$result = null;

// Call_user_func_array is ~3 times slower than direct method calls.
switch (count($arguments))
{
case 0 :
$result = $entity->$method();
break;
case 1 :
$result = $entity->$method($arguments[0]);
break;
case 2:
$result = $entity->$method($arguments[0], $arguments[1]);
break;
case 3:
$result = $entity->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array($entity, $method), $arguments);
}

return $result;
if (!method_exists($model, $method) && is_callable(array($entity, $method))) {
return $entity->$method(...$arguments);
}

return parent::__call($method, $arguments);
Expand Down
23 changes: 2 additions & 21 deletions code/model/entity/composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,27 +715,8 @@ public function __call($method, $arguments)
{
$result = null;

if($entity = $this->getIterator()->current())
{
// Call_user_func_array is ~3 times slower than direct method calls.
switch (count($arguments))
{
case 0 :
$result = $entity->$method();
break;
case 1 :
$result = $entity->$method($arguments[0]);
break;
case 2 :
$result = $entity->$method($arguments[0], $arguments[1]);
break;
case 3 :
$result = $entity->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array($entity, $method), $arguments);
}
if($entity = $this->getIterator()->current()) {
$result = $entity->$method(...$arguments);
}

return $result;
Expand Down
39 changes: 2 additions & 37 deletions code/object/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,24 +390,7 @@ public function __call($method, $arguments)
if ($this->__mixed_methods[$method] instanceof \Closure)
{
$closure = $this->__mixed_methods[$method];

switch (count($arguments)) {
case 0 :
$result = $closure();
break;
case 1 :
$result = $closure($arguments[0]);
break;
case 2 :
$result = $closure($arguments[0], $arguments[1]);
break;
case 3 :
$result = $closure($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array($closure, $arguments);
}
$result = $closure(...$arguments);
}
elseif(is_object($this->__mixed_methods[$method]))
{
Expand All @@ -416,25 +399,7 @@ public function __call($method, $arguments)
//Switch the mixin's attached mixer
$mixin->setMixer($this);

// Call_user_func_array is ~3 times slower than direct method calls.
switch (count($arguments))
{
case 0 :
$result = $mixin->$method();
break;
case 1 :
$result = $mixin->$method($arguments[0]);
break;
case 2 :
$result = $mixin->$method($arguments[0], $arguments[1]);
break;
case 3 :
$result = $mixin->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array($mixin, $method), $arguments);
}
$result = $mixin->$method(...$arguments);
}
else $result = $this->__mixed_methods[$method];

Expand Down
22 changes: 1 addition & 21 deletions code/object/decorator/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,7 @@ public function __call($method, $arguments)
//Call the method if it exists
if ($exists)
{
$result = null;

// Call_user_func_array is ~3 times slower than direct method calls.
switch (count($arguments))
{
case 0 :
$result = $delegate->$method();
break;
case 1 :
$result = $delegate->$method($arguments[0]);
break;
case 2:
$result = $delegate->$method($arguments[0], $arguments[1]);
break;
case 3:
$result = $delegate->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array($delegate, $method), $arguments);
}
$result = $delegate->$method(...$arguments);

//Allow for method chaining through the decorator
$class = get_class($delegate);
Expand Down
25 changes: 2 additions & 23 deletions code/object/mixin/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,8 @@ public function __call($method, $arguments)
$mixer = $this->getMixer();

//Make sure we don't end up in a recursive loop
if(isset($mixer) && !($mixer instanceof $this))
{
// Call_user_func_array is ~3 times slower than direct method calls.
switch(count($arguments))
{
case 0 :
$result = $mixer->$method();
break;
case 1 :
$result = $mixer->$method($arguments[0]);
break;
case 2:
$result = $mixer->$method($arguments[0], $arguments[1]);
break;
case 3:
$result = $mixer->$method($arguments[0], $arguments[1], $arguments[2]);
break;
default:
// Resort to using call_user_func_array for many segments
$result = call_user_func_array(array($mixer, $method), $arguments);
}

return $result;
if(isset($mixer) && !($mixer instanceof $this)) {
return $mixer->$method(...$arguments);
}

throw new \BadMethodCallException('Call to undefined method :'.$method);
Expand Down
6 changes: 3 additions & 3 deletions code/template/engine/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function registerEngine($identifier, array $config = array())
);
}

$types = call_user_func(array($class, 'getFileTypes'));/*$class::getFileTypes();*/
$types = $class::getFileTypes();

if (!empty($types))
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public function unregisterEngine($identifier)
);
}

$types = call_user_func(array($class, 'getFileTypes'));/*$class::getFileTypes();*/
$types = $class::getFileTypes();

}
else $types = (array) $identifier;
Expand Down Expand Up @@ -284,7 +284,7 @@ public function isRegistered($identifier)
);
}

$types = call_user_func(array($class, 'getFileTypes'));/*$class::getFileTypes();*/
$types = $class::getFileTypes();
}
else $types = (array) $identifier;

Expand Down
2 changes: 1 addition & 1 deletion code/translator/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static function getInstance(ObjectConfigInterface $config, ObjectManagerI
{
$class = $manager->getClass('lib:translator.cache');

if(call_user_func(array($class, 'isSupported'))/*$class::isSupported()*/)
if($class::isSupported())
{
$instance = $instance->decorate('lib:translator.cache');
$instance->setNamespace($config->cache_namespace);
Expand Down
6 changes: 1 addition & 5 deletions code/user/session/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ public function __construct(ObjectConfig $config)
parent::__construct($config);

//Session write and close handlers are called after destructing objects since PHP 5.0.5.
if (version_compare(phpversion(), '5.4.0', '>=')) {
session_register_shutdown();
} else {
register_shutdown_function('session_write_close');
}
session_register_shutdown();

//Only configure the session if it's not active yet
if(!$this->isActive())
Expand Down

0 comments on commit 96870b7

Please sign in to comment.