Attempt to fix problems noted in phpcs.
[lhc/web/wiklou.git] / languages / Language.php
index 6bbd8bf..0026fdf 100644 (file)
@@ -213,8 +213,8 @@ class Language {
        protected static function newFromCode( $code ) {
                // Protect against path traversal below
                if ( !Language::isValidCode( $code )
-                       || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
-               {
+                       || strcspn( $code, ":/\\\000" ) !== strlen( $code )
+               {
                        throw new MWException( "Invalid language code \"$code\"" );
                }
 
@@ -1502,8 +1502,7 @@ class Language {
                if (
                        ( $zy > 1582 ) || ( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
                        ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
-               )
-               {
+               ) {
                        $zjd = (int)( ( 1461 * ( $zy + 4800 + (int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
                                        (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
                                        (int)( ( 3 * (int)( ( ( $zy + 4900 + (int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
@@ -1753,8 +1752,7 @@ class Language {
                                ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
                                ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
                                ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
-                       )
-                       {
+                       ) {
                                # Taishō period
                                $gy_gannen = $gy - 1912 + 1;
                                $gy_offset = $gy_gannen;
@@ -1766,8 +1764,7 @@ class Language {
                                ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
                                ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
                                ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
-                       )
-                       {
+                       ) {
                                # Shōwa period
                                $gy_gannen = $gy - 1926 + 1;
                                $gy_offset = $gy_gannen;
@@ -3325,11 +3322,13 @@ class Language {
                                $length -= $eLength;
                                $string = substr( $string, 0, $length ); // xyz...
                                $string = $this->removeBadCharLast( $string );
+                               $string = rtrim( $string );
                                $string = $string . $ellipsis;
                        } else {
                                $length += $eLength;
                                $string = substr( $string, $length ); // ...xyz
                                $string = $this->removeBadCharFirst( $string );
+                               $string = ltrim( $string );
                                $string = $ellipsis . $string;
                        }
                }
@@ -3633,17 +3632,10 @@ class Language {
         */
        function convertPlural( $count, $forms ) {
                // Handle explicit n=pluralform cases
-               foreach ( $forms as $index => $form ) {
-                       if ( preg_match( '/^\d+=/i', $form ) ) {
-                               $pos = strpos( $form, '=' );
-                               if ( substr( $form, 0, $pos ) === (string)$count ) {
-                                       return substr( $form, $pos + 1 );
-                               }
-                               unset( $forms[$index] );
-                       }
+               $forms = $this->handleExplicitPluralForms( $count, $forms );
+               if ( is_string( $forms ) ) {
+                       return $forms;
                }
-
-               $forms = array_values( $forms );
                if ( !count( $forms ) ) {
                        return '';
                }
@@ -3653,6 +3645,34 @@ class Language {
                return $forms[$pluralForm];
        }
 
+       /**
+        * Handles explicit plural forms for Language::convertPlural()
+        *
+        * In {{PLURAL:$1|0=nothing|one|many}}, 0=nothing will be returned if $1 equals zero.
+        * If an explicitly defined plural form matches the $count, then
+        * string value returned, otherwise array returned for further consideration
+        * by CLDR rules or overridden convertPlural().
+        *
+        * @since 1.23
+        *
+        * @param int $count non-localized number
+        * @param array $forms different plural forms
+        *
+        * @return array|string
+        */
+       protected function handleExplicitPluralForms( $count, array $forms ) {
+               foreach ( $forms as $index => $form ) {
+                       if ( preg_match( '/\d+=/i', $form ) ) {
+                               $pos = strpos( $form, '=' );
+                               if ( substr( $form, 0, $pos ) === (string) $count ) {
+                                       return substr( $form, $pos + 1 );
+                               }
+                               unset( $forms[$index] );
+                       }
+               }
+               return array_values( $forms );
+       }
+
        /**
         * Checks that convertPlural was given an array and pads it to requested
         * amount of forms by copying the last one.
@@ -4046,8 +4066,8 @@ class Language {
        public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
                // Protect against path traversal
                if ( !Language::isValidCode( $code )
-                       || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
-               {
+                       || strcspn( $code, ":/\\\000" ) !== strlen( $code )
+               {
                        throw new MWException( "Invalid language code \"$code\"" );
                }