Simplify PHP by using ?? and ?:
authorFomafix <fomafix@googlemail.com>
Wed, 20 Jun 2018 05:26:57 +0000 (07:26 +0200)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 10 Jul 2018 20:03:17 +0000 (20:03 +0000)
Also remove not necessary surrounding parentheses.

Change-Id: I0eb5c9c1bdfb09a800258379cdcefb5fd4d3d21c

32 files changed:
includes/Block.php
includes/CommentStore.php
includes/EditPage.php
includes/Storage/RevisionStore.php
includes/WikiReference.php
includes/actions/InfoAction.php
includes/api/ApiQueryAllDeletedRevisions.php
includes/api/ApiQueryAllImages.php
includes/api/ApiQueryAllLinks.php
includes/api/ApiQueryContributors.php
includes/api/ApiQueryImageInfo.php
includes/api/ApiQueryLogEvents.php
includes/api/ApiSetPageLanguage.php
includes/db/CloneDatabase.php
includes/htmlform/HTMLFormField.php
includes/libs/filebackend/fileop/FileOp.php
includes/logging/LogEntry.php
includes/media/FormatMetadata.php
includes/search/SearchResultSet.php
includes/site/DBSiteStore.php
includes/specialpage/QueryPage.php
includes/specials/SpecialContributions.php
includes/specials/SpecialFileDuplicateSearch.php
includes/specials/SpecialPageLanguage.php
includes/specials/SpecialSearch.php
includes/specials/SpecialUserrights.php
includes/specials/pagers/ProtectedPagesPager.php
maintenance/updateSpecialPages.php
tests/phpunit/ResourceLoaderTestCase.php
tests/phpunit/includes/Storage/DerivedPageDataUpdaterTest.php
tests/phpunit/includes/Storage/PageUpdaterTest.php
tests/phpunit/includes/session/CookieSessionProviderTest.php

