Safe replacement of a lot of `!count()` with `=== []`
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Wed, 9 Jan 2019 16:24:36 +0000 (17:24 +0100)
committerThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Tue, 15 Jan 2019 16:28:49 +0000 (17:28 +0100)
This was originally a global search and replace. I manually checked all
replacements and reverted them if (due to the lack of type hints) either
null (that would be 0 when counted) or a Countable object can end in the
variable or property in question.

Now this patch only touches places where I'm sure nothing can break.

For the sanity of the honorable reviewers this patch is exclusively touching
negated counts. You should not find a single `!== []` in this patch, that
would be a mistake.

Change-Id: I5eafd4d8fccdb53a668be8e6f25a566f9c3a0a95

40 files changed:
includes/Block.php
includes/HistoryBlob.php
includes/api/ApiQueryAllUsers.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryContributors.php
includes/api/ApiQueryInfo.php
includes/api/ApiQueryPageProps.php
includes/api/ApiQueryUserContribs.php
includes/clientpool/SquidPurgeClientPool.php
includes/db/DatabaseOracle.php
includes/editpage/TextboxBuilder.php
includes/filerepo/LocalRepo.php
includes/filerepo/file/ArchivedFile.php
includes/jobqueue/JobQueue.php
includes/jobqueue/JobQueueDB.php
includes/jobqueue/JobQueueGroup.php
includes/jobqueue/JobQueueRedis.php
includes/libs/filebackend/FileBackend.php
includes/libs/filebackend/filejournal/FileJournal.php
includes/libs/lockmanager/FSLockManager.php
includes/libs/lockmanager/PostgreSqlLockManager.php
includes/libs/lockmanager/QuorumLockManager.php
includes/libs/rdbms/ChronologyProtector.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMysqlBase.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
includes/mail/EmailNotification.php
includes/parser/Preprocessor_DOM.php
includes/resourceloader/ResourceLoader.php
includes/search/SearchOracle.php
includes/search/SearchSqlite.php
includes/skins/Skin.php
includes/specials/SpecialEditWatchlist.php
includes/specials/SpecialListgrants.php
includes/specials/SpecialPasswordPolicies.php
includes/specials/SpecialRecentchanges.php
includes/specials/SpecialSearch.php
includes/specials/SpecialSpecialpages.php
includes/specials/SpecialTrackingCategories.php
includes/user/User.php

