Make use of array deconstruction directly in foreach, if possible

Deconstructing non-sparse, numerically indexed arrays directly in
foreach (a.k.a. using the list() syntax in foreach) is possible since
PHP 5.5.

The possibility to use string array keys as well as non-sequential
numeric keys in array deconstruction was added in PHP 7.1.

Change-Id: I56a48552a45f61cedc291b306cad8548fc70d485
This commit is contained in:
Thiemo Kreuz 2020-06-18 16:57:50 +02:00 committed by Umherirrender
parent 5798765640
commit 20b2c5000d
15 changed files with 20 additions and 42 deletions

View file

@ -1496,8 +1496,7 @@ class FileRepo {
$backend = $this->backend; // convenience
$operations = [];
// Validate filenames and create archive directories
foreach ( $sourceDestPairs as $pair ) {
list( $srcRel, $archiveRel ) = $pair;
foreach ( $sourceDestPairs as [ $srcRel, $archiveRel ] ) {
if ( !$this->validateFilename( $srcRel ) ) {
throw new MWException( __METHOD__ . ':Validation error in $srcRel' );
} elseif ( !$this->validateFilename( $archiveRel ) ) {

View file

@ -41,7 +41,7 @@ class LocalFileDeleteBatch {
/** @var array */
private $archiveUrls = [];
/** @var array Items to be processed in the deletion batch */
/** @var array[] Items to be processed in the deletion batch */
private $deletionBatch;
/** @var bool Whether to suppress all suppressable fields when deleting */
@ -376,8 +376,7 @@ class LocalFileDeleteBatch {
protected function removeNonexistentFiles( $batch ) {
$files = $newBatch = [];
foreach ( $batch as $batchItem ) {
list( $src, ) = $batchItem;
foreach ( $batch as [ $src, /* dest */ ] ) {
$files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
}

View file

@ -72,18 +72,17 @@ class TraditionalImageGallery extends ImageGalleryBase {
// of the filename below
$linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
$lb = $linkBatchFactory->newLinkBatch();
foreach ( $this->mImages as $img ) {
$lb->addObj( $img[0] );
foreach ( $this->mImages as [ $title, /* see below */ ] ) {
$lb->addObj( $title );
}
$lb->execute();
}
$lang = $this->getRenderLang();
# Output each image...
foreach ( $this->mImages as $pair ) {
foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading ] ) {
// "text" means "caption" here
/** @var Title $nt */
list( $nt, $text, $alt, $link, $handlerOpts, $loading ) = $pair;
$descQuery = false;
if ( $nt->getNamespace() === NS_FILE ) {
@ -103,8 +102,7 @@ class TraditionalImageGallery extends ImageGalleryBase {
}
$params = $this->getThumbParams( $img );
// $pair[4] is per image handler options
$transformOptions = $params + $pair[4];
$transformOptions = $params + $handlerOpts;
$thumb = false;

View file

@ -1197,8 +1197,7 @@ class HTMLForm extends ContextSource {
$html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
}
foreach ( $this->mHiddenFields as $data ) {
list( $value, $attribs ) = $data;
foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
$html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
}

View file

@ -460,8 +460,7 @@ abstract class DatabaseUpdater {
$updates = $this->updatesSkipped;
$this->updatesSkipped = [];
foreach ( $updates as $funcList ) {
list( $func, $args, $origParams ) = $funcList;
foreach ( $updates as [ $func, $args, $origParams ] ) {
// @phan-suppress-next-line PhanUndeclaredInvokeInCallable
$func( ...$args );
flush();

View file

@ -995,9 +995,7 @@ class MemcachedClient {
* All data has been read, time to process the data and build
* meaningful return values.
*/
foreach ( $results as $vars ) {
list( $rkey, $flags, /* length */, $casToken, $data ) = $vars;
foreach ( $results as [ $rkey, $flags, /* length */, $casToken, $data ] ) {
if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
$this->_handle_error( $sock,
'line ending missing from data block from $1' );

View file

@ -344,8 +344,7 @@ class TransactionProfiler implements LoggerAwareInterface {
}
if ( $slow ) {
$trace = '';
foreach ( $this->dbTrxMethodTimes[$name] as $i => $info ) {
list( $query, $sTime, $end ) = $info;
foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) {
$trace .= sprintf(
"%d\t%.6f\t%s\n", $i, ( $end - $sTime ), self::queryString( $query ) );
}

View file

@ -486,8 +486,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
$user = $this->getUser();
$badItems = $this->badItems;
DeferredUpdates::addCallableUpdate( function () use ( $user, $badItems ) {
foreach ( $badItems as $row ) {
list( $title, $namespace, $dbKey ) = $row;
foreach ( $badItems as [ $title, $namespace, $dbKey ] ) {
$action = $title ? 'cleaning up' : 'deleting';
wfDebug( "User {$user->getName()} has broken watchlist item " .
"ns($namespace):$dbKey, $action." );

View file

@ -121,9 +121,7 @@ class SpecialSpecialpages extends UnlistedSpecialPage {
Html::openElement( 'div', [ 'class' => 'mw-specialpages-list' ] )
. '<ul>'
);
foreach ( $sortedPages as $desc => $specialpage ) {
list( $title, $restricted, $cached ) = $specialpage;
foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
$pageClasses = [];
if ( $cached ) {
$includesCachedPages = true;

View file

@ -193,9 +193,7 @@ abstract class BackupDumper extends Maintenance {
$this->schemaVersion = WikiExporter::schemaVersion();
$options = $this->orderedOptions;
foreach ( $options as $arg ) {
list( $opt, $param ) = $arg;
foreach ( $options as [ $opt, $param ] ) {
switch ( $opt ) {
case 'plugin':
$val = explode( ':', $param, 2 );

View file

@ -590,8 +590,7 @@ class NamespaceDupes extends Maintenance {
[ 'templatelinks', 'tl' ],
[ 'imagelinks', 'il' ]
];
foreach ( $fromNamespaceTables as $tableInfo ) {
list( $table, $fieldPrefix ) = $tableInfo;
foreach ( $fromNamespaceTables as [ $table, $fieldPrefix ] ) {
$dbw->update( $table,
// SET
[ "{$fieldPrefix}_from_namespace" => $newLinkTarget->getNamespace() ],

View file

@ -66,9 +66,7 @@ class DifferenceEngineTest extends MediaWikiIntegrationTestCase {
public function testMapDiffPrevNext() {
$cases = $this->getMapDiffPrevNextCases();
foreach ( $cases as $case ) {
list( $expected, $old, $new, $message ) = $case;
foreach ( $cases as [ $expected, $old, $new, $message ] ) {
$diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
$diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
$this->assertEquals( $expected, $diffMap, $message );
@ -88,9 +86,7 @@ class DifferenceEngineTest extends MediaWikiIntegrationTestCase {
public function testLoadRevisionData() {
$cases = $this->getLoadRevisionDataCases();
foreach ( $cases as $testName => $case ) {
list( $expectedOld, $expectedNew, $expectedRet, $old, $new ) = $case;
foreach ( $cases as $testName => [ $expectedOld, $expectedNew, $expectedRet, $old, $new ] ) {
$diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
$ret = $diffEngine->loadRevisionData();
$ret2 = $diffEngine->loadRevisionData();

View file

@ -21,8 +21,7 @@ class RefreshLinksPartitionTest extends MediaWikiIntegrationTestCase {
public function testRefreshLinks( $ns, $dbKey, $pages ) {
$title = Title::makeTitle( $ns, $dbKey );
foreach ( $pages as $page ) {
list( $bns, $bdbkey ) = $page;
foreach ( $pages as [ $bns, $bdbkey ] ) {
$bpage = WikiPage::factory( Title::makeTitle( $bns, $bdbkey ) );
$content = ContentHandler::makeContent( "[[{$title->getPrefixedText()}]]", $bpage->getTitle() );
$bpage->doEditContent( $content, "test" );

View file

@ -41,8 +41,7 @@ class QueryAllSpecialPagesTest extends MediaWikiIntegrationTestCase {
protected function setUp() : void {
parent::setUp();
foreach ( QueryPage::getPages() as $page ) {
list( $class, $name ) = $page;
foreach ( QueryPage::getPages() as [ $class, $name ] ) {
if ( !in_array( $class, $this->manualTest ) ) {
$this->queryPages[$class] =
MediaWikiServices::getInstance()->getSpecialPageFactory()->getPage( $name );

View file

@ -42,8 +42,7 @@ class FileContentsHasherTest extends PHPUnit\Framework\TestCase {
public function testMultipleFileHash( $files ) {
$fileNames = [];
$hashes = [];
foreach ( $files as $fileInfo ) {
list( $fileName, $contents ) = $fileInfo;
foreach ( $files as [ $fileName, $contents ] ) {
$fileNames[] = $fileName;
$hashes[] = md5( $contents );
}