From 2c34fd6e0ecf33a77c947eeba98154c02554ee3a Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 19 Sep 2017 15:57:18 -0400 Subject: [PATCH] Replace uses of each() It's deprecated in PHP 7.2, may as well replace it now. I note that, contrary to claims at https://wiki.php.net/rfc/deprecations_php_7_2#each, none of our uses were trivially replaceable with foreach. * wfArrayDiff2_cmp() is processing two arrays by value in parallel. * MagicWordArray::parseMatch() is doing something funky with the data structure returned by preg_match(). * HashRing was using it like "nextKey()", replaced with calls to key() and next(). * FormatMetadata and IndexPager were both using it as a shorter way to get both key() and current() for the first element in the array. I suppose a foreach(){ break; } would do the same, but that's confusing. Bug: T174354 Change-Id: I36169a04c764fdf1bfd6603395111c6fe0aae5eb --- includes/GlobalFunctions.php | 6 +++++- includes/MagicWordArray.php | 4 +++- includes/libs/HashRing.php | 3 ++- includes/media/FormatMetadata.php | 6 +++--- includes/pager/IndexPager.php | 4 ++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index e80ecf1c46..484dfe8d4d 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -195,11 +195,15 @@ function wfArrayDiff2_cmp( $a, $b ) { } else { reset( $a ); reset( $b ); - while ( ( list( , $valueA ) = each( $a ) ) && ( list( , $valueB ) = each( $b ) ) ) { + while ( key( $a ) !== null && key( $b ) !== null ) { + $valueA = current( $a ); + $valueB = current( $b ); $cmp = strcmp( $valueA, $valueB ); if ( $cmp !== 0 ) { return $cmp; } + next( $a ); + next( $b ); } return 0; } diff --git a/includes/MagicWordArray.php b/includes/MagicWordArray.php index 5856e21b71..4010ec7585 100644 --- a/includes/MagicWordArray.php +++ b/includes/MagicWordArray.php @@ -203,7 +203,9 @@ class MagicWordArray { */ public function parseMatch( $m ) { reset( $m ); - while ( list( $key, $value ) = each( $m ) ) { + while ( ( $key = key( $m ) ) !== null ) { + $value = current( $m ); + next( $m ); if ( $key === 0 || $value === '' ) { continue; } diff --git a/includes/libs/HashRing.php b/includes/libs/HashRing.php index be40965e84..f61c139a99 100644 --- a/includes/libs/HashRing.php +++ b/includes/libs/HashRing.php @@ -116,11 +116,12 @@ class HashRing { // If more locations are requested, wrap-around and keep adding them reset( $this->ring ); while ( count( $locations ) < $limit ) { - list( $location, ) = each( $this->ring ); + $location = key( $this->ring ); if ( $location === $primaryLocation ) { break; // don't go in circles } $locations[] = $location; + next( $this->ring ); } return $locations; diff --git a/includes/media/FormatMetadata.php b/includes/media/FormatMetadata.php index 6cac1261c7..666196585a 100644 --- a/includes/media/FormatMetadata.php +++ b/includes/media/FormatMetadata.php @@ -1761,9 +1761,9 @@ class FormatMetadata extends ContextSource { } return $newValue; } else { // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul' - list( $k, $v ) = each( $value ); - if ( $k === '_type' ) { - $v = current( $value ); + $v = reset( $value ); + if ( key( $value ) === '_type' ) { + $v = next( $value ); } return $v; } diff --git a/includes/pager/IndexPager.php b/includes/pager/IndexPager.php index 6620c4751b..d1c98f22fc 100644 --- a/includes/pager/IndexPager.php +++ b/includes/pager/IndexPager.php @@ -162,8 +162,8 @@ abstract class IndexPager extends ContextSource implements Pager { : []; } elseif ( is_array( $index ) ) { # First element is the default - reset( $index ); - list( $this->mOrderType, $this->mIndexField ) = each( $index ); + $this->mIndexField = reset( $index ); + $this->mOrderType = key( $index ); $this->mExtraSortFields = isset( $extraSort[$this->mOrderType] ) ? (array)$extraSort[$this->mOrderType] : []; -- 2.20.1