wp-cli / search-replace-command

Searches/replaces strings in the database.
MIT License
57 stars 45 forks source link

PHP 8.1 - PHP Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated #168

Closed cjhaas closed 1 year ago

cjhaas commented 2 years ago

Bug Report

Describe the current, buggy behavior

PHP 8.1 is deprecating passing null to internal functions that accept scalar values. Although there is discussion about trying to relax this, the consensus seems to be that developers and frameworks should solve this.

The WP CLI core Utils namespace has a helper function called get_flag_value that accepts a fallback parameter for missing flags that defaults to null:

function get_flag_value( $assoc_args, $flag, $default = null ) {
    return isset( $assoc_args[ $flag ] ) ? $assoc_args[ $flag ] : $default;
}

On lines 226 through 228 of the search-replace command, the return value of that function is passed directly to explode which will result in a deprecation warning if those flags aren't set

https://github.com/wp-cli/search-replace-command/blob/7ffc57340e66c2e44e005ee4321f74ed558b6f0f/src/Search_Replace_Command.php#L226-L228

This can be fixed with more LoC with null-checks, or by just passing the empty string as the third argument to that functions on those three lines.

$this->skip_columns    = explode( ',', Utils\get_flag_value( $assoc_args, 'skip-columns', '' ) );
$this->skip_tables     = explode( ',', Utils\get_flag_value( $assoc_args, 'skip-tables', '' ) );
$this->include_columns = array_filter( explode( ',', Utils\get_flag_value( $assoc_args, 'include-columns', '' ) ) );

Describe how other contributors can replicate this bug

Describe what you would expect as the correct outcome

Let us know what environment you are running this on

OS:     Windows NT 10.0 build 22000 (Windows 11) AMD64
Shell:  C:\Program Files\Git\usr\bin\bash.exe
PHP binary:     C:\Users\Chris Haas.VENDI\Tools\PHP\8.1.2\php.exe
PHP version:    8.1.2
php.ini used:   C:\Users\Chris Haas.VENDI\Tools\PHP\8.1.2\php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       C:\Users\Chris Haas.VENDI\Desktop\dev\bapi-us
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.7.0-alpha-be7a10b

Provide a possible solution

A PR is in progress

schlessera commented 1 year ago

This fix was pushed as a patch release with release v2.7.1.