From 6e956d55aac4e4d7e6145ed51c44311ca0e9b0c7 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Thu, 7 Jun 2018 19:58:35 -0700 Subject: [PATCH] Replace call_user_func_array(), part 2 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. In one occurrence, a simple conditional instead of trickery was much more readable. This patch finishes all the easy stuf in the core, the remainder is either unobvious or would result in smaller readability gains. It will be carefully dealt with in further commits. Change-Id: I79a16c48bfb98b75e5b99f2f6f4fa07b3ae02c5b --- includes/MergeHistory.php | 2 +- includes/MovePage.php | 2 +- includes/OutputPage.php | 4 ++-- includes/Storage/RevisionRecord.php | 2 +- includes/api/ApiQueryLinks.php | 2 +- includes/api/ApiQueryTokens.php | 2 +- includes/api/ApiQueryUserContribs.php | 8 +++++--- includes/auth/ButtonAuthenticationRequest.php | 4 ++-- includes/exception/MWExceptionHandler.php | 4 +--- includes/htmlform/HTMLForm.php | 2 +- includes/installer/WebInstaller.php | 4 ++-- includes/libs/StatusValue.php | 2 +- includes/libs/filebackend/FileBackend.php | 2 +- includes/libs/lockmanager/MemcLockManager.php | 4 ++-- includes/libs/lockmanager/RedisLockManager.php | 4 ++-- includes/logging/LogEventsList.php | 2 +- includes/media/BitmapHandler.php | 2 +- includes/parser/Parser.php | 2 +- includes/parser/ParserOutput.php | 8 ++++---- includes/resourceloader/ResourceLoaderContext.php | 2 +- includes/skins/BaseTemplate.php | 2 +- includes/specialpage/AuthManagerSpecialPage.php | 4 ++-- includes/specialpage/ChangesListSpecialPage.php | 5 ++--- includes/specialpage/SpecialPage.php | 5 +---- includes/specials/SpecialBlock.php | 2 +- includes/upload/UploadBase.php | 4 ++-- includes/upload/UploadFromChunks.php | 8 ++++---- maintenance/backup.inc | 2 +- maintenance/storage/recompressTracked.php | 6 +++--- 29 files changed, 49 insertions(+), 53 deletions(-) diff --git a/includes/MergeHistory.php b/includes/MergeHistory.php index 0e9bb4673d..4c655ebab5 100644 --- a/includes/MergeHistory.php +++ b/includes/MergeHistory.php @@ -167,7 +167,7 @@ class MergeHistory { // Convert into a Status object if ( $errors ) { foreach ( $errors as $error ) { - call_user_func_array( [ $status, 'fatal' ], $error ); + $status->fatal( ...$error ); } } diff --git a/includes/MovePage.php b/includes/MovePage.php index fc9f6a6676..3210ba8eb0 100644 --- a/includes/MovePage.php +++ b/includes/MovePage.php @@ -57,7 +57,7 @@ class MovePage { // Convert into a Status object if ( $errors ) { foreach ( $errors as $error ) { - call_user_func_array( [ $status, 'fatal' ], $error ); + $status->fatal( ...$error ); } } diff --git a/includes/OutputPage.php b/includes/OutputPage.php index c51f6f8ec2..f92f4f38da 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2645,13 +2645,13 @@ class OutputPage extends ContextSource { foreach ( $errors as $error ) { $text .= '
  • '; - $text .= call_user_func_array( [ $this, 'msg' ], $error )->plain(); + $text .= $this->msg( ...$error )->plain(); $text .= "
  • \n"; } $text .= ''; } else { $text .= "
    \n" . - call_user_func_array( [ $this, 'msg' ], reset( $errors ) )->plain() . + $this->msg( ...reset( $errors ) )->plain() . "\n
    "; } diff --git a/includes/Storage/RevisionRecord.php b/includes/Storage/RevisionRecord.php index ff0a70dd24..66ec2c07a2 100644 --- a/includes/Storage/RevisionRecord.php +++ b/includes/Storage/RevisionRecord.php @@ -474,7 +474,7 @@ abstract class RevisionRecord { $permissionlist = implode( ', ', $permissions ); if ( $title === null ) { wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" ); - return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions ); + return $user->isAllowedAny( ...$permissions ); } else { $text = $title->getPrefixedText(); wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" ); diff --git a/includes/api/ApiQueryLinks.php b/includes/api/ApiQueryLinks.php index 67bf619c89..144c7582b0 100644 --- a/includes/api/ApiQueryLinks.php +++ b/includes/api/ApiQueryLinks.php @@ -103,7 +103,7 @@ class ApiQueryLinks extends ApiQueryGeneratorBase { if ( $cond ) { $this->addWhere( $cond ); $multiNS = count( $lb->data ) !== 1; - $multiTitle = count( call_user_func_array( 'array_merge', $lb->data ) ) !== 1; + $multiTitle = count( array_merge( ...$lb->data ) ) !== 1; } else { // No titles so no results return; diff --git a/includes/api/ApiQueryTokens.php b/includes/api/ApiQueryTokens.php index e86fdf3533..8854aac0de 100644 --- a/includes/api/ApiQueryTokens.php +++ b/includes/api/ApiQueryTokens.php @@ -94,7 +94,7 @@ class ApiQueryTokens extends ApiQueryBase { public static function getToken( User $user, MediaWiki\Session\Session $session, $salt ) { if ( is_array( $salt ) ) { $session->persist(); - return call_user_func_array( [ $session, 'getToken' ], $salt ); + return $session->getToken( ...$salt ); } else { return $user->getEditTokenObject( $salt, $session->getRequest() ); } diff --git a/includes/api/ApiQueryUserContribs.php b/includes/api/ApiQueryUserContribs.php index 420d138df7..fdcaa76932 100644 --- a/includes/api/ApiQueryUserContribs.php +++ b/includes/api/ApiQueryUserContribs.php @@ -306,9 +306,11 @@ class ApiQueryUserContribs extends ApiQueryBase { foreach ( $res as $row ) { $names[$row->user_name] = $row; } - call_user_func_array( - $this->params['dir'] == 'newer' ? 'ksort' : 'krsort', [ &$names, SORT_STRING ] - ); + if ( $this->params['dir'] == 'newer' ) { + ksort( $names, SORT_STRING ); + } else { + krsort( $names, SORT_STRING ); + } $neg = $op === '>' ? -1 : 1; $userIter = call_user_func( function () use ( $names, $fromName, $neg ) { foreach ( $names as $name => $row ) { diff --git a/includes/auth/ButtonAuthenticationRequest.php b/includes/auth/ButtonAuthenticationRequest.php index d274e18f7b..1268e68de5 100644 --- a/includes/auth/ButtonAuthenticationRequest.php +++ b/includes/auth/ButtonAuthenticationRequest.php @@ -90,14 +90,14 @@ class ButtonAuthenticationRequest extends AuthenticationRequest { } elseif ( is_string( $data['label'] ) ) { $data['label'] = new \Message( $data['label'] ); } elseif ( is_array( $data['label'] ) ) { - $data['label'] = call_user_func_array( 'Message::newFromKey', $data['label'] ); + $data['label'] = Message::newFromKey( ...$data['label'] ); } if ( !isset( $data['help'] ) ) { $data['help'] = new \RawMessage( '$1', $data['name'] ); } elseif ( is_string( $data['help'] ) ) { $data['help'] = new \Message( $data['help'] ); } elseif ( is_array( $data['help'] ) ) { - $data['help'] = call_user_func_array( 'Message::newFromKey', $data['help'] ); + $data['help'] = Message::newFromKey( ...$data['help'] ); } $ret = new static( $data['name'], $data['label'], $data['help'] ); foreach ( $data as $k => $v ) { diff --git a/includes/exception/MWExceptionHandler.php b/includes/exception/MWExceptionHandler.php index 79f0a23374..9039cfca94 100644 --- a/includes/exception/MWExceptionHandler.php +++ b/includes/exception/MWExceptionHandler.php @@ -173,9 +173,7 @@ class MWExceptionHandler { global $wgPropagateErrors; if ( in_array( $level, self::$fatalErrorTypes ) ) { - return call_user_func_array( - 'MWExceptionHandler::handleFatalError', func_get_args() - ); + return self::handleFatalError( ...func_get_args() ); } // Map error constant to error name (reverse-engineer PHP error diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 785d30f8a3..e72faa0e30 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -607,7 +607,7 @@ class HTMLForm extends ContextSource { $hoistedErrors = Status::newGood(); if ( $this->mValidationErrorMessage ) { foreach ( (array)$this->mValidationErrorMessage as $error ) { - call_user_func_array( [ $hoistedErrors, 'fatal' ], $error ); + $hoistedErrors->fatal( ...$error ); } } else { $hoistedErrors->fatal( 'htmlform-invalid-input' ); diff --git a/includes/installer/WebInstaller.php b/includes/installer/WebInstaller.php index 8fb980791e..306afc4e93 100644 --- a/includes/installer/WebInstaller.php +++ b/includes/installer/WebInstaller.php @@ -717,7 +717,7 @@ class WebInstaller extends Installer { */ public function showHelpBox( $msg /*, ... */ ) { $args = func_get_args(); - $html = call_user_func_array( [ $this, 'getHelpBox' ], $args ); + $html = $this->getHelpBox( ...$args ); $this->output->addHTML( $html ); } @@ -742,7 +742,7 @@ class WebInstaller extends Installer { public function showStatusMessage( Status $status ) { $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() ); foreach ( $errors as $error ) { - call_user_func_array( [ $this, 'showMessage' ], $error ); + $this->showMessage( ...$error ); } } diff --git a/includes/libs/StatusValue.php b/includes/libs/StatusValue.php index 6f348c2b96..3bdafe1282 100644 --- a/includes/libs/StatusValue.php +++ b/includes/libs/StatusValue.php @@ -68,7 +68,7 @@ class StatusValue { public static function newFatal( $message /*, parameters...*/ ) { $params = func_get_args(); $result = new static(); - call_user_func_array( [ &$result, 'fatal' ], $params ); + $result->fatal( ...$params ); return $result; } diff --git a/includes/libs/filebackend/FileBackend.php b/includes/libs/filebackend/FileBackend.php index 2d4a772b7e..785cb72655 100644 --- a/includes/libs/filebackend/FileBackend.php +++ b/includes/libs/filebackend/FileBackend.php @@ -1590,7 +1590,7 @@ abstract class FileBackend implements LoggerAwareInterface { final protected function newStatus() { $args = func_get_args(); if ( count( $args ) ) { - $sv = call_user_func_array( [ StatusValue::class, 'newFatal' ], $args ); + $sv = StatusValue::newFatal( ...$args ); } else { $sv = StatusValue::newGood(); } diff --git a/includes/libs/lockmanager/MemcLockManager.php b/includes/libs/lockmanager/MemcLockManager.php index ebd72de894..f1f749faa0 100644 --- a/includes/libs/lockmanager/MemcLockManager.php +++ b/includes/libs/lockmanager/MemcLockManager.php @@ -89,7 +89,7 @@ class MemcLockManager extends QuorumLockManager { $memc = $this->getCache( $lockSrv ); // List of affected paths - $paths = call_user_func_array( 'array_merge', array_values( $pathsByType ) ); + $paths = array_merge( ...array_values( $pathsByType ) ); $paths = array_unique( $paths ); // List of affected lock record keys $keys = array_map( [ $this, 'recordKeyForPath' ], $paths ); @@ -164,7 +164,7 @@ class MemcLockManager extends QuorumLockManager { $memc = $this->getCache( $lockSrv ); // List of affected paths - $paths = call_user_func_array( 'array_merge', array_values( $pathsByType ) ); + $paths = array_merge( ...array_values( $pathsByType ) ); $paths = array_unique( $paths ); // List of affected lock record keys $keys = array_map( [ $this, 'recordKeyForPath' ], $paths ); diff --git a/includes/libs/lockmanager/RedisLockManager.php b/includes/libs/lockmanager/RedisLockManager.php index ea9dde7f2a..a624f0a971 100644 --- a/includes/libs/lockmanager/RedisLockManager.php +++ b/includes/libs/lockmanager/RedisLockManager.php @@ -76,7 +76,7 @@ class RedisLockManager extends QuorumLockManager { protected function getLocksOnServer( $lockSrv, array $pathsByType ) { $status = StatusValue::newGood(); - $pathList = call_user_func_array( 'array_merge', array_values( $pathsByType ) ); + $pathList = array_merge( ...array_values( $pathsByType ) ); $server = $this->lockServers[$lockSrv]; $conn = $this->redisPool->getConnection( $server, $this->logger ); @@ -171,7 +171,7 @@ LUA; protected function freeLocksOnServer( $lockSrv, array $pathsByType ) { $status = StatusValue::newGood(); - $pathList = call_user_func_array( 'array_merge', array_values( $pathsByType ) ); + $pathList = array_merge( ...array_values( $pathsByType ) ); $server = $this->lockServers[$lockSrv]; $conn = $this->redisPool->getConnection( $server, $this->logger ); diff --git a/includes/logging/LogEventsList.php b/includes/logging/LogEventsList.php index 9e4a630bd0..40498cd558 100644 --- a/includes/logging/LogEventsList.php +++ b/includes/logging/LogEventsList.php @@ -553,7 +553,7 @@ class LogEventsList extends ContextSource { } $permissionlist = implode( ', ', $permissions ); wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" ); - return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions ); + return $user->isAllowedAny( ...$permissions ); } return true; } diff --git a/includes/media/BitmapHandler.php b/includes/media/BitmapHandler.php index cda037c16d..4cb7c87ec0 100644 --- a/includes/media/BitmapHandler.php +++ b/includes/media/BitmapHandler.php @@ -228,7 +228,7 @@ class BitmapHandler extends TransformationalImageHandler { $rotation = isset( $params['disableRotation'] ) ? 0 : $this->getRotation( $image ); list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation ); - $cmd = call_user_func_array( 'wfEscapeShellArg', array_merge( + $cmd = wfEscapeShellArg( ...array_merge( [ $wgImageMagickConvertCommand ], $quality, // Specify white background color, will be used for transparent images diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 8df5b5ba09..15ed93c5ff 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -3435,7 +3435,7 @@ class Parser { } } - $result = call_user_func_array( $callback, $allArgs ); + $result = $callback( ...$allArgs ); # The interface for function hooks allows them to return a wikitext # string or an array containing the string and any flags. This mungs diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php index 9ec6cf82b2..265d1516a6 100644 --- a/includes/parser/ParserOutput.php +++ b/includes/parser/ParserOutput.php @@ -311,10 +311,10 @@ class ParserOutput extends CacheTime { } $skin = $wgOut->getSkin(); - return call_user_func_array( - [ $skin, 'doEditSectionLink' ], - [ $editsectionPage, $editsectionSection, - $editsectionContent, $wgLang->getCode() ] + return $skin->doEditSectionLink( $editsectionPage, + $editsectionSection, + $editsectionContent, + $wgLang->getCode() ); }, $text diff --git a/includes/resourceloader/ResourceLoaderContext.php b/includes/resourceloader/ResourceLoaderContext.php index d41198ae55..3ceb915d27 100644 --- a/includes/resourceloader/ResourceLoaderContext.php +++ b/includes/resourceloader/ResourceLoaderContext.php @@ -226,7 +226,7 @@ class ResourceLoaderContext implements MessageLocalizer { * @return Message */ public function msg( $key ) { - return call_user_func_array( 'wfMessage', func_get_args() ) + return wfMessage( ...func_get_args() ) ->inLanguage( $this->getLanguage() ) // Use a dummy title because there is no real title // for this endpoint, and the cache won't vary on it diff --git a/includes/skins/BaseTemplate.php b/includes/skins/BaseTemplate.php index d1bea8d1b2..683877439e 100644 --- a/includes/skins/BaseTemplate.php +++ b/includes/skins/BaseTemplate.php @@ -36,7 +36,7 @@ abstract class BaseTemplate extends QuickTemplate { * @return Message */ public function getMsg( $name /* ... */ ) { - return call_user_func_array( [ $this->getSkin(), 'msg' ], func_get_args() ); + return $this->getSkin()->msg( ...func_get_args() ); } function msg( $str ) { diff --git a/includes/specialpage/AuthManagerSpecialPage.php b/includes/specialpage/AuthManagerSpecialPage.php index 81e13f00f0..557bd9cc5a 100644 --- a/includes/specialpage/AuthManagerSpecialPage.php +++ b/includes/specialpage/AuthManagerSpecialPage.php @@ -432,11 +432,11 @@ abstract class AuthManagerSpecialPage extends SpecialPage { $status = Status::newFatal( new RawMessage( '$1', $status ) ); } elseif ( is_array( $status ) ) { if ( is_string( reset( $status ) ) ) { - $status = call_user_func_array( 'Status::newFatal', $status ); + $status = Status::newFatal( ...$status ); } elseif ( is_array( reset( $status ) ) ) { $status = Status::newGood(); foreach ( $status as $message ) { - call_user_func_array( [ $status, 'fatal' ], $message ); + $status->fatal( ...$message ); } } else { throw new UnexpectedValueException( 'invalid HTMLForm::trySubmit() return value: ' diff --git a/includes/specialpage/ChangesListSpecialPage.php b/includes/specialpage/ChangesListSpecialPage.php index 0622584331..831644ef2f 100644 --- a/includes/specialpage/ChangesListSpecialPage.php +++ b/includes/specialpage/ChangesListSpecialPage.php @@ -704,9 +704,8 @@ abstract class ChangesListSpecialPage extends SpecialPage { return; } - $knownParams = call_user_func_array( - [ $this->getRequest(), 'getValues' ], - array_keys( $this->getOptions()->getAllValues() ) + $knownParams = $this->getRequest()->getValues( + ...array_keys( $this->getOptions()->getAllValues() ) ); // HACK: Temporarily until we can properly define "sticky" filters and parameters, diff --git a/includes/specialpage/SpecialPage.php b/includes/specialpage/SpecialPage.php index 317aa0d7c8..5db8066228 100644 --- a/includes/specialpage/SpecialPage.php +++ b/includes/specialpage/SpecialPage.php @@ -791,10 +791,7 @@ class SpecialPage implements MessageLocalizer { * @see wfMessage */ public function msg( $key /* $args */ ) { - $message = call_user_func_array( - [ $this->getContext(), 'msg' ], - func_get_args() - ); + $message = $this->getContext()->msg( ...func_get_args() ); // RequestContext passes context to wfMessage, and the language is set from // the context, but setting the language for Message class removes the // interface message status, which breaks for example usernameless gender diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index efe354a346..bc632b1c0d 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -553,7 +553,7 @@ class SpecialBlock extends FormSpecialPage { if ( !$status->isOK() ) { $errors = $status->getErrorsArray(); - return call_user_func_array( [ $form, 'msg' ], $errors[0] ); + return $form->msg( ...$errors[0] ); } else { return true; } diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index 6a471ba573..5352d95b8d 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -854,7 +854,7 @@ abstract class UploadBase { if ( !is_array( $error ) ) { $error = [ $error ]; } - return call_user_func_array( 'Status::newFatal', $error ); + return Status::newFatal( ...$error ); } $status = $this->getLocalFile()->upload( @@ -1063,7 +1063,7 @@ abstract class UploadBase { if ( !$isPartial ) { $error = $this->runUploadStashFileHook( $user ); if ( $error ) { - return call_user_func_array( 'Status::newFatal', $error ); + return Status::newFatal( ...$error ); } } try { diff --git a/includes/upload/UploadFromChunks.php b/includes/upload/UploadFromChunks.php index 68bcb9d9c7..ee6f250a76 100644 --- a/includes/upload/UploadFromChunks.php +++ b/includes/upload/UploadFromChunks.php @@ -210,7 +210,7 @@ class UploadFromChunks extends UploadFromFile { // override doStashFile() with completely different functionality in this class... $error = $this->runUploadStashFileHook( $this->user ); if ( $error ) { - call_user_func_array( [ $status, 'fatal' ], $error ); + $status->fatal( ...$error ); return $status; } try { @@ -422,9 +422,9 @@ class UploadChunkFileException extends MWException { class UploadChunkVerificationException extends MWException { public $msg; - public function __construct( $res ) { - $this->msg = call_user_func_array( 'wfMessage', $res ); - parent::__construct( call_user_func_array( 'wfMessage', $res ) + public function __construct( array $res ) { + $this->msg = wfMessage( ...$res ); + parent::__construct( wfMessage( ...$res ) ->inLanguage( 'en' )->useDatabase( false )->text() ); } } diff --git a/maintenance/backup.inc b/maintenance/backup.inc index 0fdd417fc3..21d9bb1639 100644 --- a/maintenance/backup.inc +++ b/maintenance/backup.inc @@ -142,7 +142,7 @@ class BackupDumper extends Maintenance { require_once $file; } $register = [ $class, 'register' ]; - call_user_func_array( $register, [ $this ] ); + $register( $this ); } function execute() { diff --git a/maintenance/storage/recompressTracked.php b/maintenance/storage/recompressTracked.php index 49b8e0a69d..271cbf3dd6 100644 --- a/maintenance/storage/recompressTracked.php +++ b/maintenance/storage/recompressTracked.php @@ -226,7 +226,7 @@ class RecompressTracked { } $cmd .= ' --child' . ' --wiki ' . wfEscapeShellArg( wfWikiID() ) . - ' ' . call_user_func_array( 'wfEscapeShellArg', $this->destClusters ); + ' ' . wfEscapeShellArg( ...$this->destClusters ); $this->replicaPipes = $this->replicaProcs = []; for ( $i = 0; $i < $this->numProcs; $i++ ) { @@ -426,12 +426,12 @@ class RecompressTracked { $args = array_slice( $ids, 0, $this->orphanBatchSize ); $ids = array_slice( $ids, $this->orphanBatchSize ); array_unshift( $args, 'doOrphanList' ); - call_user_func_array( [ $this, 'dispatch' ], $args ); + $this->dispatch( ...$args ); } if ( count( $ids ) ) { $args = $ids; array_unshift( $args, 'doOrphanList' ); - call_user_func_array( [ $this, 'dispatch' ], $args ); + $this->dispatch( ...$args ); } $this->report( 'orphans', $i, $numOrphans ); -- 2.20.1