index fb3caf6..7138301 100644 (file)
@@ -1307,7 +1307,7 @@ class Block {
         * @since 1.22
         */
        public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
-               if ( !count( $ipChain ) ) {
+               if ( $ipChain === [] ) {
                        return [];
                }
 
@@ -1332,7 +1332,7 @@ class Block {
                        $conds[] = self::getRangeCond( IP::toHex( $ipaddr ) );
                }
 
-               if ( !count( $conds ) ) {
+               if ( $conds === [] ) {
                        return [];
                }
 
@@ -1388,7 +1388,7 @@ class Block {
         * @return Block|null The "best" block from the list
         */
        public static function chooseBlock( array $blocks, array $ipChain ) {
-               if ( !count( $blocks ) ) {
+               if ( $blocks === [] ) {
                        return null;
                } elseif ( count( $blocks ) == 1 ) {
                        return $blocks[0];
index 1d4f6e4..bca6c7e 100644 (file)
@@ -445,8 +445,7 @@ class DiffHistoryBlob implements HistoryBlob {
                        // Already compressed
                        return;
                }
-               if ( !count( $this->mItems ) ) {
-                       // Empty
+               if ( $this->mItems === [] ) {
                        return;
                }
 
@@ -492,7 +491,7 @@ class DiffHistoryBlob implements HistoryBlob {
                $this->mDiffs = [];
                $this->mDiffMap = [];
                foreach ( $sequences as $seq ) {
-                       if ( !count( $seq['diffs'] ) ) {
+                       if ( $seq['diffs'] === [] ) {
                                continue;
                        }
                        if ( $tail === '' ) {
@@ -627,8 +626,7 @@ class DiffHistoryBlob implements HistoryBlob {
         */
        function __sleep() {
                $this->compress();
-               if ( !count( $this->mItems ) ) {
-                       // Empty object
+               if ( $this->mItems === [] ) {
                        $info = false;
                } else {
                        // Take forward differences to improve the compression ratio for sequences
index 7d5f6e2..7b5df50 100644 (file)
@@ -94,7 +94,7 @@ class ApiQueryAllUsers extends ApiQueryBase {
                        }
 
                        // no group with the given right(s) exists, no need for a query
-                       if ( !count( $groups ) ) {
+                       if ( $groups === [] ) {
                                $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' );
 
                                return;
index d9fe50b..c92f037 100644 (file)
@@ -277,7 +277,7 @@ abstract class ApiQueryBase extends ApiBase {
                if ( count( $ids ) ) {
                        $ids = $this->filterIDs( [ [ $table, $field ] ], $ids );
 
-                       if ( !count( $ids ) ) {
+                       if ( $ids === [] ) {
                                // Return nothing, no IDs are valid
                                $this->where[] = '0 = 1';
                        } else {
index 642c9ac..a8f970e 100644 (file)
@@ -64,7 +64,7 @@ class ApiQueryContributors extends ApiQueryBase {
                                return $v >= $cont_page;
                        } );
                }
-               if ( !count( $pages ) ) {
+               if ( $pages === [] ) {
                        // Nothing to do
                        return;
                }
index 2ab3c56..8a54c0b 100644 (file)
@@ -721,7 +721,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $getTitles[] = $t->getTalkPage();
                        }
                }
-               if ( !count( $getTitles ) ) {
+               if ( $getTitles === [] ) {
                        return;
                }
 
@@ -751,7 +751,7 @@ class ApiQueryInfo extends ApiQueryBase {
 
                $pageIds = array_keys( $this->titles );
 
-               if ( !count( $pageIds ) ) {
+               if ( $pageIds === [] ) {
                        return;
                }
 
@@ -768,7 +768,7 @@ class ApiQueryInfo extends ApiQueryBase {
        }
 
        private function getVariantTitles() {
-               if ( !count( $this->titles ) ) {
+               if ( $this->titles === [] ) {
                        return;
                }
                $this->variantTitles = [];
index 2bee698..3258004 100644 (file)
@@ -50,7 +50,7 @@ class ApiQueryPageProps extends ApiQueryBase {
                        $pages = $filteredPages;
                }
 
-               if ( !count( $pages ) ) {
+               if ( $pages === [] ) {
                        # Nothing to do
                        return;
                }
index ed83130..6082617 100644 (file)
@@ -136,7 +136,7 @@ class ApiQueryUserContribs extends ApiQueryBase {
                        // prepareQuery might try to sort by actor and confuse everything.
                        $batchSize = 1;
                } elseif ( isset( $this->params['userids'] ) ) {
-                       if ( !count( $this->params['userids'] ) ) {
+                       if ( $this->params['userids'] === [] ) {
                                $encParamName = $this->encodeParamName( 'userids' );
                                $this->dieWithError( [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName" );
                        }
index f6109f1..6dd85e7 100644 (file)
@@ -61,9 +61,10 @@ class SquidPurgeClientPool {
                                        $writeSockets["$clientIndex/$i"] = $socket;
                                }
                        }
-                       if ( !count( $readSockets ) && !count( $writeSockets ) ) {
+                       if ( $readSockets === [] && $writeSockets === [] ) {
                                break;
                        }
+
                        $exceptSockets = null;
                        $timeout = min( $startTime + $this->timeout - microtime( true ), 1 );
                        Wikimedia\suppressWarnings();
index 628b47b..6af6de5 100644 (file)
@@ -589,7 +589,7 @@ class DatabaseOracle extends Database {
        public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
                $fname = __METHOD__
        ) {
-               if ( !count( $rows ) ) {
+               if ( $rows === [] ) {
                        return true; // nothing to do
                }
 
index 81dc78d..354cc61 100644 (file)
@@ -58,7 +58,7 @@ class TextboxBuilder {
         * @return mixed[]
         */
        public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
-               if ( !count( $classes ) ) {
+               if ( $classes === [] ) {
                        return $attribs;
                }
 
index b3eae90..bb65b0a 100644 (file)
@@ -405,7 +405,7 @@ class LocalRepo extends FileRepo {
         * @return array[] An Array of arrays or iterators of file objects and the hash as key
         */
        function findBySha1s( array $hashes ) {
-               if ( !count( $hashes ) ) {
+               if ( $hashes === [] ) {
                        return []; // empty parameter
                }
 
index 4a84cff..6a3e819 100644 (file)
@@ -169,7 +169,7 @@ class ArchivedFile {
                        $conds['fa_sha1'] = $this->sha1;
                }
 
-               if ( !count( $conds ) ) {
+               if ( $conds === [] ) {
                        throw new MWException( "No specific information for retrieving archived file" );
                }
 
index 3689ba4..4f4728d 100644 (file)
@@ -323,7 +323,7 @@ abstract class JobQueue {
        final public function batchPush( array $jobs, $flags = 0 ) {
                $this->assertNotReadOnly();
 
-               if ( !count( $jobs ) ) {
+               if ( $jobs === [] ) {
                        return; // nothing to do
                }
 
index 9931d83..fa17284 100644 (file)
@@ -214,7 +214,7 @@ class JobQueueDB extends JobQueue {
         * @return void
         */
        public function doBatchPushInternal( IDatabase $dbw, array $jobs, $flags, $method ) {
-               if ( !count( $jobs ) ) {
+               if ( $jobs === [] ) {
                        return;
                }
 
index b103b8e..4853c4a 100644 (file)
@@ -143,7 +143,7 @@ class JobQueueGroup {
                }
 
                $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
-               if ( !count( $jobs ) ) {
+               if ( $jobs === [] ) {
                        return;
                }
 
index b868128..a1ef28b 100644 (file)
@@ -203,7 +203,7 @@ class JobQueueRedis extends JobQueue {
                        }
                }
 
-               if ( !count( $items ) ) {
+               if ( $items === [] ) {
                        return; // nothing to do
                }
 
index 27e6924..e32d496 100644 (file)
@@ -417,7 +417,7 @@ abstract class FileBackend implements LoggerAwareInterface {
                if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
                        return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
                }
-               if ( !count( $ops ) ) {
+               if ( $ops === [] ) {
                        return $this->newStatus(); // nothing to do
                }
 
@@ -655,7 +655,7 @@ abstract class FileBackend implements LoggerAwareInterface {
                if ( empty( $opts['bypassReadOnly'] ) && $this->isReadOnly() ) {
                        return $this->newStatus( 'backend-fail-readonly', $this->name, $this->readOnly );
                }
-               if ( !count( $ops ) ) {
+               if ( $ops === [] ) {
                        return $this->newStatus(); // nothing to do
                }
 
index 47be4eb..999594b 100644 (file)
@@ -97,7 +97,7 @@ abstract class FileJournal {
         * @return StatusValue
         */
        final public function logChangeBatch( array $entries, $batchId ) {
-               if ( !count( $entries ) ) {
+               if ( $entries === [] ) {
                        return StatusValue::newGood();
                }
 
index f2624e7..019029c 100644 (file)
@@ -169,7 +169,7 @@ class FSLockManager extends LockManager {
                        if ( $this->locksHeld[$path][$type] <= 0 ) {
                                unset( $this->locksHeld[$path][$type] );
                        }
-                       if ( !count( $this->locksHeld[$path] ) ) {
+                       if ( $this->locksHeld[$path] === [] ) {
                                unset( $this->locksHeld[$path] ); // no locks on this path
                                if ( isset( $this->handles[$path] ) ) {
                                        $handlesToClose[] = $this->handles[$path];
index 65c6993..fd3ffa5 100644 (file)
@@ -18,7 +18,7 @@ class PostgreSqlLockManager extends DBLockManager {
 
        protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) {
                $status = StatusValue::newGood();
-               if ( !count( $paths ) ) {
+               if ( $paths === [] ) {
                        return $status; // nothing to lock
                }
 
index 1d2e21a..1ef4642 100644 (file)
@@ -98,7 +98,7 @@ abstract class QuorumLockManager extends LockManager {
                                                $bucket = $this->getBucketFromPath( $path );
                                                $pathsToUnlock[$bucket][$type][] = $path;
                                        }
-                                       if ( !count( $this->locksHeld[$path] ) ) {
+                                       if ( $this->locksHeld[$path] === [] ) {
                                                unset( $this->locksHeld[$path] ); // no SH or EX locks left for key
                                        }
                                }
@@ -110,7 +110,7 @@ abstract class QuorumLockManager extends LockManager {
                foreach ( $pathsToUnlock as $bucket => $pathsToUnlockByType ) {
                        $status->merge( $this->doUnlockingRequestBucket( $bucket, $pathsToUnlockByType ) );
                }
-               if ( !count( $this->locksHeld ) ) {
+               if ( $this->locksHeld === [] ) {
                        $status->merge( $this->releaseAllLocks() );
                        $this->degradedBuckets = []; // safe to retry the normal quorum
                }
index 938e534..3e71e36 100644 (file)
@@ -202,7 +202,7 @@ class ChronologyProtector implements LoggerAwareInterface {
                        );
                }
 
-               if ( !count( $this->shutdownPositions ) ) {
+               if ( $this->shutdownPositions === [] ) {
                        return []; // nothing to save
                }
 
index 9a9e36a..7d971af 100644 (file)
@@ -2855,7 +2855,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
                $fname = __METHOD__
        ) {
-               if ( !count( $rows ) ) {
+               if ( $rows === [] ) {
                        return true; // nothing to do
                }
 
index 3fcbcf9..186c89f 100644 (file)
@@ -1337,7 +1337,7 @@ abstract class DatabaseMysqlBase extends Database {
        public function upsert( $table, array $rows, array $uniqueIndexes,
                array $set, $fname = __METHOD__
        ) {
-               if ( !count( $rows ) ) {
+               if ( $rows === [] ) {
                        return true; // nothing to do
                }
 
index ca18122..ab5c3cd 100644 (file)
@@ -408,7 +408,7 @@ class LoadBalancer implements ILoadBalancer {
         * @return array (reader index, lagged replica mode) or false on failure
         */
        private function pickReaderIndex( array $loads, $domain = false ) {
-               if ( !count( $loads ) ) {
+               if ( $loads === [] ) {
                        throw new InvalidArgumentException( "Empty server array given to LoadBalancer" );
                }
 
@@ -476,7 +476,7 @@ class LoadBalancer implements ILoadBalancer {
                }
 
                // If all servers were down, quit now
-               if ( !count( $currentLoads ) ) {
+               if ( $currentLoads === [] ) {
                        $this->connLogger->error( __METHOD__ . ": all servers down" );
                }
 
index 8a089f6..76a7760 100644 (file)
@@ -154,7 +154,7 @@ class EmailNotification {
                // If nobody is watching the page, and there are no users notified on all changes
                // don't bother creating a job/trying to send emails, unless it's a
                // talk page with an applicable notification.
-               if ( !count( $watchers ) && !count( $wgUsersNotifiedOnAllChanges ) ) {
+               if ( $watchers === [] && !count( $wgUsersNotifiedOnAllChanges ) ) {
                        $sendEmail = false;
                        // Only send notification for non minor edits, unless $wgEnotifMinorEdits
                        if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
index f4e4efa..3bcd012 100644 (file)
@@ -878,7 +878,7 @@ class PPDStack {
        }
 
        public function pop() {
-               if ( !count( $this->stack ) ) {
+               if ( $this->stack === [] ) {
                        throw new MWException( __METHOD__ . ': no elements remaining' );
                }
                $temp = array_pop( $this->stack );
@@ -902,7 +902,7 @@ class PPDStack {
         * @return array
         */
        public function getFlags() {
-               if ( !count( $this->stack ) ) {
+               if ( $this->stack === [] ) {
                        return [
                                'findEquals' => false,
                                'findPipe' => false,
index 9570e03..c513aed 100644 (file)
@@ -781,7 +781,7 @@ class ResourceLoader implements LoggerAwareInterface {
                }
 
                // Save response to file cache unless there are errors
-               if ( isset( $fileCache ) && !$this->errors && !count( $missing ) ) {
+               if ( isset( $fileCache ) && !$this->errors && $missing === [] ) {
                        // Cache single modules and images...and other requests if there are enough hits
                        if ( ResourceFileCache::useFileCache( $context ) ) {
                                if ( $fileCache->isCacheWorthy() ) {
@@ -1036,7 +1036,7 @@ class ResourceLoader implements LoggerAwareInterface {
                $out = '';
                $states = [];
 
-               if ( !count( $modules ) && !count( $missing ) ) {
+               if ( $modules === [] && $missing === [] ) {
                        return <<<MESSAGE
 /* This file is the Web entry point for MediaWiki's ResourceLoader:
    <https://www.mediawiki.org/wiki/ResourceLoader>. In this request,
index 9cd245a..0cbb41c 100644 (file)
@@ -98,7 +98,7 @@ class SearchOracle extends SearchDatabase {
                if ( is_null( $this->namespaces ) ) {
                        return '';
                }
-               if ( !count( $this->namespaces ) ) {
+               if ( $this->namespaces === [] ) {
                        $namespaces = '0';
                } else {
                        $namespaces = $this->db->makeList( $this->namespaces );
index 6332ea2..f653796 100644 (file)
@@ -199,7 +199,7 @@ class SearchSqlite extends SearchDatabase {
                if ( is_null( $this->namespaces ) ) {
                        return '';  # search all
                }
-               if ( !count( $this->namespaces ) ) {
+               if ( $this->namespaces === [] ) {
                        $namespaces = '0';
                } else {
                        $namespaces = $this->db->makeList( $this->namespaces );
index 08ff8f0..e31bc06 100644 (file)
@@ -519,7 +519,7 @@ abstract class Skin extends ContextSource {
                $out = $this->getOutput();
                $allCats = $out->getCategoryLinks();
 
-               if ( !count( $allCats ) ) {
+               if ( $allCats === [] ) {
                        return '';
                }
 
index 70b4207..b05c81a 100644 (file)
@@ -431,7 +431,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * Attempts to clean up broken items
         */
        private function cleanupWatchlist() {
-               if ( !count( $this->badItems ) ) {
+               if ( $this->badItems === [] ) {
                        return; // nothing to do
                }
 
index 1a04eec..ba16baf 100644 (file)
@@ -62,7 +62,7 @@ class SpecialListGrants extends SpecialPage {
                                        '<span class="mw-listgrants-right-name">' . $permission . '</span>'
                                )->parse();
                        }
-                       if ( !count( $descs ) ) {
+                       if ( $descs === [] ) {
                                $grantCellHtml = '';
                        } else {
                                sort( $descs );
index 0a3a679..573dcb5 100644 (file)
@@ -151,7 +151,7 @@ class SpecialPasswordPolicies extends SpecialPage {
                                '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
                        )->parse();
                }
-               if ( !count( $ret ) ) {
+               if ( $ret === [] ) {
                        return '';
                } else {
                        return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
index 60e797e..b566305 100644 (file)
@@ -709,7 +709,7 @@ class SpecialRecentChanges extends ChangesListSpecialPage {
 
                $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
 
-               if ( !count( $categories ) ) {
+               if ( $categories === [] ) {
                        return;
                }
 
@@ -744,7 +744,7 @@ class SpecialRecentChanges extends ChangesListSpecialPage {
                }
 
                # Shortcut?
-               if ( !count( $articles ) || !count( $cats ) ) {
+               if ( $articles === [] || $cats === [] ) {
                        return;
                }
 
index d904ad1..ec6c5b9 100644 (file)
@@ -212,13 +212,13 @@ class SpecialSearch extends SpecialPage {
 
                # Extract manually requested namespaces
                $nslist = $this->powerSearch( $request );
-               if ( !count( $nslist ) ) {
+               if ( $nslist === [] ) {
                        # Fallback to user preference
                        $nslist = $this->searchConfig->userNamespaces( $user );
                }
 
                $profile = null;
-               if ( !count( $nslist ) ) {
+               if ( $nslist === [] ) {
                        $profile = 'default';
                }
 
index 585a7cd..9de31da 100644 (file)
@@ -55,7 +55,7 @@ class SpecialSpecialpages extends UnlistedSpecialPage {
                $pages = MediaWikiServices::getInstance()->getSpecialPageFactory()->
                        getUsablePages( $this->getUser() );
 
-               if ( !count( $pages ) ) {
+               if ( $pages === [] ) {
                        # Yeah, that was pointless. Thanks for coming.
                        return false;
                }
index 3ee7cea..4a586b7 100644 (file)
@@ -94,7 +94,7 @@ class SpecialTrackingCategories extends SpecialPage {
                        }
 
                        # Extra message, when no category was found
-                       if ( !count( $allMsgs ) ) {
+                       if ( $allMsgs === [] ) {
                                $allMsgs[] = $this->msg( 'trackingcategories-disabled' )->parse();
                        }
 
index 65fc4b4..79889ae 100644 (file)
@@ -1579,7 +1579,7 @@ class User implements IDBAccessObject, UserIdentity {
 
                if ( is_array( $data ) ) {
                        if ( isset( $data['user_groups'] ) && is_array( $data['user_groups'] ) ) {
-                               if ( !count( $data['user_groups'] ) ) {
+                               if ( $data['user_groups'] === [] ) {
                                        $this->mGroupMemberships = [];
                                } else {
                                        $firstGroup = reset( $data['user_groups'] );
@@ -1645,7 +1645,7 @@ class User implements IDBAccessObject, UserIdentity {
                }
 
                $toPromote = Autopromote::getAutopromoteOnceGroups( $this, $event );
-               if ( !count( $toPromote ) ) {
+               if ( $toPromote === [] ) {
                        return [];
                }