Use PHP 7 '??' operator instead of if-then-else
authorFomafix <fomafix@googlemail.com>
Sat, 27 Oct 2018 12:30:02 +0000 (14:30 +0200)
committerFomafix <fomafix@googlemail.com>
Sat, 27 Oct 2018 21:46:13 +0000 (23:46 +0200)
Change-Id: Ia86f8433f30a166d38ee63d0d1745b26740767b9

13 files changed:
includes/libs/rdbms/database/Database.php
languages/ConverterRule.php
languages/Language.php
languages/LanguageCode.php
languages/LanguageConverter.php
maintenance/language/generateCollationData.php
maintenance/storage/checkStorage.php
maintenance/storage/fixT22757.php
maintenance/storage/moveToExternal.php
maintenance/updateCollation.php
profileinfo.php
tests/common/TestsAutoLoader.php
tests/parser/ParserTestParserHook.php

index 16e8d8b..83b9660 100644 (file)
@@ -2038,10 +2038,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $options = [ $options ];
                }
 
-               $fh = null;
-               if ( isset( $options['fileHandle'] ) ) {
-                       $fh = $options['fileHandle'];
-               }
+               $fh = $options['fileHandle'] ?? null;
                $options = $this->makeInsertOptions( $options );
 
                if ( isset( $a[0] ) && is_array( $a[0] ) ) {
index 999d648..8be2d6a 100644 (file)
@@ -399,11 +399,7 @@ class ConverterRule {
                                case 'N':
                                        // process N flag: output current variant name
                                        $ruleVar = trim( $rules );
-                                       if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
-                                               $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
-                                       } else {
-                                               $this->mRuleDisplay = '';
-                                       }
+                                       $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar] ?? '';
                                        break;
                                case 'D':
                                        // process D flag: output rules description
index dad9c6c..86f4505 100644 (file)
@@ -3227,12 +3227,8 @@ class Language {
                        $this->doMagicHook();
                }
 
