From 8085c58a4b675c0c11c6454bdf15b3ba61022283 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Mon, 4 Jun 2018 23:24:34 -0700 Subject: [PATCH] Replace call_user_func_array(), part 1 Uses new PHP 5.6 syntax like ...parameter unpacking and calling anything looking like a callback to make the code more readable. There are much more occurrences but this commit is intentionally limited to an easily reviewable size. Change-Id: Idcec077ef3fdf029b632cceafd0150851ad723e3 --- includes/EditPage.php | 2 +- includes/GlobalFunctions.php | 19 +++--------- includes/actions/Action.php | 4 +-- includes/api/ApiBase.php | 11 +++---- includes/auth/AuthManager.php | 6 ++-- .../LegacyHookPreAuthenticationProvider.php | 2 +- includes/cache/CacheDependency.php | 2 +- includes/cache/CacheHelper.php | 4 +-- includes/changes/CategoryMembershipChange.php | 29 +++++++++---------- includes/changes/ChangesListBooleanFilter.php | 21 ++++++-------- .../ChangesListStringOptionsFilterGroup.php | 23 +++++++-------- includes/context/ContextSource.php | 2 +- includes/context/DerivativeContext.php | 2 +- includes/context/RequestContext.php | 2 +- includes/debug/logger/MonologSpi.php | 2 +- includes/deferred/AutoCommitUpdate.php | 2 +- includes/diff/DifferenceEngine.php | 5 +--- includes/filerepo/FileRepo.php | 4 +-- includes/filerepo/ForeignDBRepo.php | 3 +- includes/filerepo/LocalRepo.php | 4 +-- includes/installer/CliInstaller.php | 2 +- 21 files changed, 62 insertions(+), 89 deletions(-) diff --git a/includes/EditPage.php b/includes/EditPage.php index 13d1623d08..22c29d6413 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -3541,7 +3541,7 @@ ERROR; // Allow for site and per-namespace customization of contribution/copyright notice. Hooks::run( 'EditPageCopyrightWarning', [ $title, &$copywarnMsg ] ); - $msg = call_user_func_array( 'wfMessage', $copywarnMsg )->title( $title ); + $msg = wfMessage( ...$copywarnMsg )->title( $title ); if ( $langcode ) { $msg->inLanguage( $langcode ); } diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index ae09602995..6b4e4ee4df 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2196,9 +2196,7 @@ function wfStringToBool( $val ) { * @deprecated since 1.30 use MediaWiki\Shell::escape() */ function wfEscapeShellArg( /*...*/ ) { - $args = func_get_args(); - - return call_user_func_array( Shell::class . '::escape', $args ); + return Shell::escape( ...func_get_args() ); } /** @@ -2697,10 +2695,7 @@ function wfMakeStaticArrayFile( array $data, $header = 'Automatically generated' * @return string */ function wfMemcKey( /*...*/ ) { - return call_user_func_array( - [ ObjectCache::getLocalClusterInstance(), 'makeKey' ], - func_get_args() - ); + return ObjectCache::getLocalClusterInstance()->makeKey( ...func_get_args() ); } /** @@ -2716,10 +2711,7 @@ function wfMemcKey( /*...*/ ) { function wfForeignMemcKey( $db, $prefix /*...*/ ) { $args = array_slice( func_get_args(), 2 ); $keyspace = $prefix ? "$db-$prefix" : $db; - return call_user_func_array( - [ ObjectCache::getLocalClusterInstance(), 'makeKeyInternal' ], - [ $keyspace, $args ] - ); + return ObjectCache::getLocalClusterInstance()->makeKeyInternal( $keyspace, $args ); } /** @@ -2735,10 +2727,7 @@ function wfForeignMemcKey( $db, $prefix /*...*/ ) { * @return string */ function wfGlobalCacheKey( /*...*/ ) { - return call_user_func_array( - [ ObjectCache::getLocalClusterInstance(), 'makeGlobalKey' ], - func_get_args() - ); + return ObjectCache::getLocalClusterInstance()->makeGlobalKey( ...func_get_args() ); } /** diff --git a/includes/actions/Action.php b/includes/actions/Action.php index e8d9a3e402..fb22445410 100644 --- a/includes/actions/Action.php +++ b/includes/actions/Action.php @@ -104,7 +104,7 @@ abstract class Action implements MessageLocalizer { } if ( is_callable( $classOrCallable ) ) { - return call_user_func_array( $classOrCallable, [ $page, $context ] ); + return $classOrCallable( $page, $context ); } return $classOrCallable; @@ -255,7 +255,7 @@ abstract class Action implements MessageLocalizer { */ final public function msg( $key ) { $params = func_get_args(); - return call_user_func_array( [ $this->getContext(), 'msg' ], $params ); + return $this->getContext()->msg( ...$params ); } /** diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 8a99e6a97f..98aa554b1d 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -1819,7 +1819,7 @@ abstract class ApiBase extends ContextSource { if ( is_string( $msg ) ) { $msg = wfMessage( $msg ); } elseif ( is_array( $msg ) ) { - $msg = call_user_func_array( 'wfMessage', $msg ); + $msg = wfMessage( ...$msg ); } if ( !$msg instanceof Message ) { return null; @@ -1866,7 +1866,7 @@ abstract class ApiBase extends ContextSource { [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ] ) ); } else { - call_user_func_array( [ $status, 'fatal' ], (array)$error ); + $status->fatal( ...(array)$error ); } } return $status; @@ -2045,10 +2045,7 @@ abstract class ApiBase extends ContextSource { if ( !$status->getErrorsByType( 'error' ) ) { $newStatus = Status::newGood(); foreach ( $status->getErrorsByType( 'warning' ) as $err ) { - call_user_func_array( - [ $newStatus, 'fatal' ], - array_merge( [ $err['message'] ], $err['params'] ) - ); + $newStatus->fatal( $err['message'], ...$err['params'] ); } if ( !$newStatus->getErrorsByType( 'error' ) ) { $newStatus->fatal( 'unknownerror-nocode' ); @@ -2085,7 +2082,7 @@ abstract class ApiBase extends ContextSource { $user = $this->getUser(); } $rights = (array)$rights; - if ( !call_user_func_array( [ $user, 'isAllowedAny' ], $rights ) ) { + if ( !$user->isAllowedAny( ...$rights ) ) { $this->dieWithError( [ 'apierror-permissiondenied', $this->msg( "action-{$rights[0]}" ) ] ); } } diff --git a/includes/auth/AuthManager.php b/includes/auth/AuthManager.php index 2adc00efae..161dd56320 100644 --- a/includes/auth/AuthManager.php +++ b/includes/auth/AuthManager.php @@ -240,7 +240,7 @@ class AuthManager implements LoggerAwareInterface { global $wgAuth; if ( $wgAuth && !$wgAuth instanceof AuthManagerAuthPlugin ) { - return call_user_func_array( [ $wgAuth, $method ], $params ); + return $wgAuth->$method( ...$params ); } else { return $return; } @@ -985,7 +985,7 @@ class AuthManager implements LoggerAwareInterface { if ( $permErrors ) { $status = Status::newGood(); foreach ( $permErrors as $args ) { - call_user_func_array( [ $status, 'fatal' ], $args ); + $status->fatal( ...$args ); } return $status; } @@ -2427,7 +2427,7 @@ class AuthManager implements LoggerAwareInterface { $providers += $this->getSecondaryAuthenticationProviders(); } foreach ( $providers as $provider ) { - call_user_func_array( [ $provider, $method ], $args ); + $provider->$method( ...$args ); } } diff --git a/includes/auth/LegacyHookPreAuthenticationProvider.php b/includes/auth/LegacyHookPreAuthenticationProvider.php index 95fe3ab852..e8a276cc2d 100644 --- a/includes/auth/LegacyHookPreAuthenticationProvider.php +++ b/includes/auth/LegacyHookPreAuthenticationProvider.php @@ -168,7 +168,7 @@ class LegacyHookPreAuthenticationProvider extends AbstractPreAuthenticationProvi case LoginForm::USER_MIGRATED: $error = $msg ?: 'login-migrated-generic'; - return call_user_func_array( 'StatusValue::newFatal', (array)$error ); + return StatusValue::newFatal( ...(array)$error ); // @codeCoverageIgnoreStart case LoginForm::CREATE_BLOCKED: // Can never happen diff --git a/includes/cache/CacheDependency.php b/includes/cache/CacheDependency.php index 4ff10047a4..11df5bcffc 100644 --- a/includes/cache/CacheDependency.php +++ b/includes/cache/CacheDependency.php @@ -118,7 +118,7 @@ class DependencyWrapper { if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) { $value = $obj->value; } elseif ( $callback ) { - $value = call_user_func_array( $callback, $callbackParams ); + $value = $callback( ...$callbackParams ); # Cache the newly-generated value $wrapper = new DependencyWrapper( $value, $deps ); $wrapper->storeToCache( $cache, $key, $expiry ); diff --git a/includes/cache/CacheHelper.php b/includes/cache/CacheHelper.php index b2a91c2974..9db8166d50 100644 --- a/includes/cache/CacheHelper.php +++ b/includes/cache/CacheHelper.php @@ -294,7 +294,7 @@ class CacheHelper implements ICacheHelper { $args = [ $args ]; } - $value = call_user_func_array( $computeFunction, $args ); + $value = $computeFunction( ...$args ); if ( $this->cacheEnabled ) { if ( is_null( $key ) ) { @@ -350,7 +350,7 @@ class CacheHelper implements ICacheHelper { throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' ); } - return call_user_func_array( 'wfMemcKey', $this->cacheKey ); + return wfMemcKey( ...$this->cacheKey ); } /** diff --git a/includes/changes/CategoryMembershipChange.php b/includes/changes/CategoryMembershipChange.php index f095b64f82..a49fb4c727 100644 --- a/includes/changes/CategoryMembershipChange.php +++ b/includes/changes/CategoryMembershipChange.php @@ -187,22 +187,19 @@ class CategoryMembershipChange { } /** @var RecentChange $rc */ - $rc = call_user_func_array( - $this->newForCategorizationCallback, - [ - $timestamp, - $categoryTitle, - $user, - $comment, - $pageTitle, - $lastRevId, - $newRevId, - $lastTimestamp, - $bot, - $ip, - $deleted, - $added - ] + $rc = ( $this->newForCategorizationCallback )( + $timestamp, + $categoryTitle, + $user, + $comment, + $pageTitle, + $lastRevId, + $newRevId, + $lastTimestamp, + $bot, + $ip, + $deleted, + $added ); $rc->save(); } diff --git a/includes/changes/ChangesListBooleanFilter.php b/includes/changes/ChangesListBooleanFilter.php index f37ed2dd6d..fc37882c02 100644 --- a/includes/changes/ChangesListBooleanFilter.php +++ b/includes/changes/ChangesListBooleanFilter.php @@ -206,18 +206,15 @@ class ChangesListBooleanFilter extends ChangesListFilter { return; } - call_user_func_array( - $this->queryCallable, - [ - get_class( $specialPage ), - $specialPage->getContext(), - $dbr, - &$tables, - &$fields, - &$conds, - &$query_options, - &$join_conds - ] + ( $this->queryCallable )( + get_class( $specialPage ), + $specialPage->getContext(), + $dbr, + $tables, + $fields, + $conds, + $query_options, + $join_conds ); } diff --git a/includes/changes/ChangesListStringOptionsFilterGroup.php b/includes/changes/ChangesListStringOptionsFilterGroup.php index 8cd7ba8dea..e06f0817ba 100644 --- a/includes/changes/ChangesListStringOptionsFilterGroup.php +++ b/includes/changes/ChangesListStringOptionsFilterGroup.php @@ -200,19 +200,16 @@ class ChangesListStringOptionsFilterGroup extends ChangesListFilterGroup { sort( $selectedValues ); - call_user_func_array( - $this->queryCallable, - [ - get_class( $specialPage ), - $specialPage->getContext(), - $dbr, - &$tables, - &$fields, - &$conds, - &$query_options, - &$join_conds, - $selectedValues - ] + ( $this->queryCallable )( + get_class( $specialPage ), + $specialPage->getContext(), + $dbr, + $tables, + $fields, + $conds, + $query_options, + $join_conds, + $selectedValues ); } diff --git a/includes/context/ContextSource.php b/includes/context/ContextSource.php index 03fb9e2bbf..618253859d 100644 --- a/includes/context/ContextSource.php +++ b/includes/context/ContextSource.php @@ -168,7 +168,7 @@ abstract class ContextSource implements IContextSource { public function msg( $key /* $args */ ) { $args = func_get_args(); - return call_user_func_array( [ $this->getContext(), 'msg' ], $args ); + return $this->getContext()->msg( ...$args ); } /** diff --git a/includes/context/DerivativeContext.php b/includes/context/DerivativeContext.php index f7a1815d02..acf6fcb9fc 100644 --- a/includes/context/DerivativeContext.php +++ b/includes/context/DerivativeContext.php @@ -296,6 +296,6 @@ class DerivativeContext extends ContextSource implements MutableContext { public function msg( $key ) { $args = func_get_args(); - return call_user_func_array( 'wfMessage', $args )->setContext( $this ); + return wfMessage( ...$args )->setContext( $this ); } } diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index db3a7a9695..7563330697 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -423,7 +423,7 @@ class RequestContext implements IContextSource, MutableContext { public function msg( $key ) { $args = func_get_args(); - return call_user_func_array( 'wfMessage', $args )->setContext( $this ); + return wfMessage( ...$args )->setContext( $this ); } /** diff --git a/includes/debug/logger/MonologSpi.php b/includes/debug/logger/MonologSpi.php index 670ba391e7..ff653ab241 100644 --- a/includes/debug/logger/MonologSpi.php +++ b/includes/debug/logger/MonologSpi.php @@ -199,7 +199,7 @@ class MonologSpi implements Spi { if ( isset( $spec['calls'] ) ) { foreach ( $spec['calls'] as $method => $margs ) { - call_user_func_array( [ $obj, $method ], $margs ); + $obj->$method( ...$margs ); } } diff --git a/includes/deferred/AutoCommitUpdate.php b/includes/deferred/AutoCommitUpdate.php index f9297af584..85071576b0 100644 --- a/includes/deferred/AutoCommitUpdate.php +++ b/includes/deferred/AutoCommitUpdate.php @@ -39,7 +39,7 @@ class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback { try { /** @var Exception $e */ $e = null; - call_user_func_array( $this->callback, [ $this->dbw, $this->fname ] ); + ( $this->callback )( $this->dbw, $this->fname ); } catch ( Exception $e ) { } if ( $autoTrx ) { diff --git a/includes/diff/DifferenceEngine.php b/includes/diff/DifferenceEngine.php index b1ca435269..25ba36ac10 100644 --- a/includes/diff/DifferenceEngine.php +++ b/includes/diff/DifferenceEngine.php @@ -756,10 +756,7 @@ class DifferenceEngine extends ContextSource { // for backwards-compatibility $key = $this->getDiffBodyCacheKey(); if ( $key === null ) { - $key = call_user_func_array( - [ $cache, 'makeKey' ], - $this->getDiffBodyCacheKeyParams() - ); + $key = $cache->makeKey( ...$this->getDiffBodyCacheKeyParams() ); } // Try cache diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 89bb81a7c0..70068b998b 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -1735,7 +1735,7 @@ class FileRepo { * @return Status */ public function newFatal( $message /*, parameters...*/ ) { - $status = call_user_func_array( [ Status::class, 'newFatal' ], func_get_args() ); + $status = Status::newFatal( ...func_get_args() ); $status->cleanCallback = $this->getErrorCleanupFunction(); return $status; @@ -1840,7 +1840,7 @@ class FileRepo { $args = func_get_args(); array_unshift( $args, 'filerepo', $this->getName() ); - return call_user_func_array( 'wfMemcKey', $args ); + return wfMemcKey( ...$args ); } /** diff --git a/includes/filerepo/ForeignDBRepo.php b/includes/filerepo/ForeignDBRepo.php index 7879448bb6..4b331389b7 100644 --- a/includes/filerepo/ForeignDBRepo.php +++ b/includes/filerepo/ForeignDBRepo.php @@ -132,9 +132,8 @@ class ForeignDBRepo extends LocalRepo { function getSharedCacheKey( /*...*/ ) { if ( $this->hasSharedCache() ) { $args = func_get_args(); - array_unshift( $args, $this->dbName, $this->tablePrefix ); - return call_user_func_array( 'wfForeignMemcKey', $args ); + return wfForeignMemcKey( $this->dbName, $this->tablePrefix, ...$args ); } else { return false; } diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index 03a9d44168..9a6ef22f17 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -507,7 +507,7 @@ class LocalRepo extends FileRepo { function getSharedCacheKey( /*...*/ ) { $args = func_get_args(); - return call_user_func_array( 'wfMemcKey', $args ); + return wfMemcKey( ...$args ); } /** @@ -590,7 +590,7 @@ class LocalRepo extends FileRepo { wfDebug( __METHOD__ . ": skipped because storage uses sha1 paths\n" ); return Status::newGood(); } else { - return call_user_func_array( 'parent::' . $function, $args ); + return parent::$function( ...$args ); } } } diff --git a/includes/installer/CliInstaller.php b/includes/installer/CliInstaller.php index 2264b80ddb..845408abd7 100644 --- a/includes/installer/CliInstaller.php +++ b/includes/installer/CliInstaller.php @@ -197,7 +197,7 @@ class CliInstaller extends Installer { if ( count( $warnings ) !== 0 ) { foreach ( $warnings as $w ) { - call_user_func_array( [ $this, 'showMessage' ], $w ); + $this->showMessage( ...$w ); } } -- 2.20.1