Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion php/WP_CLI/Dispatcher/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use WP_CLI\Utils;

/**
* Creates CompositeCommand or Subcommand instances.
Expand All @@ -29,7 +30,7 @@ public static function create( $name, $callable, $parent ) {
|| ( is_string( $callable ) && function_exists( $callable ) ) ) {
$reflection = new ReflectionFunction( $callable );
$command = self::create_subcommand( $parent, $name, $callable, $reflection );
} elseif ( is_array( $callable ) && is_callable( $callable ) ) {
} elseif ( is_array( $callable ) && ( is_callable( $callable ) || Utils\is_valid_class_and_method_pair( $callable ) ) ) {
$reflection = new ReflectionClass( $callable[0] );
$command = self::create_subcommand(
$parent,
Expand Down
2 changes: 2 additions & 0 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ public static function add_command( $name, $callable, $args = array() ) {
$valid = true;
} elseif ( is_object( $callable ) ) {
$valid = true;
} elseif ( Utils\is_valid_class_and_method_pair( $callable ) ) {
$valid = true;
}
if ( ! $valid ) {
if ( is_array( $callable ) ) {
Expand Down
29 changes: 29 additions & 0 deletions php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,35 @@ function describe_callable( $callable ) {
}
}

/**
* Checks if the given class and method pair is a valid callable.
*
* This accommodates changes to `is_callable()` in PHP 8 that mean an array of a
* classname and instance method is no longer callable.
*
* @param array $pair The class and method pair to check.
* @return bool
*/
function is_valid_class_and_method_pair( $pair ) {
if ( ! is_array( $pair ) || 2 !== count( $pair ) ) {
return false;
}

if ( ! is_string( $pair[0] ) || ! is_string( $pair[1] ) ) {
return false;
}

if ( ! class_exists( $pair[0] ) ) {
return false;
}

if ( ! method_exists( $pair[0], $pair[1] ) ) {
return false;
}

return true;
}

/**
* Pluralizes a noun in a grammatically correct way.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/test-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,22 @@ public function testReplacePathConstsAddSlashes() {
$actual = Utils\replace_path_consts( $source, "C:\Users\\test's\site" );
$this->assertSame( $expected, $actual );
}

/** @dataProvider dataValidClassAndMethodPair */
public function testValidClassAndMethodPair( $pair, $is_valid ) {
$this->assertEquals( $is_valid, Utils\is_valid_class_and_method_pair( $pair ) );
}

public function dataValidClassAndMethodPair() {
return [
[ 'string', false ],
[ [], false ],
[ [ 'WP_CLI' ], false ],
[ [ true, false ], false ],
[ [ 'WP_CLI', 'invalid_method' ], false ],
[ [ 'Invalid_Class', 'invalid_method' ], false ],
[ [ 'WP_CLI', 'add_command' ], true ],
[ [ 'Exception', 'getMessage' ], true ],
];
}
}