-               if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
-                       $rawEntry = $this->mMagicExtensions[$mw->mId];
-               } else {
-                       $rawEntry = self::$dataCache->getSubitem(
-                               $this->mCode, 'magicWords', $mw->mId );
-               }
+               $rawEntry = $this->mMagicExtensions[$mw->mId] ??
+                       self::$dataCache->getSubitem( $this->mCode, 'magicWords', $mw->mId );
 
                if ( !is_array( $rawEntry ) ) {
                        wfWarn( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
@@ -5063,10 +5059,6 @@ class Language {
        public function getPluralRuleType( $number ) {
                $index = $this->getPluralRuleIndexNumber( $number );
                $pluralRuleTypes = $this->getPluralRuleTypes();
-               if ( isset( $pluralRuleTypes[$index] ) ) {
-                       return $pluralRuleTypes[$index];
-               } else {
-                       return 'other';
-               }
+               return $pluralRuleTypes[$index] ?? 'other';
        }
 }
index 1e10496..7d954d3 100644 (file)
@@ -163,10 +163,7 @@ class LanguageCode {
         * @since 1.30
         */
        public static function replaceDeprecatedCodes( $code ) {
-               if ( isset( self::$deprecatedLanguageCodeMapping[$code] ) ) {
-                       return self::$deprecatedLanguageCodeMapping[$code];
-               }
-               return $code;
+               return self::$deprecatedLanguageCodeMapping[$code] ?? $code;
        }
 
        /**
index 21902af..3c8d300 100644 (file)
@@ -140,10 +140,7 @@ class LanguageConverter {
         *   main code if there is no fallback
         */
        public function getVariantFallbacks( $variant ) {
-               if ( isset( $this->mVariantFallbacks[$variant] ) ) {
-                       return $this->mVariantFallbacks[$variant];
-               }
-               return $this->mMainLanguageCode;
+               return $this->mVariantFallbacks[$variant] ?? $this->mMainLanguageCode;
        }
 
        /**
index a105920..210e907 100644 (file)
@@ -247,11 +247,7 @@ class GenerateCollationData extends Maintenance {
                        if ( $weight !== $prevWeight ) {
                                $this->groups[$prevWeight] = $group;
                                $prevWeight = $weight;
-                               if ( isset( $this->groups[$weight] ) ) {
-                                       $group = $this->groups[$weight];
-                               } else {
-                                       $group = [];
-                               }
+                               $group = $this->groups[$weight] ?? [];
                        }
                        $group[] = $cp;
                }
index 0dfb834..a95789d 100644 (file)
@@ -29,11 +29,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 
        $cs = new CheckStorage;
        $fix = isset( $options['fix'] );
-       if ( isset( $args[0] ) ) {
-               $xml = $args[0];
-       } else {
-               $xml = false;
-       }
+       $xml = $args[0] ?? false;
        $cs->check( $fix, $xml );
 }
 
index 6bc2f98..61f1177 100644 (file)
@@ -255,11 +255,7 @@ class FixT22757 extends Maintenance {
 
        function findTextIdInPage( $pageId, $textId ) {
                $ids = $this->getRevTextMap( $pageId );
-               if ( !isset( $ids[$textId] ) ) {
-                       return null;
-               } else {
-                       return $ids[$textId];
-               }
+               return $ids[$textId] ?? null;
        }
 
        function getRevTextMap( $pageId ) {
index 639ef58..0b95ba5 100644 (file)
@@ -38,11 +38,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
        $cluster = $args[0];
        $dbw = wfGetDB( DB_MASTER );
 
-       if ( isset( $options['e'] ) ) {
-               $maxID = $options['e'];
-       } else {
-               $maxID = $dbw->selectField( 'text', 'MAX(old_id)', '', $fname );
-       }
+       $maxID = $options['e'] ?? $dbw->selectField( 'text', 'MAX(old_id)', '', $fname );
        $minID = $options['s'] ?? 1;
 
        moveToExternal( $cluster, $maxID, $minID );
index 0670454..12b33b4 100644 (file)
@@ -304,11 +304,7 @@ TEXT
                        if ( $raw !== '' ) {
                                $raw .= ', ';
                        }
-                       if ( !isset( $this->sizeHistogram[$i] ) ) {
-                               $val = 0;
-                       } else {
-                               $val = $this->sizeHistogram[$i];
-                       }
+                       $val = $this->sizeHistogram[$i] ?? 0;
                        for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
                                if ( $coarseBoundaries[$coarseIndex] > $i ) {
                                        $coarseHistogram[$coarseIndex] += $val;
@@ -327,11 +323,7 @@ TEXT
                $scale = 60 / $maxBinVal;
                $prevBoundary = 0;
                for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
-                       if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
-                               $val = 0;
-                       } else {
-                               $val = $coarseHistogram[$coarseIndex];
-                       }
+                       $val = $coarseHistogram[$coarseIndex] ?? 0;
                        $boundary = $coarseBoundaries[$coarseIndex];
                        $this->output( sprintf( "%-10s %-10d |%s\n",
                                $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
index c65f952..d000972 100644 (file)
@@ -338,11 +338,7 @@ $res = $dbr->select(
        [ 'ORDER BY' => 'pf_name ASC' ]
 );
 
-if ( isset( $_REQUEST['filter'] ) ) {
-       $filter = $_REQUEST['filter'];
-} else {
-       $filter = '';
-}
+$filter = $_REQUEST['filter'] ?? '';
 
 ?>
 <form method="get" action="profileinfo.php">
index f843123..de8ddbf 100644 (file)
@@ -230,11 +230,7 @@ spl_autoload_register( function ( $class ) {
                'PHPUnit_Framework_Error' => 'PHPUnit\Framework\Error\Error',
        ];
 
-       if ( isset( $map[$class] ) ) {
-               $newForm = $map[$class];
-       } else {
-               $newForm = str_replace( '_', '\\', $class );
-       }
+       $newForm = $map[$class] ?? str_replace( '_', '\\', $class );
 
        if ( class_exists( $newForm ) || interface_exists( $newForm ) ) {
                // If the new class name exists, alias
index 5995012..acc5cb1 100644 (file)
@@ -49,11 +49,7 @@ class ParserTestParserHook {
                        && $argv['action'] === 'flush' && $in === null
                ) {
                        // Clear the buffer, we probably don't need to
-                       if ( isset( $parser->static_tag_buf ) ) {
-                               $tmp = $parser->static_tag_buf;
-                       } else {
-                               $tmp = '';
-                       }
+                       $tmp = $parser->static_tag_buf ?? '';
                        $parser->static_tag_buf = null;
                        return $tmp;
                } else { // wtf?