From: Mark A. Hershberger Date: Fri, 13 Jul 2018 18:19:51 +0000 (-0400) Subject: Language::listToText: Avoid PHP warnings from E_STRICT X-Git-Tag: 1.34.0-rc.0~4698^2 X-Git-Url: http://git.cyclocoop.org///%22%40url%40//%22?a=commitdiff_plain;h=2fb1ec2a758580227e688042f4d0852e435c0f69;p=lhc%2Fweb%2Fwiklou.git Language::listToText: Avoid PHP warnings from E_STRICT Language::listToText() uses for(;;) which assumes the index in a list starts at 0 and following keys are consecutive integers. This assumption fails (and results in warnings with E_STRICT) when SpecialListGroupRights::formatPermissions() uses the key-preserving array_intersect() to create a subset of $allGroups. Bug: T199559 Change-Id: I4eb03a95509d69653156a2764d58c0c5d0d1dfbc --- diff --git a/languages/Language.php b/languages/Language.php index 97920950e0..7f04a6874e 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3413,32 +3413,26 @@ class Language { * Take a list of strings and build a locale-friendly comma-separated * list, using the local comma-separator message. * The last two strings are chained with an "and". - * NOTE: This function will only work with standard numeric array keys (0, 1, 2…) * - * @param string[] $l + * @param string[] $list * @return string */ - function listToText( array $l ) { - $m = count( $l ) - 1; - if ( $m < 0 ) { + public function listToText( array $list ) { + $itemCount = count( $list ); + if ( $itemCount < 1 ) { return ''; } - if ( $m > 0 ) { + $text = array_pop( $list ); + if ( $itemCount > 1 ) { $and = $this->msg( 'and' )->escaped(); $space = $this->msg( 'word-separator' )->escaped(); - if ( $m > 1 ) { + $comma = ''; + if ( $itemCount > 2 ) { $comma = $this->msg( 'comma-separator' )->escaped(); } + $text = implode( $comma, $list ) . $and . $space . $text; } - $s = $l[$m]; - for ( $i = $m - 1; $i >= 0; $i-- ) { - if ( $i == $m - 1 ) { - $s = $l[$i] . $and . $space . $s; - } else { - $s = $l[$i] . $comma . $s; - } - } - return $s; + return $text; } /**