Commit 3456ae0a authored by crell's avatar crell Committed by Benni Mack
Browse files

[TASK] Remove usages of call_user_func() and call_user_func_array()

Using direct calls and variadics is slightly faster, and usually cleaner to read.

I left in the instant-call call_user_func() instances in the entry point files, as
that's only a single call and is arguably at least no worse than (function() {})().

Resolves: #94060
Releases: master
Change-Id: I95988feadf8522d8d936a273608e0ef2d5ce75b5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69017


Tested-by: Benjamin Franzke's avatarBenjamin Franzke <bfr@qbus.de>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Benjamin Franzke's avatarBenjamin Franzke <bfr@qbus.de>
Reviewed-by: crell's avatarcrell <larry@garfieldtech.com>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 9fd59c58
......@@ -60,7 +60,7 @@ class RouteDispatcher extends Dispatcher
$targetIdentifier = $route->getOption('target');
$target = $this->getCallableFromTarget($targetIdentifier);
$arguments = [$request];
return call_user_func_array($target, $arguments);
return $target(...$arguments);
}
/**
......
......@@ -155,7 +155,7 @@ class PageTreeRepository
return;
}
foreach ($tree['_children'] as $k => &$childPage) {
if (!call_user_func_array($callback, [$childPage])) {
if (!$callback($childPage)) {
unset($tree['_children'][$k]);
continue;
}
......
......@@ -882,7 +882,7 @@ class FlexFormTools
*/
protected function executeCallBackMethod($methodName, array $parameterArray)
{
return call_user_func_array([$this->callBackObj, $methodName], $parameterArray);
return $this->callBackObj->$methodName(...$parameterArray);
}
/***********************************
......
......@@ -110,7 +110,7 @@ class CommandApplication implements ApplicationInterface
$exitCode = $this->application->run($input, $output);
if ($execute !== null) {
call_user_func($execute);
$execute();
}
exit($exitCode);
......
......@@ -6182,7 +6182,7 @@ class DataHandler implements LoggerAwareInterface
if (!empty($remapAction['func'])) {
$callable = [$this, $remapAction['func']];
if (is_callable($callable)) {
$newValue = call_user_func_array($callable, $remapAction['args']);
$newValue = $callable(...$remapAction['args']);
}
}
// If array is returned, check for maxitems condition, if string is returned this was already done:
......@@ -6252,7 +6252,7 @@ class DataHandler implements LoggerAwareInterface
if ($this->remapStackActions) {
foreach ($this->remapStackActions as $action) {
if (isset($action['callback'], $action['arguments'])) {
call_user_func_array($action['callback'], $action['arguments']);
$action['callback'](...$action['arguments']);
}
}
}
......
......@@ -382,7 +382,7 @@ class SlugHelper
$newValue = $slug;
$counter = 0;
while (
!call_user_func($isUnique, $newValue, $state)
!$isUnique($newValue, $state)
&& ++$counter < 100
) {
$newValue = $this->sanitize($rawValue . '-' . $counter);
......
......@@ -1409,7 +1409,7 @@ class RelationHandler
$purgedItemIds = [];
$callable =[$this, $purgeCallback];
if (is_callable($callable)) {
$purgedItemIds = call_user_func($callable, $itemTableName, $itemIds);
$purgedItemIds = $callable($itemTableName, $itemIds);
}
$removedItemIds = array_diff($itemIds, $purgedItemIds);
......
......@@ -97,7 +97,7 @@ abstract class AbstractApplication implements ApplicationInterface, RequestHandl
ServerRequestFactory::fromGlobals()
);
if ($execute !== null) {
call_user_func($execute);
$execute();
}
} catch (ImmediateResponseException $exception) {
$response = $exception->getResponse();
......
......@@ -50,12 +50,12 @@ class Dispatcher implements DispatcherInterface
$targetIdentifier = $request->getAttribute('target');
$target = $this->getCallableFromTarget($targetIdentifier);
$arguments = [$request];
return call_user_func_array($target, $arguments);
return $target(...$arguments);
}
/**
* Creates a callable out of the given parameter, which can be a string, a callable / closure or an array
* which can be handed to call_user_func_array()
* which can be invoked as a function.
*
* @param array|string|callable $target the target which is being resolved.
* @return callable
......
......@@ -422,9 +422,9 @@ class LocalDriver extends AbstractHierarchicalFilesystemDriver implements Stream
{
foreach ($filterMethods as $filter) {
if (is_callable($filter)) {
$result = call_user_func($filter, $itemName, $itemIdentifier, $parentIdentifier, [], $this);
// We have to use -1 as the don't include“ return value, as call_user_func() will return FALSE
// If calling the method succeeded and thus we can't use that as a return value.
$result = $filter($itemName, $itemIdentifier, $parentIdentifier, [], $this);
// We use -1 as the "don't include“ return value, for historic reasons,
// as call_user_func() used to return FALSE if calling the method failed.
if ($result === -1) {
return false;
}
......
......@@ -84,8 +84,8 @@ class FileExtensionFilter
/**
* Entry method for use as filelist filter.
*
* We have to use -1 as the don't include“ return value, as call_user_func() will return FALSE
* if calling the method failed and thus we can't use that as a return value.
* We use -1 as the "don't include“ return value, for historic reasons,
* as call_user_func() used to return FALSE if calling the method failed.
*
* @param string $itemName
* @param string $itemIdentifier
......
......@@ -32,8 +32,8 @@ class FileNameFilter
/**
* Filter method that checks if a file/folder name starts with a dot (e.g. .htaccess)
*
* We have to use -1 as the don't include“ return value, as call_user_func() will return FALSE
* If calling the method succeeded and thus we can't use that as a return value.
* We use -1 as the "don't include“ return value, for historic reasons,
* as call_user_func() used to return FALSE if calling the method failed.
*
* @param string $itemName
* @param string $itemIdentifier
......
......@@ -142,8 +142,8 @@ class DriverFilteredSearchResult implements FileSearchResultInterface
continue;
}
$result = $filter($itemName, $itemIdentifier, $parentIdentifier, [], $this->driver);
// We have to use -1 as the don't include“ return value, as call_user_func() will return FALSE
// If calling the method succeeded and thus we can't use that as a return value.
// We use -1 as the "don't include“ return value, for historic reasons,
// as call_user_func() used to return FALSE if calling the method failed.
if ($result === -1) {
$matches = false;
}
......
......@@ -698,7 +698,7 @@ class ArrayUtility
$keepItems = array_flip($keepItems);
foreach ($array as $key => $value) {
// Get the value to compare by using the callback function:
$keepValue = isset($getValueFunc) ? call_user_func($getValueFunc, $value) : $value;
$keepValue = isset($getValueFunc) ? $getValueFunc($value) : $value;
if (!isset($keepItems[$keepValue])) {
unset($array[$key]);
}
......@@ -862,7 +862,7 @@ class ArrayUtility
$array[$key] = self::filterRecursive($value, $callback);
}
if (!call_user_func($callback, $value)) {
if (!$callback($value)) {
unset($array[$key]);
}
}
......
......@@ -623,15 +623,12 @@ class GeneralUtility
$ipad = str_repeat(chr(54), $hashBlocksize);
if (strlen($secret) > $hashBlocksize) {
// Keys longer than block size are shorten
$key = str_pad(pack('H*', call_user_func($hashAlgorithm, $secret)), $hashBlocksize, "\0");
$key = str_pad(pack('H*', $hashAlgorithm($secret)), $hashBlocksize, "\0");
} else {
// Keys shorter than block size are zero-padded
$key = str_pad($secret, $hashBlocksize, "\0");
}
$hmac = call_user_func($hashAlgorithm, ($key ^ $opad) . pack('H*', call_user_func(
$hashAlgorithm,
($key ^ $ipad) . $input
)));
$hmac = $hashAlgorithm(($key ^ $opad) . pack('H*', $hashAlgorithm(($key ^ $ipad) . $input)));
}
return $hmac;
}
......
......@@ -113,7 +113,7 @@ class ErrorHandlerTest extends FunctionalTestCase
{
// process errors
if ($this->existingHandler !== null) {
return call_user_func($this->existingHandler, $code, $message, $file, $line, $context);
return ($this->existingHandler)($code, $message, $file, $line, $context);
}
return false;
......
......@@ -213,7 +213,7 @@ class PharStreamWrapperInterceptorTest extends FunctionalTestCase
self::assertSame(
$expectation,
call_user_func($functionName, 'phar://' . $path)
$functionName('phar://' . $path)
);
}
......@@ -282,7 +282,7 @@ class PharStreamWrapperInterceptorTest extends FunctionalTestCase
self::expectExceptionCode(1539625084);
$path = $this->instancePath . '/' . $path;
call_user_func($functionName, 'phar://' . $path);
$functionName('phar://' . $path);
}
/**
......
......@@ -90,8 +90,8 @@ class FormProtectionFactoryTest extends UnitTestCase
$this->createMock(Registry::class)
];
self::assertSame(
call_user_func_array([FormProtectionFactory::class, 'get'], $arguments),
call_user_func_array([FormProtectionFactory::class, 'get'], $arguments)
FormProtectionFactory::get(...$arguments),
FormProtectionFactory::get(...$arguments)
);
}
......
......@@ -159,7 +159,7 @@ class LoggerTest extends UnitTestCase
$logger = new Logger('test.core.log');
$writer = new WriterFixture();
$logger->addWriter(LogLevel::DEBUG, $writer);
call_user_func([$logger, $shorthandMethod], 'test message');
$logger->$shorthandMethod('test message');
self::assertNotEmpty($writer->getRecords());
}
......
......@@ -74,7 +74,7 @@ class FileTest extends UnitTestCase
];
$fixture = new File($properties, $this->storageMock);
foreach ($properties as $key => $value) {
self::assertEquals($value, call_user_func([$fixture, 'get' . $key]));
self::assertEquals($value, $fixture->{'get' . $key}());
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment