Deprecate wfArrayFilter() and wfArrayFilterByKey()
authorMax Semenik <maxsem.wiki@gmail.com>
Wed, 18 Jul 2018 08:42:52 +0000 (01:42 -0700)
committerJames D. Forrester <jforrester@wikimedia.org>
Thu, 19 Jul 2018 06:40:46 +0000 (08:40 +0200)
Now that all our supported PHP versions have array_filter()
with a third parameter, these functions aren't needed anymore.

Depends-On: I3b097a1a048baabcaca15dc214a3a1bb06e746cc
Depends-On: I0187e27ac47cbab099249572201d1a649226a734
Change-Id: I7cabd0252691a083cb749cf9d3a7a23f1d076c39

RELEASE-NOTES-1.32
includes/GlobalFunctions.php
includes/actions/HistoryAction.php
includes/changes/EnhancedChangesList.php
includes/logging/LogEventsList.php
includes/shell/Command.php
includes/specials/SpecialNewpages.php
includes/specials/pagers/ContribsPager.php
includes/specials/pagers/DeletedContribsPager.php
tests/phpunit/includes/GlobalFunctions/wfArrayFilterTest.php

index 79f5bf9..5435213 100644 (file)
@@ -277,6 +277,8 @@ because of Phabricator reports.
   to running searchText/searchTitle.
 * (T199657) Messages for $wgFilterLogTypes labels should be no longer be in the
   'log-show-hide-[type]' format. Instead use 'logeventslist-[type]-log'.
+* Global functions  wfArrayFilter() and wfArrayFilterByKey() are deprecated.
+  use array_filter() directly.
 
 === Other changes in 1.32 ===
 * (T198811) The following tables have had their UNIQUE indexes turned into
