From: Fomafix Date: Wed, 20 Jun 2018 05:26:57 +0000 (+0200) Subject: Simplify PHP by using ?? and ?: X-Git-Tag: 1.34.0-rc.0~4816^2 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=6866cfec3731409994b3edf857504e8674c00a8e;p=lhc%2Fweb%2Fwiklou.git Simplify PHP by using ?? and ?: Also remove not necessary surrounding parentheses. Change-Id: I0eb5c9c1bdfb09a800258379cdcefb5fd4d3d21c --- diff --git a/includes/Block.php b/includes/Block.php index a7b80352b3..0d0dde46e0 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -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() ), diff --git a/includes/CommentStore.php b/includes/CommentStore.php index 6b94d58e6c..1fea309a70 100644 --- a/includes/CommentStore.php +++ b/includes/CommentStore.php @@ -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' ); diff --git a/includes/EditPage.php b/includes/EditPage.php index 9a8a4a6a09..b8f52082c9 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -3357,7 +3357,7 @@ ERROR; } $this->showTextbox( - $textoverride !== null ? $textoverride : $this->textbox1, + $textoverride ?? $this->textbox1, 'wpTextbox1', $attribs ); diff --git a/includes/Storage/RevisionStore.php b/includes/Storage/RevisionStore.php index 12bee1e01d..a184986099 100644 --- a/includes/Storage/RevisionStore.php +++ b/includes/Storage/RevisionStore.php @@ -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 ); diff --git a/includes/WikiReference.php b/includes/WikiReference.php index 724ba980a3..d3688c8276 100644 --- a/includes/WikiReference.php +++ b/includes/WikiReference.php @@ -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; } /** diff --git a/includes/actions/InfoAction.php b/includes/actions/InfoAction.php index 0a4eae8781..98dfeb33cb 100644 --- a/includes/actions/InfoAction.php +++ b/includes/actions/InfoAction.php @@ -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 ]; diff --git a/includes/api/ApiQueryAllDeletedRevisions.php b/includes/api/ApiQueryAllDeletedRevisions.php index be12977994..2aec66f2d1 100644 --- a/includes/api/ApiQueryAllDeletedRevisions.php +++ b/includes/api/ApiQueryAllDeletedRevisions.php @@ -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 diff --git a/includes/api/ApiQueryAllImages.php b/includes/api/ApiQueryAllImages.php index 14f1cc4f84..c7a0cbc3c2 100644 --- a/includes/api/ApiQueryAllImages.php +++ b/includes/api/ApiQueryAllImages.php @@ -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" ); } } diff --git a/includes/api/ApiQueryAllLinks.php b/includes/api/ApiQueryAllLinks.php index 057dbb2ad5..8377e7426f 100644 --- a/includes/api/ApiQueryAllLinks.php +++ b/includes/api/ApiQueryAllLinks.php @@ -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'] ) ) { diff --git a/includes/api/ApiQueryContributors.php b/includes/api/ApiQueryContributors.php index d07df5a370..6848fcbd62 100644 --- a/includes/api/ApiQueryContributors.php +++ b/includes/api/ApiQueryContributors.php @@ -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; diff --git a/includes/api/ApiQueryImageInfo.php b/includes/api/ApiQueryImageInfo.php index e447f4f487..040b735b57 100644 --- a/includes/api/ApiQueryImageInfo.php +++ b/includes/api/ApiQueryImageInfo.php @@ -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', diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index 84e12d7dfa..8fd1ed5ca9 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -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 ); diff --git a/includes/api/ApiSetPageLanguage.php b/includes/api/ApiSetPageLanguage.php index 40826ae341..84ab3ea981 100644 --- a/includes/api/ApiSetPageLanguage.php +++ b/includes/api/ApiSetPageLanguage.php @@ -80,7 +80,7 @@ class ApiSetPageLanguage extends ApiBase { $this, $titleObj, $params['lang'], - $params['reason'] === null ? '' : $params['reason'], + $params['reason'] ?? '', $params['tags'] ?: [] ); diff --git a/includes/db/CloneDatabase.php b/includes/db/CloneDatabase.php index edb54aee89..822d917d76 100644 --- a/includes/db/CloneDatabase.php +++ b/includes/db/CloneDatabase.php @@ -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; } diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 83fe65a27d..97e4b50551 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -968,11 +968,7 @@ abstract class HTMLFormField { } public function getDefault() { - if ( isset( $this->mDefault ) ) { - return $this->mDefault; - } else { - return null; - } + return $this->mDefault ?? null; } /** diff --git a/includes/libs/filebackend/fileop/FileOp.php b/includes/libs/filebackend/fileop/FileOp.php index 211928973d..206048bd71 100644 --- a/includes/libs/filebackend/fileop/FileOp.php +++ b/includes/libs/filebackend/fileop/FileOp.php @@ -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; diff --git a/includes/logging/LogEntry.php b/includes/logging/LogEntry.php index e9e338dc3a..b329587b67 100644 --- a/includes/logging/LogEntry.php +++ b/includes/logging/LogEntry.php @@ -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 ); } diff --git a/includes/media/FormatMetadata.php b/includes/media/FormatMetadata.php index b98d7f1e74..f647a9dd79 100644 --- a/includes/media/FormatMetadata.php +++ b/includes/media/FormatMetadata.php @@ -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: diff --git a/includes/search/SearchResultSet.php b/includes/search/SearchResultSet.php index 5728a52304..e82779ab80 100644 --- a/includes/search/SearchResultSet.php +++ b/includes/search/SearchResultSet.php @@ -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] ?? []; } ); } diff --git a/includes/site/DBSiteStore.php b/includes/site/DBSiteStore.php index b1da25c865..54d9e9cf1d 100644 --- a/includes/site/DBSiteStore.php +++ b/includes/site/DBSiteStore.php @@ -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() ), diff --git a/includes/specialpage/QueryPage.php b/includes/specialpage/QueryPage.php index 655b495e77..96f50bf62c 100644 --- a/includes/specialpage/QueryPage.php +++ b/includes/specialpage/QueryPage.php @@ -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(); diff --git a/includes/specials/SpecialContributions.php b/includes/specials/SpecialContributions.php index 836dfcdadc..f5e2b86e2f 100644 --- a/includes/specials/SpecialContributions.php +++ b/includes/specials/SpecialContributions.php @@ -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'; diff --git a/includes/specials/SpecialFileDuplicateSearch.php b/includes/specials/SpecialFileDuplicateSearch.php index e6d81c99fe..cf0ca48e94 100644 --- a/includes/specials/SpecialFileDuplicateSearch.php +++ b/includes/specials/SpecialFileDuplicateSearch.php @@ -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 ); diff --git a/includes/specials/SpecialPageLanguage.php b/includes/specials/SpecialPageLanguage.php index 37412722bb..52db060122 100644 --- a/includes/specials/SpecialPageLanguage.php +++ b/includes/specials/SpecialPageLanguage.php @@ -163,7 +163,7 @@ class SpecialPageLanguage extends FormSpecialPage { $this->getContext(), $title, $newLanguage, - $data['reason'] === null ? '' : $data['reason'] + $data['reason'] ?? '' ); } diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index f8268445b1..13259c9b3a 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -259,7 +259,7 @@ class SpecialSearch extends SpecialPage { return null; } - return $url === null ? $title->getFullUrlForRedirect() : $url; + return $url ?? $title->getFullUrlForRedirect(); } /** diff --git a/includes/specials/SpecialUserrights.php b/includes/specials/SpecialUserrights.php index 0a35178db7..42051886f7 100644 --- a/includes/specials/SpecialUserrights.php +++ b/includes/specials/SpecialUserrights.php @@ -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 ); diff --git a/includes/specials/pagers/ProtectedPagesPager.php b/includes/specials/pagers/ProtectedPagesPager.php index 0d4b5ab92b..3e97923faf 100644 --- a/includes/specials/pagers/ProtectedPagesPager.php +++ b/includes/specials/pagers/ProtectedPagesPager.php @@ -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(); } diff --git a/maintenance/updateSpecialPages.php b/maintenance/updateSpecialPages.php index 3c4d1f9f82..01aace0738 100644 --- a/maintenance/updateSpecialPages.php +++ b/maintenance/updateSpecialPages.php @@ -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" ); diff --git a/tests/phpunit/ResourceLoaderTestCase.php b/tests/phpunit/ResourceLoaderTestCase.php index f0c78ecea0..cadd0ff3c7 100644 --- a/tests/phpunit/ResourceLoaderTestCase.php +++ b/tests/phpunit/ResourceLoaderTestCase.php @@ -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() { diff --git a/tests/phpunit/includes/Storage/DerivedPageDataUpdaterTest.php b/tests/phpunit/includes/Storage/DerivedPageDataUpdaterTest.php index 29248120c2..3a1f078c31 100644 --- a/tests/phpunit/includes/Storage/DerivedPageDataUpdaterTest.php +++ b/tests/phpunit/includes/Storage/DerivedPageDataUpdaterTest.php @@ -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. diff --git a/tests/phpunit/includes/Storage/PageUpdaterTest.php b/tests/phpunit/includes/Storage/PageUpdaterTest.php index bdabf9c0cb..f01c6bac24 100644 --- a/tests/phpunit/includes/Storage/PageUpdaterTest.php +++ b/tests/phpunit/includes/Storage/PageUpdaterTest.php @@ -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 ); diff --git a/tests/phpunit/includes/session/CookieSessionProviderTest.php b/tests/phpunit/includes/session/CookieSessionProviderTest.php index c1df365aac..aeaf2737d5 100644 --- a/tests/phpunit/includes/session/CookieSessionProviderTest.php +++ b/tests/phpunit/includes/session/CookieSessionProviderTest.php @@ -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,