Use proper parameter type in Closure::bindTo() signature

This commit is contained in:
Nikita Popov 2020-09-21 14:45:31 +02:00
parent 4c821cf206
commit 8e0789a21c
4 changed files with 45 additions and 40 deletions

View file

@ -24,14 +24,16 @@ $a = new A(20);
$ca = $a->getIncrementor();
$cas = $a->getStaticIncrementor();
$ca->bindTo($a, array());
try {
$ca->bindTo($a, array());
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
$cas->bindTo($a, 'A');
?>
--EXPECTF--
Warning: Array to string conversion in %s on line %d
Warning: Class "Array" not found in %s on line %d
Closure::bindTo(): Argument #2 ($newScope) must be of type object|string|null, array given
Warning: Cannot bind an instance to a static closure in %s on line %d

View file

@ -192,30 +192,22 @@ ZEND_METHOD(Closure, call)
}
/* }}} */
static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zval *scope_arg)
static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zend_object *scope_obj, zend_string *scope_str)
{
zend_class_entry *ce, *called_scope;
zend_closure *closure = (zend_closure *) Z_OBJ_P(zclosure);
if (scope_arg != NULL) { /* scope argument was given */
if (Z_TYPE_P(scope_arg) == IS_OBJECT) {
ce = Z_OBJCE_P(scope_arg);
} else if (Z_TYPE_P(scope_arg) == IS_NULL) {
ce = NULL;
} else {
zend_string *tmp_class_name;
zend_string *class_name = zval_get_tmp_string(scope_arg, &tmp_class_name);
if (zend_string_equals_literal(class_name, "static")) {
if (scope_obj) {
ce = scope_obj->ce;
} else if (scope_str) {
if (zend_string_equals(scope_str, ZSTR_KNOWN(ZEND_STR_STATIC))) {
ce = closure->func.common.scope;
} else if ((ce = zend_lookup_class(class_name)) == NULL) {
zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(class_name));
zend_tmp_string_release(tmp_class_name);
} else if ((ce = zend_lookup_class(scope_str)) == NULL) {
zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(scope_str));
RETURN_NULL();
}
zend_tmp_string_release(tmp_class_name);
}
} else { /* scope argument not given; do not change the scope by default */
ce = closure->func.common.scope;
} else {
ce = NULL;
}
if (!zend_valid_closure_binding(closure, newthis, ce)) {
@ -234,25 +226,34 @@ static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, z
/* {{{ Create a closure from another one and bind to another object and scope */
ZEND_METHOD(Closure, bind)
{
zval *newthis, *zclosure, *scope_arg = NULL;
zval *zclosure, *newthis;
zend_object *scope_obj = NULL;
zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJECT_OF_CLASS(zclosure, zend_ce_closure)
Z_PARAM_OBJECT_OR_NULL(newthis)
Z_PARAM_OPTIONAL
Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str)
ZEND_PARSE_PARAMETERS_END();
do_closure_bind(return_value, zclosure, newthis, scope_arg);
do_closure_bind(return_value, zclosure, newthis, scope_obj, scope_str);
}
/* {{{ Create a closure from another one and bind to another object and scope */
ZEND_METHOD(Closure, bindTo)
{
zval *newthis, *scope_arg = NULL;
zval *newthis;
zend_object *scope_obj = NULL;
zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o!|z", &newthis, &scope_arg) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJECT_OR_NULL(newthis)
Z_PARAM_OPTIONAL
Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str)
ZEND_PARSE_PARAMETERS_END();
do_closure_bind(return_value, getThis(), newthis, scope_arg);
do_closure_bind(return_value, getThis(), newthis, scope_obj, scope_str);
}
static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ {

View file

@ -6,11 +6,13 @@ final class Closure
{
private function __construct() {}
/** @param object|string|null $newScope */
public static function bind(Closure $closure, ?object $newThis, $newScope = UNKNOWN): ?Closure {}
public static function bind(
Closure $closure,
?object $newThis,
object|string|null $newScope = "static"
): ?Closure {}
/** @param object|string|null $newScope */
public function bindTo(?object $newThis, $newScope = UNKNOWN): ?Closure {}
public function bindTo(?object $newThis, object|string|null $newScope = "static"): ?Closure {}
public function call(object $newThis, mixed ...$arguments): mixed {}

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: abbbe7b04323dc44b0675ad58700e996a6d7c43b */
* Stub hash: 6c9840dd5c2e4c597cd0133bf2d0b523c272d3fe */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Closure___construct, 0, 0, 0)
ZEND_END_ARG_INFO()
@ -7,12 +7,12 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_bind, 0, 2, Closure, 1)
ZEND_ARG_OBJ_INFO(0, closure, Closure, 0)
ZEND_ARG_TYPE_INFO(0, newThis, IS_OBJECT, 1)
ZEND_ARG_INFO(0, newScope)
ZEND_ARG_TYPE_MASK(0, newScope, MAY_BE_OBJECT|MAY_BE_STRING|MAY_BE_NULL, "\"static\"")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_bindTo, 0, 1, Closure, 1)
ZEND_ARG_TYPE_INFO(0, newThis, IS_OBJECT, 1)
ZEND_ARG_INFO(0, newScope)
ZEND_ARG_TYPE_MASK(0, newScope, MAY_BE_OBJECT|MAY_BE_STRING|MAY_BE_NULL, "\"static\"")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Closure_call, 0, 1, IS_MIXED, 0)