From d1a497a9f4c2a5c50914e19aee1e97f849cef6ca Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Mon, 11 Mar 2019 10:29:20 +0100 Subject: [PATCH] Avoid a few overly complicated is_null() checks We can use the ?? syntax introduced with PHP 7.0 because it is already supported by HHVM, even when HHVM is set to be compatible with PHP 5.6. This was inspired by Ib117e05. Change-Id: If980839fe6f86f2b4e86bbe99905a796e4708c7c --- includes/PathRouter.php | 2 +- includes/Revision/MainSlotRoleHandler.php | 2 +- includes/Title.php | 4 +--- includes/api/ApiParse.php | 4 ++-- includes/page/Article.php | 10 ++++++++-- includes/page/WikiPage.php | 4 +++- includes/specials/SpecialEmailuser.php | 4 +--- includes/specials/SpecialMovepage.php | 2 +- includes/specials/SpecialProtectedpages.php | 3 +-- tests/phpunit/includes/content/WikitextContentTest.php | 3 ++- tests/phpunit/includes/page/WikiPageDbTestBase.php | 8 +++++--- 11 files changed, 26 insertions(+), 20 deletions(-) diff --git a/includes/PathRouter.php b/includes/PathRouter.php index faf4db4f62..db12e74c42 100644 --- a/includes/PathRouter.php +++ b/includes/PathRouter.php @@ -252,7 +252,7 @@ class PathRouter { // array() (a match with no data) but our WebRequest caller // expects array() even when we have no matches so return // a array() when we have null - return is_null( $matches ) ? [] : $matches; + return $matches ?? []; } /** diff --git a/includes/Revision/MainSlotRoleHandler.php b/includes/Revision/MainSlotRoleHandler.php index 6c6fdd6d05..f3e5a8570d 100644 --- a/includes/Revision/MainSlotRoleHandler.php +++ b/includes/Revision/MainSlotRoleHandler.php @@ -120,7 +120,7 @@ class MainSlotRoleHandler extends SlotRoleHandler { case 'json': return CONTENT_MODEL_JSON; default: - return is_null( $model ) ? CONTENT_MODEL_TEXT : $model; + return $model ?? CONTENT_MODEL_TEXT; } } diff --git a/includes/Title.php b/includes/Title.php index 82e79b379c..c6e78e652c 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -4995,9 +4995,7 @@ class Title implements LinkTarget, IDBAccessObject { public function canUseNoindex() { global $wgExemptFromUserRobotsControl; - $bannedNamespaces = is_null( $wgExemptFromUserRobotsControl ) - ? MWNamespace::getContentNamespaces() - : $wgExemptFromUserRobotsControl; + $bannedNamespaces = $wgExemptFromUserRobotsControl ?? MWNamespace::getContentNamespaces(); return !in_array( $this->mNamespace, $bannedNamespaces ); } diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index fc730e3ceb..5e4639d6f5 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -632,8 +632,8 @@ class ApiParse extends ApiBase { */ private function formatSummary( $title, $params ) { global $wgParser; - $summary = !is_null( $params['summary'] ) ? $params['summary'] : ''; - $sectionTitle = !is_null( $params['sectiontitle'] ) ? $params['sectiontitle'] : ''; + $summary = $params['summary'] ?? ''; + $sectionTitle = $params['sectiontitle'] ?? ''; if ( $this->section === 'new' && ( $sectionTitle === '' || $summary === '' ) ) { if ( $sectionTitle !== '' ) { diff --git a/includes/page/Article.php b/includes/page/Article.php index 6f3162dc5c..d71cdcb5f4 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -2857,7 +2857,10 @@ class Article implements Page { * @return array */ public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails, User $user = null ) { - $user = is_null( $user ) ? $this->getContext()->getUser() : $user; + if ( !$user ) { + $user = $this->getContext()->getUser(); + } + return $this->mPage->doRollback( $fromP, $summary, $token, $bot, $resultDetails, $user ); } @@ -2870,7 +2873,10 @@ class Article implements Page { * @return array */ public function commitRollback( $fromP, $summary, $bot, &$resultDetails, User $guser = null ) { - $guser = is_null( $guser ) ? $this->getContext()->getUser() : $guser; + if ( !$guser ) { + $guser = $this->getContext()->getUser(); + } + return $this->mPage->commitRollback( $fromP, $summary, $bot, $resultDetails, $guser ); } diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php index 32b35f7d02..cd0fa8be22 100644 --- a/includes/page/WikiPage.php +++ b/includes/page/WikiPage.php @@ -2626,7 +2626,9 @@ class WikiPage implements Page, IDBAccessObject { // Avoid PHP 7.1 warning of passing $this by reference $wikiPage = $this; - $deleter = is_null( $deleter ) ? $wgUser : $deleter; + if ( !$deleter ) { + $deleter = $wgUser; + } if ( !Hooks::run( 'ArticleDelete', [ &$wikiPage, &$deleter, &$reason, &$error, &$status, $suppress ] ) ) { diff --git a/includes/specials/SpecialEmailuser.php b/includes/specials/SpecialEmailuser.php index 10a9e966c6..887f90541d 100644 --- a/includes/specials/SpecialEmailuser.php +++ b/includes/specials/SpecialEmailuser.php @@ -108,9 +108,7 @@ class SpecialEmailUser extends UnlistedSpecialPage { $request = $this->getRequest(); $out->addModuleStyles( 'mediawiki.special' ); - $this->mTarget = is_null( $par ) - ? $request->getVal( 'wpTarget', $request->getVal( 'target', '' ) ) - : $par; + $this->mTarget = $par ?? $request->getVal( 'wpTarget', $request->getVal( 'target', '' ) ); // Make sure, that HTMLForm uses the correct target. $request->setVal( 'wpTarget', $this->mTarget ); diff --git a/includes/specials/SpecialMovepage.php b/includes/specials/SpecialMovepage.php index 0234507a13..8b5562f9c7 100644 --- a/includes/specials/SpecialMovepage.php +++ b/includes/specials/SpecialMovepage.php @@ -75,7 +75,7 @@ class MovePageForm extends UnlistedSpecialPage { $this->outputHeader(); $request = $this->getRequest(); - $target = !is_null( $par ) ? $par : $request->getVal( 'target' ); + $target = $par ?? $request->getVal( 'target' ); // Yes, the use of getVal() and getText() is wanted, see T22365 diff --git a/includes/specials/SpecialProtectedpages.php b/includes/specials/SpecialProtectedpages.php index d99de1e2fa..e9dca35923 100644 --- a/includes/specials/SpecialProtectedpages.php +++ b/includes/specials/SpecialProtectedpages.php @@ -46,8 +46,7 @@ class SpecialProtectedpages extends SpecialPage { $size = $request->getIntOrNull( 'size' ); $ns = $request->getIntOrNull( 'namespace' ); - $filters = $request->getArray( 'wpfilters' ); - $filters = is_null( $filters ) ? [] : $filters; + $filters = $request->getArray( 'wpfilters', [] ); $indefOnly = in_array( 'indefonly', $filters ); $cascadeOnly = in_array( 'cascadeonly', $filters ); $noRedirect = in_array( 'noredirect', $filters ); diff --git a/tests/phpunit/includes/content/WikitextContentTest.php b/tests/phpunit/includes/content/WikitextContentTest.php index 2fc7794b78..cd7cc1010d 100644 --- a/tests/phpunit/includes/content/WikitextContentTest.php +++ b/tests/phpunit/includes/content/WikitextContentTest.php @@ -182,9 +182,10 @@ just a test" */ public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) { $content = $this->newContent( $text ); + /** @var WikitextContent $c */ $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle ); - $this->assertEquals( $expected, is_null( $c ) ? null : $c->getText() ); + $this->assertEquals( $expected, $c ? $c->getText() : null ); } /** diff --git a/tests/phpunit/includes/page/WikiPageDbTestBase.php b/tests/phpunit/includes/page/WikiPageDbTestBase.php index e4134b8ff7..ac5fef9389 100644 --- a/tests/phpunit/includes/page/WikiPageDbTestBase.php +++ b/tests/phpunit/includes/page/WikiPageDbTestBase.php @@ -851,7 +851,7 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase { # now, test the actual redirect $t = $page->getRedirectTarget(); - $this->assertEquals( $target, is_null( $t ) ? null : $t->getFullText() ); + $this->assertEquals( $target, $t ? $t->getFullText() : null ); } /** @@ -1109,9 +1109,10 @@ more stuff $page = $this->createPage( $title, $text, $model ); $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() ); + /** @var TextContent $c */ $c = $page->replaceSectionContent( $section, $content, $sectionTitle ); - $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getText() ) ); + $this->assertEquals( $expected, $c ? trim( $c->getText() ) : null ); } /** @@ -1125,9 +1126,10 @@ more stuff $baseRevId = $page->getLatest(); $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() ); + /** @var TextContent $c */ $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId ); - $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getText() ) ); + $this->assertEquals( $expected, $c ? trim( $c->getText() ) : null ); } /** -- 2.20.1