index a7b8035..0d0dde4 100644 (file)
@@ -1641,7 +1641,7 @@ class Block {
                        $reason,
                        $context->getRequest()->getIP(),
                        $this->getByName(),
-                       $systemBlockType !== null ? $systemBlockType : $this->getId(),
+                       $systemBlockType ?? $this->getId(),
                        $lang->formatExpiry( $this->mExpiry ),
                        (string)$intended,
                        $lang->userTimeAndDate( $this->mTimestamp, $context->getUser() ),
index 6b94d58..1fea309 100644 (file)
@@ -138,7 +138,7 @@ class CommentStore {
         * @return string
         */
        private function getKey( $methodKey = null ) {
-               $key = $this->key !== null ? $this->key : $methodKey;
+               $key = $this->key ?? $methodKey;
                if ( $key === null ) {
                        // @codeCoverageIgnoreStart
                        throw new InvalidArgumentException( '$key should not be null' );
index 9a8a4a6..b8f5208 100644 (file)
@@ -3357,7 +3357,7 @@ ERROR;
                }
 
                $this->showTextbox(
-                       $textoverride !== null ? $textoverride : $this->textbox1,
+                       $textoverride ?? $this->textbox1,
                        'wpTextbox1',
                        $attribs
                );
index 12bee1e..a184986 100644 (file)
@@ -1071,7 +1071,7 @@ class RevisionStore
                                if ( !property_exists( $row, 'old_flags' ) ) {
                                        throw new InvalidArgumentException( 'old_flags was not set in $row' );
                                }
-                               $blobFlags = ( $row->old_flags === null ) ? '' : $row->old_flags;
+                               $blobFlags = $row->old_flags ?? '';
                        }
 
                        $mainSlotRow->slot_revision_id = intval( $row->rev_id );
index 724ba98..d3688c8 100644 (file)
@@ -36,7 +36,7 @@ class WikiReference {
        public function __construct( $canonicalServer, $path, $server = null ) {
                $this->mCanonicalServer = $canonicalServer;
                $this->mPath = $path;
-               $this->mServer = $server === null ? $canonicalServer : $server;
+               $this->mServer = $server ?? $canonicalServer;
        }
 
        /**
index 0a4eae8..98dfeb3 100644 (file)
@@ -218,21 +218,15 @@ class InfoAction extends FormlessAction {
 
                $pageCounts = $this->pageCounts( $this->page );
 
-               $pageProperties = [];
                $props = PageProps::getInstance()->getAllProperties( $title );
-               if ( isset( $props[$id] ) ) {
-                       $pageProperties = $props[$id];
-               }
+               $pageProperties = $props[$id] ?? [];
 
                // Basic information
                $pageInfo = [];
                $pageInfo['header-basic'] = [];
 
                // Display title
-               $displayTitle = $title->getPrefixedText();
-               if ( isset( $pageProperties['displaytitle'] ) ) {
-                       $displayTitle = $pageProperties['displaytitle'];
-               }
+               $displayTitle = $pageProperties['displaytitle'] ?? $title->getPrefixedText();
 
                $pageInfo['header-basic'][] = [
                        $this->msg( 'pageinfo-display-title' ), $displayTitle
@@ -254,10 +248,7 @@ class InfoAction extends FormlessAction {
                }
 
                // Default sort key
-               $sortKey = $title->getCategorySortkey();
-               if ( isset( $pageProperties['defaultsort'] ) ) {
-                       $sortKey = $pageProperties['defaultsort'];
-               }
+               $sortKey = $pageProperties['defaultsort'] ?? $title->getCategorySortkey();
 
                $sortKey = htmlspecialchars( $sortKey );
                $pageInfo['header-basic'][] = [ $this->msg( 'pageinfo-default-sort' ), $sortKey ];
index be12977..2aec66f 100644 (file)
@@ -149,11 +149,7 @@ class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase {
                $miser_ns = null;
 
                if ( $mode == 'all' ) {
-                       if ( $params['namespace'] !== null ) {
-                               $namespaces = $params['namespace'];
-                       } else {
-                               $namespaces = MWNamespace::getValidNamespaces();
-                       }
+                       $namespaces = $params['namespace'] ?? MWNamespace::getValidNamespaces();
                        $this->addWhereFld( 'ar_namespace', $namespaces );
 
                        // For from/to/prefix, we have to consider the potential
index 14f1cc4..c7a0cbc 100644 (file)
@@ -129,15 +129,15 @@ class ApiQueryAllImages extends ApiQueryGeneratorBase {
                        if ( !is_null( $params['continue'] ) ) {
                                $cont = explode( '|', $params['continue'] );
                                $this->dieContinueUsageIf( count( $cont ) != 1 );
-                               $op = ( $ascendingOrder ? '>' : '<' );
+                               $op = $ascendingOrder ? '>' : '<';
                                $continueFrom = $db->addQuotes( $cont[0] );
                                $this->addWhere( "img_name $op= $continueFrom" );
                        }
 
                        // Image filters
-                       $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
-                       $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
-                       $this->addWhereRange( 'img_name', ( $ascendingOrder ? 'newer' : 'older' ), $from, $to );
+                       $from = $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE );
+                       $to = $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE );
+                       $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', $from, $to );
 
                        if ( isset( $params['prefix'] ) ) {
                                $this->addWhere( 'img_name' . $db->buildLike(
@@ -210,7 +210,7 @@ class ApiQueryAllImages extends ApiQueryGeneratorBase {
                                                'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
                                        ]
                                ] ] );
-                               $groupCond = ( $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL' );
+                               $groupCond = $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL';
                                $this->addWhere( "ug_group IS $groupCond" );
                        }
                }
index 057dbb2..8377e74 100644 (file)
@@ -150,10 +150,10 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                }
 
                // 'continue' always overrides 'from'
-               $from = $continue || $params['from'] === null ? null :
-                       $this->titlePartToKey( $params['from'], $namespace ) );
-               $to = $params['to'] === null ? null :
-                       $this->titlePartToKey( $params['to'], $namespace ) );
+               $from = $continue || $params['from'] === null ? null :
+                       $this->titlePartToKey( $params['from'], $namespace );
+               $to = $params['to'] === null ? null :
+                       $this->titlePartToKey( $params['to'], $namespace );
                $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to );
 
                if ( isset( $params['prefix'] ) ) {
index d07df5a..6848fcb 100644 (file)
@@ -106,7 +106,7 @@ class ApiQueryContributors extends ApiQueryBase {
                                // some other module used up all the space. Just set a dummy
                                // continue and hope it works next time.
                                $this->setContinueEnumParameter( 'continue',
-                                       $params['continue'] !== null ? $params['continue'] : '0|0'
+                                       $params['continue'] ?? '0|0'
                                );
 
                                return;
index e447f4f..040b735 100644 (file)
@@ -127,7 +127,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                        if ( count( $pageIds[NS_FILE] ) == 1 ) {
                                                // See the 'the user is screwed' comment below
                                                $this->setContinueEnumParameter( 'start',
-                                                       $start !== null ? $start : wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
+                                                       $start ?? wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
                                                );
                                        } else {
                                                $this->setContinueEnumParameter( 'continue',
@@ -152,7 +152,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                                // thing again. When the violating queries have been
                                                // out-continued, the result will get through
                                                $this->setContinueEnumParameter( 'start',
-                                                       $start !== null ? $start : wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
+                                                       $start ?? wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
                                                );
                                        } else {
                                                $this->setContinueEnumParameter( 'continue',
index 84e12d7..8fd1ed5 100644 (file)
@@ -321,7 +321,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                        }
                        if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
                                if ( $this->fld_user ) {
-                                       $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
+                                       $vals['user'] = $row->user_name ?? $row->log_user_text;
                                }
                                if ( $this->fld_userid ) {
                                        $vals['userid'] = intval( $row->log_user );
index 40826ae..84ab3ea 100644 (file)
@@ -80,7 +80,7 @@ class ApiSetPageLanguage extends ApiBase {
                        $this,
                        $titleObj,
                        $params['lang'],
-                       $params['reason'] === null ? '' : $params['reason'],
+                       $params['reason'] ?? '',
                        $params['tags'] ?: []
                );
 
index edb54ae..822d917 100644 (file)
@@ -55,7 +55,7 @@ class CloneDatabase {
                $this->db = $db;
                $this->tablesToClone = $tablesToClone;
                $this->newTablePrefix = $newTablePrefix;
-               $this->oldTablePrefix = $oldTablePrefix !== null ? $oldTablePrefix : $this->db->tablePrefix();
+               $this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix();
                $this->dropCurrentTables = $dropCurrentTables;
        }
 
index 83fe65a..97e4b50 100644 (file)
@@ -968,11 +968,7 @@ abstract class HTMLFormField {
        }
 
        public function getDefault() {
-               if ( isset( $this->mDefault ) ) {
-                       return $this->mDefault;
-               } else {
-                       return null;
-               }
+               return $this->mDefault ?? null;
        }
 
        /**
index 2119289..206048b 100644 (file)
@@ -115,7 +115,7 @@ abstract class FileOp {
                if ( FileBackend::isStoragePath( $path ) ) {
                        $res = FileBackend::normalizeStoragePath( $path );
 
-                       return ( $res !== null ) ? $res : $path;
+                       return $res ?? $path;
                }
 
                return $path;
index e9e338d..b329587 100644 (file)
@@ -821,7 +821,7 @@ class ManualLogEntry extends LogEntryBase {
        }
 
        public function getTimestamp() {
-               $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
+               $ts = $this->timestamp ?? wfTimestampNow();
 
                return wfTimestamp( TS_MW, $ts );
        }
index b98d7f1..f647a9d 100644 (file)
@@ -971,11 +971,7 @@ class FormatMetadata extends ContextSource {
 
                                        case 'LanguageCode':
                                                $lang = Language::fetchLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
-                                               if ( $lang ) {
-                                                       $val = htmlspecialchars( $lang );
-                                               } else {
-                                                       $val = htmlspecialchars( $val );
-                                               }
+                                               $val = htmlspecialchars( $lang ?: $val );
                                                break;
 
                                        default:
index 5728a52..e82779a 100644 (file)
@@ -205,7 +205,7 @@ class SearchResultSet implements Countable, IteratorAggregate {
                $it = $this->bcIterator();
                $searchResult = $it->current();
                $it->next();
-               return $searchResult === null ? false : $searchResult;
+               return $searchResult ?? false;
        }
 
        /**
@@ -338,11 +338,7 @@ class SearchResultSet implements Countable, IteratorAggregate {
                        return;
                }
                $result->setExtensionData( function () use ( $id ) {
-                       if ( isset( $this->extraData[$id] ) ) {
-                               return $this->extraData[$id];
-                       } else {
-                               return [];
-                       }
+                       return $this->extraData[$id] ?? [];
                } );
        }
 
index b1da25c..54d9e9c 100644 (file)
@@ -198,7 +198,7 @@ class DBSiteStore implements SiteStore {
                                'site_type' => $site->getType(),
                                'site_group' => $site->getGroup(),
                                'site_source' => $site->getSource(),
-                               'site_language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
+                               'site_language' => $site->getLanguageCode() ?? '',
                                'site_protocol' => $site->getProtocol(),
                                'site_domain' => strrev( $site->getDomain() ) . '.',
                                'site_data' => serialize( $site->getExtraData() ),
index 655b495..96f50bf 100644 (file)
@@ -865,7 +865,7 @@ abstract class QueryPage extends SpecialPage {
 
                $batch = new LinkBatch;
                foreach ( $res as $row ) {
-                       $batch->add( $ns !== null ? $ns : $row->namespace, $row->title );
+                       $batch->add( $ns ?? $row->namespace, $row->title );
                }
                $batch->execute();
 
index 836dfcd..f5e2b86 100644 (file)
@@ -50,11 +50,7 @@ class SpecialContributions extends IncludableSpecialPage {
                $this->opts = [];
                $request = $this->getRequest();
 
-               if ( $par !== null ) {
-                       $target = $par;
-               } else {
-                       $target = $request->getVal( 'target' );
-               }
+               $target = $par ?? $request->getVal( 'target' );
 
                if ( $request->getVal( 'contribs' ) == 'newbie' || $par === 'newbies' ) {
                        $target = 'newbies';
index e6d81c9..cf0ca48 100644 (file)
@@ -103,7 +103,7 @@ class FileDuplicateSearchPage extends QueryPage {
                $this->setHeaders();
                $this->outputHeader();
 
-               $this->filename = $par !== null ? $par : $this->getRequest()->getText( 'filename' );
+               $this->filename = $par ?? $this->getRequest()->getText( 'filename' );
                $this->file = null;
                $this->hash = '';
                $title = Title::newFromText( $this->filename, NS_FILE );
index 3741272..52db060 100644 (file)
@@ -163,7 +163,7 @@ class SpecialPageLanguage extends FormSpecialPage {
                        $this->getContext(),
                        $title,
                        $newLanguage,
-                       $data['reason'] === null ? '' : $data['reason']
+                       $data['reason'] ?? ''
                );
        }
 
index f826844..13259c9 100644 (file)
@@ -259,7 +259,7 @@ class SpecialSearch extends SpecialPage {
                        return null;
                }
 
-               return $url === null ? $title->getFullUrlForRedirect() : $url;
+               return $url ?? $title->getFullUrlForRedirect();
        }
 
        /**
index 0a35178..4205188 100644 (file)
@@ -87,11 +87,7 @@ class UserrightsPage extends SpecialPage {
 
                $out->addModules( [ 'mediawiki.special.userrights' ] );
 
-               if ( $par !== null ) {
-                       $this->mTarget = $par;
-               } else {
-                       $this->mTarget = $request->getVal( 'user' );
-               }
+               $this->mTarget = $par ?? $request->getVal( 'user' );
 
                if ( is_string( $this->mTarget ) ) {
                        $this->mTarget = trim( $this->mTarget );
index 0d4b5ab..3e97923 100644 (file)
@@ -235,7 +235,7 @@ class ProtectedPagesPager extends TablePager {
                                                $this->getUser()
                                        ) ) {
                                                $value = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
-                                               $formatted = Linker::formatComment( $value !== null ? $value : '' );
+                                               $formatted = Linker::formatComment( $value ?? '' );
                                        } else {
                                                $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
                                        }
index 3c4d1f9..01aace0 100644 (file)
@@ -84,7 +84,7 @@ class UpdateSpecialPages extends Maintenance {
                                if ( $queryPage->isExpensive() ) {
                                        $t1 = microtime( true );
                                        # Do the query
-                                       $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
+                                       $num = $queryPage->recache( $limit ?? $wgQueryCacheLimit );
                                        $t2 = microtime( true );
                                        if ( $num === false ) {
                                                $this->output( "FAILED: database error\n" );
index f0c78ec..cadd0ff 100644 (file)
@@ -143,7 +143,7 @@ class ResourceLoaderTestModule extends ResourceLoaderModule {
        }
 
        public function shouldEmbedModule( ResourceLoaderContext $context ) {
-               return $this->shouldEmbed !== null ? $this->shouldEmbed : parent::shouldEmbedModule( $context );
+               return $this->shouldEmbed ?? parent::shouldEmbedModule( $context );
        }
 
        public function enableModuleContentVersion() {
index 2924812..3a1f078 100644 (file)
@@ -74,7 +74,7 @@ class DerivedPageDataUpdaterTest extends MediaWikiTestCase {
                $comment = CommentStoreComment::newUnsavedComment( $summary );
 
                if ( !$content instanceof Content ) {
-                       $content = new WikitextContent( $content === null ? $summary : $content );
+                       $content = new WikitextContent( $content ?? $summary );
                }
 
                $this->getDerivedPageDataUpdater( $page ); // flush cached instance before.
index bdabf9c..f01c6ba 100644 (file)
@@ -235,7 +235,7 @@ class PageUpdaterTest extends MediaWikiTestCase {
                $comment = CommentStoreComment::newUnsavedComment( $summary );
 
                if ( !$content instanceof Content ) {
-                       $content = new TextContent( $content === null ? $summary : $content );
+                       $content = new TextContent( $content ?? $summary );
                }
 
                $updater = $page->newPageUpdater( $user );
index c1df365..aeaf273 100644 (file)
@@ -519,7 +519,7 @@ class CookieSessionProviderTest extends MediaWikiTestCase {
 
                $normalExpiry = $config->get( 'CookieExpiration' );
                $extendedExpiry = $config->get( 'ExtendedLoginCookieExpiration' );
-               $extendedExpiry = (int)( $extendedExpiry === null ? 0 : $extendedExpiry );
+               $extendedExpiry = (int)( $extendedExpiry ?? 0 );
                $expect = [
                        'MySessionName' => [
                                'value' => (string)$sessionId,