Streamline code involving .= string concatenations
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Thu, 7 Mar 2019 09:55:31 +0000 (10:55 +0100)
committerThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Mon, 11 Mar 2019 11:43:45 +0000 (12:43 +0100)
This was inspired by Idbbdb31. Originally, I did a regex search for
code that did string concatenations like `$str = $str . …` and replaced
them all with the .= operator. A duplicate patch was uploaded by another
author. I rebeased this patch on top of the other, which leaves all
the manual optimizations I did.

Change-Id: Iaeb73d9c63302c9409bd1051b91e0d2bd77788a7

includes/EditPage.php
includes/db/DatabaseOracle.php
includes/filerepo/file/ForeignAPIFile.php
includes/libs/objectcache/BagOStuff.php
includes/media/DjVuImage.php
languages/Language.php

index 2048b87..41238df 100644 (file)
@@ -1667,11 +1667,10 @@ class EditPage {
                        case self::AS_SUCCESS_NEW_ARTICLE:
                                $query = $resultDetails['redirect'] ? 'redirect=no' : '';
                                if ( $extraQueryRedirect ) {
-                                       if ( $query === '' ) {
-                                               $query = $extraQueryRedirect;
-                                       } else {
-                                               $query .= '&' . $extraQueryRedirect;
+                                       if ( $query !== '' ) {
+                                               $query .= '&';
                                        }
+                                       $query .= $extraQueryRedirect;
                                }
                                $anchor = $resultDetails['sectionanchor'] ?? '';
                                $out->redirect( $this->mTitle->getFullURL( $query ) . $anchor );
@@ -1688,18 +1687,16 @@ class EditPage {
                                );
 
                                if ( $resultDetails['redirect'] ) {
-                                       if ( $extraQuery == '' ) {
-                                               $extraQuery = 'redirect=no';
-                                       } else {
-                                               $extraQuery = 'redirect=no&' . $extraQuery;
+                                       if ( $extraQuery !== '' ) {
+                                               $extraQuery = '&' . $extraQuery;
                                        }
+                                       $extraQuery = 'redirect=no' . $extraQuery;
                                }
                                if ( $extraQueryRedirect ) {
-                                       if ( $extraQuery === '' ) {
-                                               $extraQuery = $extraQueryRedirect;
-                                       } else {
-                                               $extraQuery .= '&' . $extraQueryRedirect;
+                                       if ( $extraQuery !== '' ) {
+                                               $extraQuery .= '&';
                                        }
+                                       $extraQuery .= $extraQueryRedirect;
                                }
 
                                $out->redirect( $this->mTitle->getFullURL( $extraQuery ) . $sectionanchor );
index 9cf924f..bb2d3f7 100644 (file)
@@ -565,7 +565,8 @@ class DatabaseOracle extends Database {
                // count-alias subselect fields to avoid abigious definition errors
                $i = 0;
                foreach ( $varMap as &$val ) {
-                       $val .= ' field' . ( $i++ );
+                       $val .= ' field' . $i;
+                       $i++;
                }
 
                $selectSql = $this->selectSQLText(
index 8e0242d..c49810c 100644 (file)
@@ -318,16 +318,15 @@ class ForeignAPIFile extends File {
         * @return null|string
         */
        function getThumbPath( $suffix = '' ) {
-               if ( $this->repo->canCacheThumbs() ) {
-                       $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
-                       if ( $suffix ) {
-                               $path .= $suffix . '/';
-                       }
-
-                       return $path;
-               } else {
+               if ( !$this->repo->canCacheThumbs() ) {
                        return null;
                }
+
+               $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
+               if ( $suffix ) {
+                       $path .= $suffix . '/';
+               }
+               return $path;
        }
 
        /**
index 44c79f3..6cad5df 100644 (file)
@@ -810,8 +810,7 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
        public function makeKeyInternal( $keyspace, $args ) {
                $key = $keyspace;
                foreach ( $args as $arg ) {
-                       $arg = str_replace( ':', '%3A', $arg );
-                       $key .= ':' . $arg;
+                       $key .= ':' . str_replace( ':', '%3A', $arg );
                }
                return strtr( $key, ' ', '_' );
        }
index 04ddda2..d059cd8 100644 (file)
@@ -286,7 +286,8 @@ EOR;
                                $txt = preg_replace_callback( $reg, [ $this, 'pageTextCallback' ], $txt );
                                $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
                                $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml, 1 ) .
-                                       $txt . '</mw-djvu>';
+                                       $txt .
+                                       '</mw-djvu>';
                        }
                }
 
index 3dbde01..bc69dad 100644 (file)
@@ -3593,14 +3593,12 @@ class Language {
                                $length -= $ellipsisLength;
                                $string = $getSubstring( $string, 0, $length ); // xyz...
                                $string = $this->removeBadCharLast( $string );
-                               $string = rtrim( $string );
-                               $string = $string . $ellipsis;
+                               $string = rtrim( $string ) . $ellipsis;
                        } else {
                                $length += $ellipsisLength;
                                $string = $getSubstring( $string, $length ); // ...xyz
                                $string = $this->removeBadCharFirst( $string );
-                               $string = ltrim( $string );
-                               $string = $ellipsis . $string;
+                               $string = $ellipsis . ltrim( $string );
                        }
                }