Avoid a few overly complicated is_null() checks
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Mon, 11 Mar 2019 09:29:20 +0000 (10:29 +0100)
committerThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Mon, 11 Mar 2019 09:29:20 +0000 (10:29 +0100)
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
includes/Revision/MainSlotRoleHandler.php
includes/Title.php
includes/api/ApiParse.php
includes/page/Article.php
includes/page/WikiPage.php
includes/specials/SpecialEmailuser.php
includes/specials/SpecialMovepage.php
includes/specials/SpecialProtectedpages.php
tests/phpunit/includes/content/WikitextContentTest.php
tests/phpunit/includes/page/WikiPageDbTestBase.php

index faf4db4..db12e74 100644 (file)
@@ -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 ?? [];
        }
 
        /**
index 6c6fdd6..f3e5a85 100644 (file)
@@ -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;
                        }
                }
 
index 82e79b3..c6e78e6 100644 (file)
@@ -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 );
        }
index fc730e3..5e4639d 100644 (file)
@@ -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 !== '' ) {
index 6f3162d..d71cdcb 100644 (file)
@@ -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 );
        }
 
index 32b35f7..cd0fa8b 100644 (file)
@@ -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 ]
                ) ) {
index 10a9e96..887f905 100644 (file)
@@ -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 );
index 0234507..8b5562f 100644 (file)
@@ -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
 
index d99de1e..e9dca35 100644 (file)
@@ -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 );
index 2fc7794..cd7cc10 100644 (file)
@@ -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 );
        }
 
        /**
index e4134b8..ac5fef9 100644 (file)
@@ -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 );
        }
 
        /**