Deprecate ReflectionProperty::setValue() with an incorrect 1st arg type

This commit is contained in:
Máté Kocsis 2023-07-13 19:31:50 +02:00
parent f41220fe5d
commit d9a7f6741e
7 changed files with 49 additions and 13 deletions

View File

@ -25,7 +25,7 @@ spl_autoload_register('main_autoload');
$classA = new ReflectionClass("A");
$props = $classA->getProperties();
$props[0]->setValue(2); //causes constant resolving, which runs autoload, all with EG(fake_scope) == "A"
$props[0]->setValue(null, 2); //causes constant resolving, which runs autoload, all with EG(fake_scope) == "A"
echo "OK\n";
?>

View File

@ -5720,6 +5720,22 @@ ZEND_METHOD(ReflectionProperty, setValue)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
RETURN_THROWS();
}
if (Z_TYPE_P(tmp) != IS_NULL && Z_TYPE_P(tmp) != IS_OBJECT) {
zend_string *method_name = get_active_function_or_method_name();
zend_error(E_DEPRECATED, "Calling %s() with a 1st argument which is not null or an object is deprecated", ZSTR_VAL(method_name));
zend_string_release(method_name);
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
}
} else {
zend_string *method_name = get_active_function_or_method_name();
zend_error(E_DEPRECATED, "Calling %s() with a single argument is deprecated", ZSTR_VAL(method_name));
zend_string_release(method_name);
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
}
zend_update_static_property_ex(intern->ce, ref->unmangled_name, value);

View File

@ -23,9 +23,9 @@ var_dump($private->getValue($a));
var_dump($privateStatic->getValue());
$protected->setValue($a, 'e');
$protectedStatic->setValue('f');
$protectedStatic->setValue(null, 'f');
$private->setValue($a, 'g');
$privateStatic->setValue('h');
$privateStatic->setValue(null, 'h');
var_dump($protected->getValue($a));
var_dump($protectedStatic->getValue());
@ -45,7 +45,7 @@ var_dump($privateStatic->getValue());
$protected->setValue($a, 'i');
$protectedStatic->setValue('j');
$private->setValue($a, 'k');
$privateStatic->setValue('l');
$privateStatic->setValue(null, 'l');
var_dump($protected->getValue($a));
var_dump($protectedStatic->getValue());
@ -63,7 +63,7 @@ var_dump($protectedStatic->getValue());
var_dump($private->getValue($b));
$protected->setValue($b, 'e');
$protectedStatic->setValue('f');
$protectedStatic->setValue(null, 'f');
$private->setValue($b, 'g');
var_dump($protected->getValue($b));
@ -79,14 +79,14 @@ var_dump($protectedStatic->getValue());
var_dump($private->getValue($b));
$protected->setValue($b, 'h');
$protectedStatic->setValue('i');
$protectedStatic->setValue(null, 'i');
$private->setValue($b, 'j');
var_dump($protected->getValue($b));
var_dump($protectedStatic->getValue());
var_dump($private->getValue($b));
?>
--EXPECT--
--EXPECTF--
string(1) "a"
string(1) "b"
string(1) "c"
@ -99,6 +99,8 @@ string(1) "e"
string(1) "f"
string(1) "g"
string(1) "h"
Deprecated: Calling ReflectionProperty::setValue() with a single argument is deprecated in %s on line %d
string(1) "i"
string(1) "j"
string(1) "k"

View File

@ -19,11 +19,11 @@ try {
echo $e->getMessage(), "\n";
}
$rp->setValue("24");
$rp->setValue(null, "24");
var_dump($rp->getValue());
try {
$rp->setValue("foo");
$rp->setValue(null, "foo");
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
@ -33,7 +33,7 @@ Test::$z =& Test::$y;
$rp = new ReflectionProperty('Test', 'z');
try {
$rp->setValue("foo");
$rp->setValue(null, "foo");
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

View File

@ -15,7 +15,9 @@ var_dump($r->getValue());
$r->setValue(3);
var_dump($r->getValue());
?>
--EXPECT--
--EXPECTF--
int(1)
int(2)
Deprecated: Calling ReflectionProperty::setValue() with a single argument is deprecated in %s on line %d
int(3)

View File

@ -28,6 +28,17 @@ $Prop2->setValue(\T2::class, 'hello');
var_dump("T2::self = " . T2::getDataBySelf());
var_dump("T2::static = " . T2::getDataByStatic());
set_error_handler(function ($severity, $message, $file, $line) {
throw new Exception($message);
});
try {
$Prop2->setValue(\T2::class, 'hi');
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
var_dump("T2::self = " . T2::getDataByStatic());
// #2
// prints: hello, hello in both PHP5 and PHP7 - OK
T1::$data = "world";
@ -36,8 +47,13 @@ T2::$data = 'hello';
var_dump("T2::self = " . T2::getDataBySelf());
var_dump("T2::static = " . T2::getDataByStatic());
?>
--EXPECT--
--EXPECTF--
Deprecated: Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated in %s on line %d
Deprecated: Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated in %s on line %d
string(16) "T2::self = hello"
string(18) "T2::static = hello"
Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated
string(16) "T2::self = hello"
string(16) "T2::self = hello"
string(18) "T2::static = hello"

View File

@ -6,7 +6,7 @@ zend_test
<?php
$rp = new ReflectionProperty('_ZendTestClass', '_StaticProp');
$rp->setValue(42);
$rp->setValue(new _ZendTestClass(), 42);
var_dump($rp->getValue());
?>