WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/15/2019 11:03:45 AM (2 months ago)
Author:
SergeyBiryukov
Message:

Code Modernisation: Introduce the spread operator in tests/phpunit/*.

Rather than relying func_get_args() to retrieve arbitrary function arguments, we can now use the spread operator to assign them directly to a variable.

Props jrf.
See #47678.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/utils.php

    r45607 r46127  
    129129    }
    130130
    131     function filterall( $tag, $arg = null ) {
     131    function filterall( $tag, ...$args ) {
    132132        // this one doesn't return the result, so it's safe to use with the new 'all' filter
    133133        if ( $this->debug ) {
     
    135135        }
    136136
    137         $args           = func_get_args();
    138137        $this->events[] = array(
    139138            'filter' => __FUNCTION__,
    140139            'tag'    => $tag,
    141             'args'   => array_slice( $args, 1 ),
     140            'args'   => $args,
    142141        );
    143142    }
     
    245244}
    246245
    247 function xml_find( $tree /*, $el1, $el2, $el3, .. */ ) {
    248     $a   = func_get_args();
    249     $a   = array_slice( $a, 1 );
    250     $n   = count( $a );
     246function xml_find( $tree, ...$elements ) {
     247    $n   = count( $elements );
    251248    $out = array();
    252249
     
    256253
    257254    for ( $i = 0; $i < count( $tree ); $i++ ) {
    258         #       echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n";
    259         #       var_dump($tree[$i]['name'], $a[0]);
    260         if ( $tree[ $i ]['name'] === $a[0] ) {
     255        #       echo "checking '{$tree[$i][name]}' == '{$elements[0]}'\n";
     256        #       var_dump( $tree[$i]['name'], $elements[0] );
     257        if ( $tree[ $i ]['name'] === $elements[0] ) {
    261258            #           echo "n == {$n}\n";
    262259            if ( 1 === $n ) {
    263260                $out[] = $tree[ $i ];
    264261            } else {
    265                 $subtree   =& $tree[ $i ]['child'];
    266                 $call_args = array( $subtree );
    267                 $call_args = array_merge( $call_args, array_slice( $a, 1 ) );
    268                 $out       = array_merge( $out, call_user_func_array( 'xml_find', $call_args ) );
     262                $subtree =& $tree[ $i ]['child'];
     263                $out     = array_merge( $out, xml_find( $subtree, ...array_slice( $elements, 1 ) ) );
    269264            }
    270265        }
     
    301296}
    302297
    303 function dmp() {
    304     $args = func_get_args();
    305 
     298function dmp( ...$args ) {
    306299    foreach ( $args as $thing ) {
    307300        echo ( is_scalar( $thing ) ? strval( $thing ) : var_export( $thing, true ) ), "\n";
Note: See TracChangeset for help on using the changeset viewer.