Replace call_user_func_array(), part 2
authorMax Semenik <maxsem.wiki@gmail.com>
Fri, 8 Jun 2018 02:58:35 +0000 (19:58 -0700)
committerMax Semenik <maxsem.wiki@gmail.com>
Fri, 8 Jun 2018 03:19:26 +0000 (20:19 -0700)
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

29 files changed:
includes/MergeHistory.php
includes/MovePage.php
includes/OutputPage.php
includes/Storage/RevisionRecord.php
includes/api/ApiQueryLinks.php
includes/api/ApiQueryTokens.php
includes/api/ApiQueryUserContribs.php
includes/auth/ButtonAuthenticationRequest.php
includes/exception/MWExceptionHandler.php
includes/htmlform/HTMLForm.php
includes/installer/WebInstaller.php
includes/libs/StatusValue.php
includes/libs/filebackend/FileBackend.php
includes/libs/lockmanager/MemcLockManager.php
includes/libs/lockmanager/RedisLockManager.php
includes/logging/LogEventsList.php
includes/media/BitmapHandler.php
includes/parser/Parser.php
includes/parser/ParserOutput.php
includes/resourceloader/ResourceLoaderContext.php
includes/skins/BaseTemplate.php
includes/specialpage/AuthManagerSpecialPage.php
includes/specialpage/ChangesListSpecialPage.php
includes/specialpage/SpecialPage.php
includes/specials/SpecialBlock.php
includes/upload/UploadBase.php
includes/upload/UploadFromChunks.php
maintenance/backup.inc
maintenance/storage/recompressTracked.php

index 0e9bb46..4c655eb 100644 (file)
@@ -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 );
                        }
                }
 
index fc9f6a6..3210ba8 100644 (file)
@@ -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 );
                        }
                }
 
index c51f6f8..f92f4f3 100644 (file)
@@ -2645,13 +2645,13 @@ class OutputPage extends ContextSource {
 
                        foreach ( $errors as $error ) {
                                $text .= '<li>';
-                               $text .= call_user_func_array( [ $this, 'msg' ], $error )->plain();
+                               $text .= $this->msg( ...$error )->plain();
                                $text .= "</li>\n";
                        }
                        $text .= '</ul>';
                } else {
                        $text .= "<div class=\"permissions-errors\">\n" .
-                                       call_user_func_array( [ $this, 'msg' ], reset( $errors ) )->plain() .
+                                       $this->msg( ...reset( $errors ) )->plain() .
                                        "\n</div>";
                }
 
index ff0a70d..66ec2c0 100644 (file)
@@ -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" );
index 67bf619..144c758 100644 (file)
@@ -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;
index e86fdf3..8854aac 100644 (file)
@@ -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() );
                }
index 420d138..fdcaa76 100644 (file)
@@ -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 ) {
index d274e18..1268e68 100644 (file)
@@ -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 ) {
index 79f0a23..9039cfc 100644 (file)
@@ -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
index 785d30f..e72faa0 100644 (file)
@@ -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' );
index 8fb9807..306afc4 100644 (file)
@@ -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 );
                }
        }
 
index 6f348c2..3bdafe1 100644 (file)
@@ -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;
        }
 
index 2d4a772..785cb72 100644 (file)
@@ -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();
                }
index ebd72de..f1f749f 100644 (file)
@@ -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 );
index ea9dde7..a624f0a 100644 (file)
@@ -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 );
index 9e4a630..40498cd 100644 (file)
@@ -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;
        }
index cda037c..4cb7c87 100644 (file)
@@ -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
index 8df5b5b..15ed93c 100644 (file)
@@ -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
index 9ec6cf8..265d151 100644 (file)
@@ -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
index d41198a..3ceb915 100644 (file)
@@ -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
index d1bea8d..6838774 100644 (file)
@@ -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 ) {
index 81e13f0..557bd9c 100644 (file)
@@ -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: '
index 0622584..831644e 100644 (file)
@@ -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,
index 317aa0d..5db8066 100644 (file)
@@ -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
index efe354a..bc632b1 100644 (file)
@@ -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;
                }
index 6a471ba..5352d95 100644 (file)
@@ -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 {
index 68bcb9d..ee6f250 100644 (file)
@@ -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() );
        }
 }
index 0fdd417..21d9bb1 100644 (file)
@@ -142,7 +142,7 @@ class BackupDumper extends Maintenance {
                        require_once $file;
                }
                $register = [ $class, 'register' ];
-               call_user_func_array( $register, [ $this ] );
+               $register( $this );
        }
 
        function execute() {
index 49b8e0a..271cbf3 100644 (file)
@@ -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 );