Simplify a few list() that only care about the first element
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Fri, 17 May 2019 14:54:47 +0000 (16:54 +0200)
committerThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Fri, 17 May 2019 14:54:47 +0000 (16:54 +0200)
The nice thing about explode() is that the resulting array is
guaranteed to contain at least one element. The array can not be
empty.

In some of these cases it might be possible to use strstr() instead,
but that returns an empty string when the needle character is not
found. explode() returns the original string in this case.

Change-Id: I6ad1f3273defeaf36e2305fd871eaaf9d3c1e134

includes/OutputHandler.php
includes/installer/Installer.php
includes/media/FormatMetadata.php
includes/specials/SpecialRedirect.php
includes/specials/pagers/UsersPager.php
maintenance/update.php

index 16c3784..ba9e2d7 100644 (file)
@@ -62,7 +62,7 @@ class OutputHandler {
                /// @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
                if ( isset( $_SERVER['REQUEST_URI'] ) ) {
                        // Strip the query string...
-                       list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
+                       $path = explode( '?', $_SERVER['REQUEST_URI'], 2 )[0];
                } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
                        // Probably IIS. QUERY_STRING appears separately.
                        $path = $_SERVER['SCRIPT_NAME'];
index 5c3d1d0..26f9bf0 100644 (file)
@@ -494,7 +494,7 @@ abstract class Installer {
 
                $good = true;
                // Must go here because an old version of PCRE can prevent other checks from completing
-               list( $pcreVersion ) = explode( ' ', PCRE_VERSION, 2 );
+               $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
                if ( version_compare( $pcreVersion, self::MINIMUM_PCRE_VERSION, '<' ) ) {
                        $this->showError( 'config-pcre-old', self::MINIMUM_PCRE_VERSION, $pcreVersion );
                        $good = false;
index 345b3cb..333c610 100644 (file)
@@ -1181,7 +1181,7 @@ class FormatMetadata extends ContextSource {
                $langName = Language::fetchLanguageName( $lowLang );
                if ( $langName === '' ) {
                        // try just the base language name. (aka en-US -> en ).
-                       list( $langPrefix ) = explode( '-', $lowLang, 2 );
+                       $langPrefix = explode( '-', $lowLang, 2 )[0];
                        $langName = Language::fetchLanguageName( $langPrefix );
                        if ( $langName === '' ) {
                                // give up.
index c4e4635..49f1b3c 100644 (file)
@@ -61,8 +61,8 @@ class SpecialRedirect extends FormSpecialPage {
        function setParameter( $subpage ) {
                // parse $subpage to pull out the parts
                $parts = explode( '/', $subpage, 2 );
-               $this->mType = count( $parts ) > 0 ? $parts[0] : null;
-               $this->mValue = count( $parts ) > 1 ? $parts[1] : null;
+               $this->mType = $parts[0];
+               $this->mValue = $parts[1] ?? null;
        }
 
        /**
index 4453772..57b575b 100644 (file)
@@ -49,7 +49,7 @@ class UsersPager extends AlphabeticPager {
                }
 
                $request = $this->getRequest();
-               $par = ( $par !== null ) ? $par : '';
+               $par = $par ?? '';
                $parms = explode( '/', $par );
                $symsForAll = [ '*', 'user' ];
 
@@ -277,7 +277,7 @@ class UsersPager extends AlphabeticPager {
         * @return string
         */
        function getPageHeader() {
-               list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
+               $self = explode( '/', $this->getTitle()->getPrefixedDBkey(), 2 )[0];
 
                $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
                foreach ( $this->getAllGroups() as $group => $groupText ) {
index 50fb6dc..b6c7ae4 100755 (executable)
@@ -64,7 +64,7 @@ class UpdateMediaWiki extends Maintenance {
        function compatChecks() {
                $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
 
-               list( $pcreVersion ) = explode( ' ', PCRE_VERSION, 2 );
+               $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
                if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
                        $this->fatalError(
                                "PCRE $minimumPcreVersion or later is required.\n" .