Make WordPress Core

Changeset 53937


Ignore:
Timestamp:
08/24/2022 01:49:36 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Remove dynamic properties in WP_UnitTestCase_Base.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

In this particular case, the scandir() method sets a dynamic $matched_dirs property, which is subsequently used in the delete_folders() method.

This commit removes the need for the property. Effectively, this changes the scandir() method to return an array of the matched directories instead of setting the property by using recursion in the method itself in an optimized manner.

Note the array_merge() not being in the loop itself, but at the very end of the function. This is for performance reasons, see clearPHP: No array_merge() In Loops for a more detailed explanation of this.

It has been verified in detail that the actual results of the previous version of the method and this version match, even when the paths passed are more complex and have deeper nested subdirectories.

Follow-up to [30658], [53557], [53558], [53850], [53851], [53852], [53853], [53854], [53856], [53916], [53935], [53936].

Props jrf, johnbillion, markjaquith, SergeyBiryukov.
See #56033.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r53832 r53937  
    14221422         */
    14231423        public function delete_folders( $path ) {
    1424                 $this->matched_dirs = array();
    14251424                if ( ! is_dir( $path ) ) {
    14261425                        return;
    14271426                }
    14281427
    1429                 $this->scandir( $path );
    1430                 foreach ( array_reverse( $this->matched_dirs ) as $dir ) {
     1428                $matched_dirs = $this->scandir( $path );
     1429
     1430                foreach ( array_reverse( $matched_dirs ) as $dir ) {
    14311431                        rmdir( $dir );
    14321432                }
     1433
    14331434                rmdir( $path );
    14341435        }
    14351436
    14361437        /**
    1437          * Retrieves all directories contained inside a directory and stores them in the `$matched_dirs` property.
     1438         * Retrieves all directories contained inside a directory.
    14381439         * Hidden directories are ignored.
    14391440         *
     
    14411442         *
    14421443         * @since 4.1.0
     1444         * @since 6.1.0 No longer sets a (dynamic) property to keep track of the directories,
     1445         *              but returns an array of the directories instead.
    14431446         *
    14441447         * @param string $dir Path to the directory to scan.
     1448         * @return string[] List of directories.
    14451449         */
    14461450        public function scandir( $dir ) {
     1451                $matched_dirs = array();
     1452
    14471453                foreach ( scandir( $dir ) as $path ) {
    14481454                        if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) {
    1449                                 $this->matched_dirs[] = $dir . '/' . $path;
    1450                                 $this->scandir( $dir . '/' . $path );
    1451                         }
    1452                 }
     1455                                $matched_dirs[] = array( $dir . '/' . $path );
     1456                                $matched_dirs[] = $this->scandir( $dir . '/' . $path );
     1457                        }
     1458                }
     1459
     1460                /*
     1461                 * Compatibility check for PHP < 7.4, where array_merge() expects at least one array.
     1462                 * See: https://3v4l.org/BIQMA
     1463                 */
     1464                if ( array() === $matched_dirs ) {
     1465                        return array();
     1466                }
     1467
     1468                return array_merge( ...$matched_dirs );
    14531469        }
    14541470
Note: See TracChangeset for help on using the changeset viewer.