Make use of the list() feature where it makes sense
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Sun, 24 Mar 2019 14:04:32 +0000 (15:04 +0100)
committerThiemo Kreuz (WMDE) <thiemo.kreuz@wikimedia.de>
Sun, 24 Mar 2019 20:12:23 +0000 (20:12 +0000)
This code is functionally identical, but less error prone (not so easy
to forget or mix these numerical indexes).

This patch happens to touch the Parser, which might be a bit scary. We
can remove this file from this patch if you prefer.

Change-Id: I8cbe3a9a6725d1c42b86e67678c1af15fbc5961a

15 files changed:
includes/Linker.php
includes/cache/localisation/LCStoreStaticArray.php
includes/diff/DiffEngine.php
includes/gallery/TraditionalImageGallery.php
includes/installer/DatabaseUpdater.php
includes/libs/rdbms/database/DatabaseMysqli.php
includes/page/ImagePage.php
includes/parser/LinkHolderArray.php
includes/parser/Parser.php
includes/utils/UIDGenerator.php
languages/classes/LanguageKk_cyrl.php
maintenance/cleanupInvalidDbKeys.php
maintenance/includes/BackupDumper.php
maintenance/mysql.php
tests/phpunit/includes/specials/QueryAllSpecialPagesTest.php

index 5e07f1e..decc13c 100644 (file)
@@ -1710,12 +1710,8 @@ class Linker {
        static function splitTrail( $trail ) {
                $regex = MediaWikiServices::getInstance()->getContentLanguage()->linkTrail();
                $inside = '';
-               if ( $trail !== '' ) {
-                       $m = [];
-                       if ( preg_match( $regex, $trail, $m ) ) {
-                               $inside = $m[1];
-                               $trail = $m[2];
-                       }
+               if ( $trail !== '' && preg_match( $regex, $trail, $m ) ) {
+                       list( , $inside, $trail ) = $m;
                }
                return [ $inside, $trail ];
        }
index 75c8465..f860146 100644 (file)
@@ -101,8 +101,7 @@ class LCStoreStaticArray implements LCStore {
                        return $encoded;
                }
 
-               $type = $encoded[0];
-               $data = $encoded[1];
+               list( $type, $data ) = $encoded;
 
                switch ( $type ) {
                        case 'a':
index edf8444..546a12c 100644 (file)
@@ -456,9 +456,7 @@ class DiffEngine {
 
                // need to store these so we don't lose them when they're
                // overwritten by the recursion
-               $len = $snake[2];
-               $startx = $snake[0];
-               $starty = $snake[1];
+               list( $startx, $starty, $len ) = $snake;
 
                // the middle snake is part of the LCS, store it
                for ( $i = 0; $i < $len; ++$i ) {
index 5ede631..9de7eb8 100644 (file)
@@ -72,11 +72,9 @@ class TraditionalImageGallery extends ImageGalleryBase {
                $lang = $this->getRenderLang();
                # Output each image...
                foreach ( $this->mImages as $pair ) {
+                       // "text" means "caption" here
                        /** @var Title $nt */
-                       $nt = $pair[0];
-                       $text = $pair[1]; # "text" means "caption" here
-                       $alt = $pair[2];
-                       $link = $pair[3];
+                       list( $nt, $text, $alt, $link ) = $pair;
 
                        $descQuery = false;
                        if ( $nt->getNamespace() === NS_FILE ) {
index 7a92807..750f108 100644 (file)
@@ -410,9 +410,7 @@ abstract class DatabaseUpdater {
                $this->updatesSkipped = [];
 
                foreach ( $updates as $funcList ) {
-                       $func = $funcList[0];
-                       $args = $funcList[1];
-                       $origParams = $funcList[2];
+                       list( $func, $args, $origParams ) = $funcList;
                        $func( ...$args );
                        flush();
                        $this->updatesSkipped[] = $origParams;
index 1c4ed56..15eeccf 100644 (file)
@@ -78,9 +78,7 @@ class DatabaseMysqli extends DatabaseMysqlBase {
                } elseif ( substr_count( $realServer, ':' ) == 1 ) {
                        // If we have a colon and something that's not a port number
                        // inside the hostname, assume it's the socket location
-                       $hostAndSocket = explode( ':', $realServer, 2 );
-                       $realServer = $hostAndSocket[0];
-                       $socket = $hostAndSocket[1];
+                       list( $realServer, $socket ) = explode( ':', $realServer, 2 );
                }
 
                $mysqli = mysqli_init();
index 66804bc..60237ff 100644 (file)
@@ -321,9 +321,7 @@ class ImagePage extends Article {
                $dirmark = $lang->getDirMarkEntity();
                $request = $this->getContext()->getRequest();
 
-               $max = $this->getImageLimitsFromOption( $user, 'imagesize' );
-               $maxWidth = $max[0];
-               $maxHeight = $max[1];
+               list( $maxWidth, $maxHeight ) = $this->getImageLimitsFromOption( $user, 'imagesize' );
 
                if ( $this->displayImg->exists() ) {
                        # image
@@ -1029,7 +1027,7 @@ EOT
         *
         * @param User $user
         * @param string $optionName Name of a option to check, typically imagesize or thumbsize
-        * @return array
+        * @return int[]
         * @since 1.21
         */
        public function getImageLimitsFromOption( $user, $optionName ) {
index 078c819..6416449 100644 (file)
@@ -632,8 +632,7 @@ class LinkHolderArray {
         * @private
         */
        public function replaceTextCallback( $matches ) {
-               $type = $matches[1];
-               $key = $matches[2];
+               list( , $type, $key ) = $matches;
                if ( $type == 'LINK' ) {
                        list( $ns, $index ) = explode( ':', $key, 2 );
                        if ( isset( $this->internals[$ns][$index]['text'] ) ) {
index 546152f..0440e89 100644 (file)
@@ -1045,10 +1045,7 @@ class Parser {
                                $inside = $p[5];
                        } else {
                                # tag
-                               $element = $p[1];
-                               $attributes = $p[2];
-                               $close = $p[3];
-                               $inside = $p[4];
+                               list( , $element, $attributes, $close, $inside ) = $p;
                        }
 
                        $marker = self::MARKER_PREFIX . "-$element-" . sprintf( '%08X', $n++ ) . self::MARKER_SUFFIX;
@@ -1072,8 +1069,7 @@ class Parser {
                                        $tail = '';
                                        $text = '';
                                } else {
-                                       $tail = $q[1];
-                                       $text = $q[2];
+                                       list( , $tail, $text ) = $q;
                                }
                        }
 
@@ -2240,8 +2236,7 @@ class Parser {
 
                        if ( $useLinkPrefixExtension ) {
                                if ( preg_match( $e2, $s, $m ) ) {
-                                       $prefix = $m[2];
-                                       $s = $m[1];
+                                       list( , $s, $prefix ) = $m;
                                } else {
                                        $prefix = '';
                                }
index 15c0cf9..65b50e2 100644 (file)
@@ -137,8 +137,7 @@ class UIDGenerator {
                        $time = $info['time'];
                        $counter = $info['offsetCounter'];
                } else {
-                       $time = $info[0];
-                       $counter = $info[1];
+                       list( $time, $counter ) = $info;
                }
                // Take the 46 LSBs of "milliseconds since epoch"
                $id_bin = $this->millisecondsSinceEpochBinary( $time );
@@ -192,9 +191,7 @@ class UIDGenerator {
                        $counter = $info['offsetCounter'];
                        $clkSeq = $info['clkSeq'];
                } else {
-                       $time = $info[0];
-                       $counter = $info[1];
-                       $clkSeq = $info[2];
+                       list( $time, $counter, $clkSeq ) = $info;
                }
                // Take the 46 LSBs of "milliseconds since epoch"
                $id_bin = $this->millisecondsSinceEpochBinary( $time );
index d695be1..a89dbc2 100644 (file)
@@ -63,9 +63,7 @@ class LanguageKk_cyrl extends Language {
                $secondPerson = [ "з" ]; // 1st plural, 2nd formal
                $thirdPerson = [ "ы", "і" ]; // 3rd
 
-               $lastLetter = $this->lastLetter( $word, $allVowels );
-               $wordEnding =& $lastLetter[0];
-               $wordLastVowel =& $lastLetter[1];
+               list( $wordEnding, $wordLastVowel ) = $this->lastLetter( $word, $allVowels );
 
                // Now convert the word
                switch ( $case ) {
@@ -297,9 +295,7 @@ class LanguageKk_cyrl extends Language {
                $secondPerson = [ "z" ]; // 1st plural, 2nd formal
                $thirdPerson = [ "ı", "i" ]; // 3rd
 
-               $lastLetter = $this->lastLetter( $word, $allVowels );
-               $wordEnding =& $lastLetter[0];
-               $wordLastVowel =& $lastLetter[1];
+               list( $wordEnding, $wordLastVowel ) = $this->lastLetter( $word, $allVowels );
 
                // Now convert the word
                switch ( $case ) {
@@ -531,9 +527,7 @@ class LanguageKk_cyrl extends Language {
                $secondPerson = [ "ز" ]; // 1st plural, 2nd formal
                $thirdPerson = [ "ى", "ٸ" ]; // 3rd
 
-               $lastLetter = $this->lastLetter( $word, $allVowels );
-               $wordEnding = $lastLetter[0];
-               $wordLastVowel = $lastLetter[1];
+               list( $wordEnding, $wordLastVowel ) = $this->lastLetter( $word, $allVowels );
 
                // Now convert the word
                switch ( $case ) {
@@ -737,7 +731,7 @@ class LanguageKk_cyrl extends Language {
 
        /**
         * @param string $word
-        * @param array $allVowels
+        * @param string[] $allVowels
         * @return array
         */
        function lastLetter( $word, $allVowels ) {
index abae4f4..eb45cfc 100644 (file)
@@ -121,8 +121,7 @@ TEXT
         * @param array $tableParams A child array of self::$tables
         */
        protected function cleanupTable( $tableParams ) {
-               $table = $tableParams[0];
-               $prefix = $tableParams[1];
+               list( $table, $prefix ) = $tableParams;
                $idField = $tableParams['idField'] ?? "{$prefix}_id";
                $nsField = $tableParams['nsField'] ?? "{$prefix}_namespace";
                $titleField = $tableParams['titleField'] ?? "{$prefix}_title";
index 3ae7f48..425bf36 100644 (file)
@@ -163,8 +163,7 @@ abstract class BackupDumper extends Maintenance {
 
                $options = $this->orderedOptions;
                foreach ( $options as $arg ) {
-                       $opt = $arg[0];
-                       $param = $arg[1];
+                       list( $opt, $param ) = $arg;
 
                        switch ( $opt ) {
                                case 'plugin':
index 34a6cb6..c1e403c 100644 (file)
@@ -137,9 +137,7 @@ class MysqlMaintenance extends Maintenance {
                } elseif ( substr_count( $realServer, ':' ) == 1 ) {
                        // If we have a colon and something that's not a port number
                        // inside the hostname, assume it's the socket location
-                       $hostAndSocket = explode( ':', $realServer, 2 );
-                       $realServer = $hostAndSocket[0];
-                       $socket = $hostAndSocket[1];
+                       list( $realServer, $socket ) = explode( ':', $realServer, 2 );
                }
 
                if ( $dbName === false ) {
index 4a171df..58f83de 100644 (file)
@@ -42,8 +42,7 @@ class QueryAllSpecialPagesTest extends MediaWikiTestCase {
                parent::__construct();
 
                foreach ( QueryPage::getPages() as $page ) {
-                       $class = $page[0];
-                       $name = $page[1];
+                       list( $class, $name ) = $page;
                        if ( !in_array( $class, $this->manualTest ) ) {
                                $this->queryPages[$class] =
                                        MediaWikiServices::getInstance()->getSpecialPageFactory()->getPage( $name );