index 3ea020f..afad5c4 100644 (file)
@@ -141,7 +141,7 @@ function wfArrayDiff2_cmp( $a, $b ) {
 }
 
 /**
- * Like array_filter with ARRAY_FILTER_USE_BOTH, but works pre-5.6.
+ * @deprecated since 1.32, use array_filter() with ARRAY_FILTER_USE_BOTH directly
  *
  * @param array $arr
  * @param callable $callback Will be called with the array value and key (in that order) and
@@ -149,17 +149,11 @@ function wfArrayDiff2_cmp( $a, $b ) {
  * @return array
  */
 function wfArrayFilter( array $arr, callable $callback ) {
-       if ( defined( 'ARRAY_FILTER_USE_BOTH' ) ) {
-               return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH );
-       }
-       $filteredKeys = array_filter( array_keys( $arr ), function ( $key ) use ( $arr, $callback ) {
-               return call_user_func( $callback, $arr[$key], $key );
-       } );
-       return array_intersect_key( $arr, array_fill_keys( $filteredKeys, true ) );
+       return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH );
 }
 
 /**
- * Like array_filter with ARRAY_FILTER_USE_KEY, but works pre-5.6.
+ * @deprecated since 1.32, use array_filter() with ARRAY_FILTER_USE_KEY directly
  *
  * @param array $arr
  * @param callable $callback Will be called with the array key and should return a bool which
@@ -167,9 +161,7 @@ function wfArrayFilter( array $arr, callable $callback ) {
  * @return array
  */
 function wfArrayFilterByKey( array $arr, callable $callback ) {
-       return wfArrayFilter( $arr, function ( $val, $key ) use ( $callback ) {
-               return call_user_func( $callback, $key );
-       } );
+       return array_filter( $arr, $callback, ARRAY_FILTER_USE_KEY );
 }
 
 /**
index bd64a41..20637fc 100644 (file)
@@ -788,7 +788,10 @@ class HistoryPager extends ReverseChronologicalPager {
                $attribs = [ 'data-mw-revid' => $rev->getId() ];
 
                Hooks::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes, &$attribs ] );
-               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
+               $attribs = array_filter( $attribs,
+                       [ Sanitizer::class, 'isReservedDataAttribute' ],
+                       ARRAY_FILTER_USE_KEY
+               );
 
                if ( $classes ) {
                        $attribs['class'] = implode( ' ', $classes );
index cdfbf56..28b30d8 100644 (file)
@@ -472,7 +472,10 @@ class EnhancedChangesList extends ChangesList {
                        // skip entry if hook aborted it
                        return [];
                }
-               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
+               $attribs = array_filter( $attribs,
+                       [ Sanitizer::class, 'isReservedDataAttribute' ],
+                       ARRAY_FILTER_USE_KEY
+               );
 
                $lineParams['recentChangesFlagsRaw'] = [];
                if ( isset( $data['recentChangesFlags'] ) ) {
@@ -704,9 +707,9 @@ class EnhancedChangesList extends ChangesList {
                }
                $attribs = $data['attribs'];
                unset( $data['attribs'] );
-               $attribs = wfArrayFilterByKey( $attribs, function ( $key ) {
+               $attribs = array_filter( $attribs, function ( $key ) {
                        return $key === 'class' || Sanitizer::isReservedDataAttribute( $key );
-               } );
+               }, ARRAY_FILTER_USE_KEY );
 
                $prefix = '';
                if ( is_callable( $this->changeLinePrefixer ) ) {
index 4b9c91c..73aaa4f 100644 (file)
@@ -423,7 +423,10 @@ class LogEventsList extends ContextSource {
 
                // Let extensions add data
                Hooks::run( 'LogEventsListLineEnding', [ $this, &$ret, $entry, &$classes, &$attribs ] );
-               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
+               $attribs = array_filter( $attribs,
+                       [ Sanitizer::class, 'isReservedDataAttribute' ],
+                       ARRAY_FILTER_USE_KEY
+               );
                $attribs['class'] = implode( ' ', $classes );
 
                return Html::rawElement( 'li', $attribs, $ret ) . "\n";
index 8ae517e..1154e05 100644 (file)
@@ -437,12 +437,12 @@ class Command {
                        @trigger_error( '' );
                        restore_error_handler();
 
-                       $readPipes = wfArrayFilterByKey( $pipes, function ( $fd ) use ( $desc ) {
+                       $readPipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
                                return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'r';
-                       } );
-                       $writePipes = wfArrayFilterByKey( $pipes, function ( $fd ) use ( $desc ) {
+                       }, ARRAY_FILTER_USE_KEY );
+                       $writePipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
                                return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'w';
-                       } );
+                       }, ARRAY_FILTER_USE_KEY );
                        // stream_select parameter names are from the POV of us being able to do the operation;
                        // proc_open desriptor types are from the POV of the process doing it.
                        // So $writePipes is passed as the $read parameter and $readPipes as $write.
index a93b522..da2b688 100644 (file)
@@ -398,7 +398,10 @@ class SpecialNewpages extends IncludableSpecialPage {
 
                // Let extensions add data
                Hooks::run( 'NewPagesLineEnding', [ $this, &$ret, $result, &$classes, &$attribs ] );
-               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
+               $attribs = array_filter( $attribs,
+                       [ Sanitizer::class, 'isReservedDataAttribute' ],
+                       ARRAY_FILTER_USE_KEY
+               );
 
                if ( count( $classes ) ) {
                        $attribs['class'] = implode( ' ', $classes );
index 205364f..59fa948 100644 (file)
@@ -593,7 +593,10 @@ class ContribsPager extends RangeChronologicalPager {
 
                // Let extensions add data
                Hooks::run( 'ContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
-               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
+               $attribs = array_filter( $attribs,
+                       [ Sanitizer::class, 'isReservedDataAttribute' ],
+                       ARRAY_FILTER_USE_KEY
+               );
 
                // TODO: Handle exceptions in the catch block above.  Do any extensions rely on
                // receiving empty rows?
index f261b72..ee7eb3e 100644 (file)
@@ -221,7 +221,10 @@ class DeletedContribsPager extends IndexPager {
 
                // Let extensions add data
                Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
-               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
+               $attribs = array_filter( $attribs,
+                       [ Sanitizer::class, 'isReservedDataAttribute' ],
+                       ARRAY_FILTER_USE_KEY
+               );
 
                if ( $classes === [] && $attribs === [] && $ret === '' ) {
                        wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
index 1011a37..bc930be 100644 (file)
@@ -5,8 +5,9 @@
  * @covers ::wfArrayFilter
  * @covers ::wfArrayFilterByKey
  */
-class WfArrayFilterTest extends \PHPUnit\Framework\TestCase {
+class WfArrayFilterTest extends MediaWikiTestCase {
        public function testWfArrayFilter() {
+               $this->hideDeprecated( 'wfArrayFilter' );
                $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
                $filtered = wfArrayFilter( $arr, function ( $val, $key ) {
                        return $key !== 'b';
@@ -27,6 +28,7 @@ class WfArrayFilterTest extends \PHPUnit\Framework\TestCase {
        }
 
        public function testWfArrayFilterByKey() {
+               $this->hideDeprecated( 'wfArrayFilterByKey' );
                $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
                $filtered = wfArrayFilterByKey( $arr, function ( $key ) {
                        return $key !== 'b';