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
This commit is contained in:
Max Semenik 2018-06-07 19:58:35 -07:00
parent 7fb5c9580d
commit 6e956d55aa
29 changed files with 49 additions and 53 deletions

View 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 );
}
}

View 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 );
}
}

View 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>";
}

View 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" );

View 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;

View 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() );
}

View 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 ) {

View 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 ) {

View 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

View 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' );

View 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 );
}
}

View 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;
}

View 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();
}

View 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 );

View 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 );

View 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;
}

View 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

View 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

View 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

View 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

View 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 ) {

View 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: '

View 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,

View 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

View 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;
}

View 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 {

View 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() );
}
}

View 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() {

View 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 );