Merge "Use PHP 7 '??' operator instead of '?:' with 'isset()' where convenient"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 31 May 2018 19:01:07 +0000 (19:01 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 31 May 2018 19:01:07 +0000 (19:01 +0000)
1  2 
includes/GlobalFunctions.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
includes/session/PHPSessionHandler.php

@@@ -1516,7 -1516,7 +1516,7 @@@ function wfBacktrace( $raw = null ) 
  
        $frames = array_map( function ( $frame ) use ( $frameFormat ) {
                $file = !empty( $frame['file'] ) ? basename( $frame['file'] ) : '-';
-               $line = isset( $frame['line'] ) ? $frame['line'] : '-';
+               $line = $frame['line'] ?? '-';
                $call = $frame['function'];
                if ( !empty( $frame['class'] ) ) {
                        $call = $frame['class'] . $frame['type'] . $call;
@@@ -2241,7 -2241,7 +2241,7 @@@ function wfShellExec( $cmd, &$retval = 
        }
  
        $includeStderr = isset( $options['duplicateStderr'] ) && $options['duplicateStderr'];
-       $profileMethod = isset( $options['profileMethod'] ) ? $options['profileMethod'] : wfGetCaller();
+       $profileMethod = $options['profileMethod'] ?? wfGetCaller();
  
        try {
                $result = Shell::command( [] )
@@@ -3150,6 -3150,17 +3150,6 @@@ function wfGetMessageCacheStorage() 
        return ObjectCache::getInstance( $wgMessageCacheType );
  }
  
 -/**
 - * Get the cache object used by the parser cache
 - *
 - * @deprecated since 1.30, use MediaWikiServices::getParserCache()->getCacheStorage()
 - * @return BagOStuff
 - */
 -function wfGetParserCacheStorage() {
 -      global $wgParserCacheType;
 -      return ObjectCache::getInstance( $wgParserCacheType );
 -}
 -
  /**
   * Call hook functions defined in $wgHooks
   *
@@@ -172,9 -172,7 +172,7 @@@ class LoadBalancer implements ILoadBala
                        : DatabaseDomain::newUnspecified();
                $this->setLocalDomain( $localDomain );
  
-               $this->waitTimeout = isset( $params['waitTimeout'] )
-                       ? $params['waitTimeout']
-                       : self::MAX_WAIT_DEFAULT;
+               $this->waitTimeout = $params['waitTimeout'] ?? self::MAX_WAIT_DEFAULT;
  
                $this->readIndex = -1;
                $this->conns = [
                } else {
                        $this->wanCache = WANObjectCache::newEmpty();
                }
-               $this->profiler = isset( $params['profiler'] ) ? $params['profiler'] : null;
+               $this->profiler = $params['profiler'] ?? null;
                if ( isset( $params['trxProfiler'] ) ) {
                        $this->trxProfiler = $params['trxProfiler'];
                } else {
                        $this->trxProfiler = new TransactionProfiler();
                }
  
-               $this->errorLogger = isset( $params['errorLogger'] )
-                       ? $params['errorLogger']
-                       : function ( Exception $e ) {
-                               trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
-                       };
-               $this->deprecationLogger = isset( $params['deprecationLogger'] )
-                       ? $params['deprecationLogger']
-                       : function ( $msg ) {
-                               trigger_error( $msg, E_USER_DEPRECATED );
-                       };
+               $this->errorLogger = $params['errorLogger'] ?? function ( Exception $e ) {
+                       trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
+               };
+               $this->deprecationLogger = $params['deprecationLogger'] ?? function ( $msg ) {
+                       trigger_error( $msg, E_USER_DEPRECATED );
+               };
  
                foreach ( [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ] as $key ) {
-                       $this->$key = isset( $params[$key] ) ? $params[$key] : new NullLogger();
+                       $this->$key = $params[$key] ?? new NullLogger();
                }
  
-               $this->host = isset( $params['hostname'] )
-                       ? $params['hostname']
-                       : ( gethostname() ?: 'unknown' );
-               $this->cliMode = isset( $params['cliMode'] )
-                       ? $params['cliMode']
-                       : ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
-               $this->agent = isset( $params['agent'] ) ? $params['agent'] : '';
+               $this->host = $params['hostname'] ?? ( gethostname() ?: 'unknown' );
+               $this->cliMode = $params['cliMode'] ?? ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
+               $this->agent = $params['agent'] ?? '';
  
                if ( isset( $params['chronologyCallback'] ) ) {
                        $this->chronologyCallback = $params['chronologyCallback'];
                foreach ( $lags as $i => $lag ) {
                        if ( $i != 0 ) {
                                # How much lag this server nominally is allowed to have
-                               $maxServerLag = isset( $this->servers[$i]['max lag'] )
-                                       ? $this->servers[$i]['max lag']
-                                       : $this->maxLag; // default
+                               $maxServerLag = $this->servers[$i]['max lag'] ?? $this->maxLag; // default
                                # Constrain that futher by $maxLag argument
                                $maxServerLag = min( $maxServerLag, $maxLag );
  
        public function getServerAttributes( $i ) {
                return Database::attributesFromType(
                        $this->getServerType( $i ),
-                       isset( $this->servers[$i]['driver'] ) ? $this->servers[$i]['driver'] : null
+                       $this->servers[$i]['driver'] ?? null
                );
        }
  
                $server['agent'] = $this->agent;
                // Use DBO_DEFAULT flags by default for LoadBalancer managed databases. Assume that the
                // application calls LoadBalancer::commitMasterChanges() before the PHP script completes.
-               $server['flags'] = isset( $server['flags'] ) ? $server['flags'] : IDatabase::DBO_DEFAULT;
+               $server['flags'] = $server['flags'] ?? IDatabase::DBO_DEFAULT;
  
                // Create a live connection object
                try {
                return ( $name != '' ) ? $name : 'localhost';
        }
  
 +      public function getServerInfo( $i ) {
 +              if ( isset( $this->servers[$i] ) ) {
 +                      return $this->servers[$i];
 +              } else {
 +                      return false;
 +              }
 +      }
 +
        public function getServerType( $i ) {
-               return isset( $this->servers[$i]['type'] ) ? $this->servers[$i]['type'] : 'unknown';
+               return $this->servers[$i]['type'] ?? 'unknown';
        }
  
        public function getMasterPos() {
        public function approveMasterChanges( array $options ) {
                $this->assertTransactionRoundStage( self::ROUND_FINALIZED );
  
-               $limit = isset( $options['maxWriteDuration'] ) ? $options['maxWriteDuration'] : 0;
+               $limit = $options['maxWriteDuration'] ?? 0;
  
                $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
                $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( $limit ) {
@@@ -162,12 -162,39 +162,12 @@@ class PHPSessionHandler implements \Ses
                }
        }
  
 -      /**
 -       * Workaround for PHP5 bug
 -       *
 -       * PHP5 has a bug in handling boolean return values for
 -       * SessionHandlerInterface methods, it expects 0 or -1 instead of true or
 -       * false. See <https://wiki.php.net/rfc/session.user.return-value>.
 -       *
 -       * PHP7 and HHVM are not affected.
 -       *
 -       * @todo When we drop support for Zend PHP 5, this can be removed.
 -       * @return bool|int
 -       * @codeCoverageIgnore
 -       */
 -      protected static function returnSuccess() {
 -              return defined( 'HHVM_VERSION' ) || version_compare( PHP_VERSION, '7.0.0', '>=' ) ? true : 0;
 -      }
 -
 -      /**
 -       * Workaround for PHP5 bug
 -       * @see self::returnSuccess()
 -       * @return bool|int
 -       * @codeCoverageIgnore
 -       */
 -      protected static function returnFailure() {
 -              return defined( 'HHVM_VERSION' ) || version_compare( PHP_VERSION, '7.0.0', '>=' ) ? false : -1;
 -      }
 -
        /**
         * Initialize the session (handler)
         * @private For internal use only
         * @param string $save_path Path used to store session files (ignored)
         * @param string $session_name Session name (ignored)
 -       * @return bool|int Success (see self::returnSuccess())
 +       * @return true
         */
        public function open( $save_path, $session_name ) {
                if ( self::$instance !== $this ) {
                if ( !$this->enable ) {
                        throw new \BadMethodCallException( 'Attempt to use PHP session management' );
                }
 -              return self::returnSuccess();
 +              return true;
        }
  
        /**
         * Close the session (handler)
         * @private For internal use only
 -       * @return bool|int Success (see self::returnSuccess())
 +       * @return true
         */
        public function close() {
                if ( self::$instance !== $this ) {
                        throw new \UnexpectedValueException( __METHOD__ . ': Wrong instance called!' );
                }
                $this->sessionFieldCache = [];
 -              return self::returnSuccess();
 +              return true;
        }
  
        /**
         * @param string $dataStr Session data. Not that you should ever call this
         *   directly, but note that this has the same issues with code injection
         *   via user-controlled data as does PHP's unserialize function.
 -       * @return bool|int Success (see self::returnSuccess())
 +       * @return bool
         */
        public function write( $id, $dataStr ) {
                if ( self::$instance !== $this ) {
                                [
                                        'session' => $id,
                        ] );
 -                      return self::returnSuccess();
 +                      return true;
                }
  
                // First, decode the string PHP handed us
                $data = \Wikimedia\PhpSessionSerializer::decode( $dataStr );
                if ( $data === null ) {
                        // @codeCoverageIgnoreStart
 -                      return self::returnFailure();
 +                      return false;
                        // @codeCoverageIgnoreEnd
                }
  
                // Now merge the data into the Session object.
                $changed = false;
-               $cache = isset( $this->sessionFieldCache[$id] ) ? $this->sessionFieldCache[$id] : [];
+               $cache = $this->sessionFieldCache[$id] ?? [];
                foreach ( $data as $key => $value ) {
                        if ( !array_key_exists( $key, $cache ) ) {
                                if ( $session->exists( $key ) ) {
  
                $session->persist();
  
 -              return self::returnSuccess();
 +              return true;
        }
  
        /**
         * Destroy a session
         * @private For internal use only
         * @param string $id Session id
 -       * @return bool|int Success (see self::returnSuccess())
 +       * @return true
         */
        public function destroy( $id ) {
                if ( self::$instance !== $this ) {
                if ( $session ) {
                        $session->clear();
                }
 -              return self::returnSuccess();
 +              return true;
        }
  
        /**
         * Execute garbage collection.
         * @private For internal use only
         * @param int $maxlifetime Maximum session life time (ignored)
 -       * @return bool|int Success (see self::returnSuccess())
 +       * @return true
         * @codeCoverageIgnore See T135576
         */
        public function gc( $maxlifetime ) {
                }
                $before = date( 'YmdHis', time() );
                $this->store->deleteObjectsExpiringBefore( $before );
 -              return self::returnSuccess();
 +              return true;
        }
  }