From 512aa4e5510fba13063dcb7e1663fb41b9959dfd Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 27 Oct 2018 14:30:02 +0200 Subject: [PATCH] Use PHP 7 '??' operator instead of if-then-else Change-Id: Ia86f8433f30a166d38ee63d0d1745b26740767b9 --- includes/libs/rdbms/database/Database.php | 5 +---- languages/ConverterRule.php | 6 +----- languages/Language.php | 14 +++----------- languages/LanguageCode.php | 5 +---- languages/LanguageConverter.php | 5 +---- maintenance/language/generateCollationData.php | 6 +----- maintenance/storage/checkStorage.php | 6 +----- maintenance/storage/fixT22757.php | 6 +----- maintenance/storage/moveToExternal.php | 6 +----- maintenance/updateCollation.php | 12 ++---------- profileinfo.php | 6 +----- tests/common/TestsAutoLoader.php | 6 +----- tests/parser/ParserTestParserHook.php | 6 +----- 13 files changed, 16 insertions(+), 73 deletions(-) diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index 16e8d8b37c..83b9660fd3 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -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] ) ) { diff --git a/languages/ConverterRule.php b/languages/ConverterRule.php index 999d648c21..8be2d6aed0 100644 --- a/languages/ConverterRule.php +++ b/languages/ConverterRule.php @@ -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 diff --git a/languages/Language.php b/languages/Language.php index dad9c6c982..86f4505cc4 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -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'; } } diff --git a/languages/LanguageCode.php b/languages/LanguageCode.php index 1e10496984..7d954d3803 100644 --- a/languages/LanguageCode.php +++ b/languages/LanguageCode.php @@ -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; } /** diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 21902af0a6..3c8d300316 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -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; } /** diff --git a/maintenance/language/generateCollationData.php b/maintenance/language/generateCollationData.php index a10592055f..210e907264 100644 --- a/maintenance/language/generateCollationData.php +++ b/maintenance/language/generateCollationData.php @@ -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; } diff --git a/maintenance/storage/checkStorage.php b/maintenance/storage/checkStorage.php index 0dfb8346a2..a95789d366 100644 --- a/maintenance/storage/checkStorage.php +++ b/maintenance/storage/checkStorage.php @@ -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 ); } diff --git a/maintenance/storage/fixT22757.php b/maintenance/storage/fixT22757.php index 6bc2f988b8..61f1177899 100644 --- a/maintenance/storage/fixT22757.php +++ b/maintenance/storage/fixT22757.php @@ -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 ) { diff --git a/maintenance/storage/moveToExternal.php b/maintenance/storage/moveToExternal.php index 639ef5846f..0b95ba5e68 100644 --- a/maintenance/storage/moveToExternal.php +++ b/maintenance/storage/moveToExternal.php @@ -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 ); diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php index 0670454947..12b33b47bb 100644 --- a/maintenance/updateCollation.php +++ b/maintenance/updateCollation.php @@ -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 ) . ': ', diff --git a/profileinfo.php b/profileinfo.php index c65f952ac2..d000972051 100644 --- a/profileinfo.php +++ b/profileinfo.php @@ -338,11 +338,7 @@ $res = $dbr->select( [ 'ORDER BY' => 'pf_name ASC' ] ); -if ( isset( $_REQUEST['filter'] ) ) { - $filter = $_REQUEST['filter']; -} else { - $filter = ''; -} +$filter = $_REQUEST['filter'] ?? ''; ?>
diff --git a/tests/common/TestsAutoLoader.php b/tests/common/TestsAutoLoader.php index f8431239b7..de8ddbfe8c 100644 --- a/tests/common/TestsAutoLoader.php +++ b/tests/common/TestsAutoLoader.php @@ -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 diff --git a/tests/parser/ParserTestParserHook.php b/tests/parser/ParserTestParserHook.php index 5995012b3b..acc5cb1581 100644 --- a/tests/parser/ParserTestParserHook.php +++ b/tests/parser/ParserTestParserHook.php @@ -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? -- 2.20.1