Skip to content

WP_CLI::has_config() vs. WP_CLI::get_config() #5353

@dougaxe1

Description

@dougaxe1

Bug Report

Describe the current, buggy behavior

WP_CLI::has_config() uses array_key_exists() to determine if a config exists:

return array_key_exists( $key, self::get_runner()->config );

WP_CLI::get_config() uses isset() to determine if a config exists:

if ( ! isset( self::get_runner()->config[ $key ] ) ) {

null values, such as url (if not specified via --url=) will satisfy the criteria for has_config() but throw a warning when calling get_config().

Describe how other contributors can replicate this bug

$ wp eval 'var_dump(WP_CLI::has_config("url"), WP_CLI::get_config("url"));'
Warning: Unknown config option 'url'.
bool(true)
NULL
$ wp eval 'if (WP_CLI::has_config("url")) echo WP_CLI::get_config("url");'
Warning: Unknown config option 'url'.
$ wp eval 'if (WP_CLI::has_config("url")) echo WP_CLI::get_config("url");' --url=

$ wp eval 'if (WP_CLI::has_config("url")) echo WP_CLI::get_config("url");' --url="www.wordpress.org"
www.wordpress.org

Describe what you expect as the correct outcome

The two functions should use the same evaluation criteria for whether the config exists. I recommend array_key_exists as it reflects what I think of as "has_config".

There is perhaps an underlying bug in that config['url'] is set on all requests regardless of the --url flag existing, but I'd settle for consistent handling even if it means doing a strlen() check on the result of get_config().

Let us know what environment you are running this on

$ wp cli info
OS:     Windows NT 10.0 build 18363 (Windows 10) AMD64
Shell:  C:\WINDOWS\system32\cmd.exe
PHP binary:     c:\wamp\bin\php\php7.3.1\php.exe
PHP version:    7.3.1
php.ini used:   C:\wamp\bin\php\php7.3.1\php.ini
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\Douglas
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.4.0

Provide a possible solution

Use WP_CLI::has_config() in WP_CLI::get_config() to ensure consistent handling.

Workaround: testing the variable the same way get_config does before using it to avoid a warning: isset( WP_CLI::get_runner()->config['url'] )

Provide additional context/screenshots

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions