search-replace-command icon indicating copy to clipboard operation
search-replace-command copied to clipboard

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

Open cjhaas opened this issue 3 years ago • 0 comments

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

  • With deprecation warnings enabled, running an valid search-replace command that omits skip-columns, skip-tables or include-columns should show this warning

Describe what you would expect as the correct outcome

  • No deprecation notice

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

cjhaas avatar Feb 17 '22 18:02 cjhaas