Merge "Enforce array type hinting in OutputPage.php"
[lhc/web/wiklou.git] / includes / utils / StringUtils.php
index c1545e6..86f4512 100644 (file)
@@ -24,7 +24,6 @@
  * A collection of static methods to play with strings.
  */
 class StringUtils {
-
        /**
         * Test whether a string is valid UTF-8.
         *
@@ -43,11 +42,11 @@ class StringUtils {
         * Beware of this when backporting code to that version of MediaWiki.
         *
         * @param string $value String to check
-        * @param boolean $disableMbstring Whether to use the pure PHP
+        * @param bool $disableMbstring Whether to use the pure PHP
         * implementation instead of trying mb_check_encoding. Intended for unit
         * testing. Default: false
         *
-        * @return boolean Whether the given $value is a valid UTF-8 encoded string
+        * @return bool Whether the given $value is a valid UTF-8 encoded string
         */
        static function isUtf8( $value, $disableMbstring = false ) {
                $value = (string)$value;
@@ -107,6 +106,7 @@ class StringUtils {
                                return false;
                        }
                }
+
                return true;
        }
 
@@ -121,10 +121,10 @@ class StringUtils {
         * hungry and inflexible. The memory requirements are such that I don't
         * recommend using it on anything but guaranteed small chunks of text.
         *
-        * @param $startDelim
-        * @param $endDelim
-        * @param $replace
-        * @param $subject
+        * @param string $startDelim
+        * @param string $endDelim
+        * @param string $replace
+        * @param string $subject
         *
         * @return string
         */
@@ -139,6 +139,7 @@ class StringUtils {
                                $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
                        }
                }
+
                return $output;
        }
 
@@ -156,15 +157,17 @@ class StringUtils {
         * start, so e.g. /*\/ is not considered to be both the start and end of a
         * comment. /*\/xy/*\/ is considered to be a single comment with contents /xy/.
         *
-        * @param string $startDelim start delimiter
-        * @param string $endDelim end delimiter
-        * @param $callback Callback: function to call on each match
-        * @param $subject String
-        * @param string $flags regular expression flags
+        * @param string $startDelim Start delimiter
+        * @param string $endDelim End delimiter
+        * @param callable $callback Function to call on each match
+        * @param string $subject
+        * @param string $flags Regular expression flags
         * @throws MWException
         * @return string
         */
-       static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
+       static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
+               $subject, $flags = ''
+       ) {
                $inputPos = 0;
                $outputPos = 0;
                $output = '';
@@ -176,13 +179,13 @@ class StringUtils {
                $m = array();
 
                while ( $inputPos < strlen( $subject ) &&
-                       preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos ) )
-               {
+                       preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
+               {
                        $tokenOffset = $m[0][1];
                        if ( $m[1][0] != '' ) {
                                if ( $foundStart &&
-                                       $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
-                               {
+                                       $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
+                               {
                                        # An end match is present at the same location
                                        $tokenType = 'end';
                                        $tokenLength = $endLength;
@@ -219,7 +222,7 @@ class StringUtils {
                                        $output .= call_user_func( $callback, array(
                                                substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
                                                substr( $subject, $contentPos, $tokenOffset - $contentPos )
-                                       ));
+                                       ) );
                                        $foundStart = false;
                                } else {
                                        # Non-matching end, write it out
@@ -233,6 +236,7 @@ class StringUtils {
                if ( $outputPos < strlen( $subject ) ) {
                        $output .= substr( $subject, $outputPos );
                }
+
                return $output;
        }
 
@@ -241,16 +245,17 @@ class StringUtils {
         *
         *   preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
         *
-        * @param string $startDelim start delimiter regular expression
-        * @param string $endDelim end delimiter regular expression
-        * @param string $replace replacement string. May contain $1, which will be
+        * @param string $startDelim Start delimiter regular expression
+        * @param string $endDelim End delimiter regular expression
+        * @param string $replace Replacement string. May contain $1, which will be
         *                 replaced by the text between the delimiters
-        * @param string $subject to search
-        * @param string $flags regular expression flags
-        * @return String: The string with the matches replaced
+        * @param string $subject String to search
+        * @param string $flags Regular expression flags
+        * @return string The string with the matches replaced
         */
        static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
                $replacer = new RegexlikeReplacer( $replace );
+
                return self::delimiterReplaceCallback( $startDelim, $endDelim,
                        $replacer->cb(), $subject, $flags );
        }
@@ -291,6 +296,7 @@ class StringUtils {
        static function escapeRegexReplacement( $string ) {
                $string = str_replace( '\\', '\\\\', $string );
                $string = str_replace( '$', '\\$', $string );
+
                return $string;
        }
 
@@ -315,7 +321,6 @@ class StringUtils {
  * StringUtils::delimiterReplaceCallback()
  */
 class Replacer {
-
        /**
         * @return array
         */
@@ -328,7 +333,7 @@ class Replacer {
  * Class to replace regex matches with a string similar to that used in preg_replace()
  */
 class RegexlikeReplacer extends Replacer {
-       var $r;
+       private $r;
 
        /**
         * @param string $r
@@ -346,19 +351,18 @@ class RegexlikeReplacer extends Replacer {
                foreach ( $matches as $i => $match ) {
                        $pairs["\$$i"] = $match;
                }
+
                return strtr( $this->r, $pairs );
        }
-
 }
 
 /**
  * Class to perform secondary replacement within each replacement string
  */
 class DoubleReplacer extends Replacer {
-
        /**
-        * @param $from
-        * @param $to
+        * @param mixed $from
+        * @param mixed $to
         * @param int $index
         */
        function __construct( $from, $to, $index = 0 ) {
@@ -380,10 +384,10 @@ class DoubleReplacer extends Replacer {
  * Class to perform replacement based on a simple hashtable lookup
  */
 class HashtableReplacer extends Replacer {
-       var $table, $index;
+       private $table, $index;
 
        /**
-        * @param $table
+        * @param array $table
         * @param int $index
         */
        function __construct( $table, $index = 0 ) {
@@ -405,8 +409,8 @@ class HashtableReplacer extends Replacer {
  * Supports lazy initialisation of FSS resource
  */
 class ReplacementArray {
-       /*mostly private*/ var $data = false;
-       /*mostly private*/ var $fss = false;
+       private $data = false;
+       private $fss = false;
 
        /**
         * Create an object with the specified replacement array
@@ -505,6 +509,7 @@ class ReplacementArray {
                        $result = strtr( $subject, $this->data );
                        wfProfileOut( __METHOD__ . '-strtr' );
                }
+
                return $result;
        }
 }
@@ -520,19 +525,19 @@ class ReplacementArray {
  */
 class ExplodeIterator implements Iterator {
        // The subject string
-       var $subject, $subjectLength;
+       private $subject, $subjectLength;
 
        // The delimiter
-       var $delim, $delimLength;
+       private $delim, $delimLength;
 
        // The position of the start of the line
-       var $curPos;
+       private $curPos;
 
        // The position after the end of the next delimiter
-       var $endPos;
+       private $endPos;
 
        // The current token
-       var $current;
+       private $current;
 
        /**
         * Construct a DelimIterator
@@ -594,6 +599,7 @@ class ExplodeIterator implements Iterator {
                        }
                }
                $this->refreshCurrent();
+
                return $this->current;
        }