Skip to content

Commit d9a7f67

Browse files
committed
Deprecate ReflectionProperty::setValue() with an incorrect 1st arg type
1 parent f41220f commit d9a7f67

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

Zend/tests/bug78868.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spl_autoload_register('main_autoload');
2525

2626
$classA = new ReflectionClass("A");
2727
$props = $classA->getProperties();
28-
$props[0]->setValue(2); //causes constant resolving, which runs autoload, all with EG(fake_scope) == "A"
28+
$props[0]->setValue(null, 2); //causes constant resolving, which runs autoload, all with EG(fake_scope) == "A"
2929

3030
echo "OK\n";
3131
?>

ext/reflection/php_reflection.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5720,6 +5720,22 @@ ZEND_METHOD(ReflectionProperty, setValue)
57205720
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
57215721
RETURN_THROWS();
57225722
}
5723+
5724+
if (Z_TYPE_P(tmp) != IS_NULL && Z_TYPE_P(tmp) != IS_OBJECT) {
5725+
zend_string *method_name = get_active_function_or_method_name();
5726+
zend_error(E_DEPRECATED, "Calling %s() with a 1st argument which is not null or an object is deprecated", ZSTR_VAL(method_name));
5727+
zend_string_release(method_name);
5728+
if (UNEXPECTED(EG(exception))) {
5729+
RETURN_THROWS();
5730+
}
5731+
}
5732+
} else {
5733+
zend_string *method_name = get_active_function_or_method_name();
5734+
zend_error(E_DEPRECATED, "Calling %s() with a single argument is deprecated", ZSTR_VAL(method_name));
5735+
zend_string_release(method_name);
5736+
if (UNEXPECTED(EG(exception))) {
5737+
RETURN_THROWS();
5738+
}
57235739
}
57245740

57255741
zend_update_static_property_ex(intern->ce, ref->unmangled_name, value);

ext/reflection/tests/ReflectionProperty_setAccessible.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ var_dump($private->getValue($a));
2323
var_dump($privateStatic->getValue());
2424

2525
$protected->setValue($a, 'e');
26-
$protectedStatic->setValue('f');
26+
$protectedStatic->setValue(null, 'f');
2727
$private->setValue($a, 'g');
28-
$privateStatic->setValue('h');
28+
$privateStatic->setValue(null, 'h');
2929

3030
var_dump($protected->getValue($a));
3131
var_dump($protectedStatic->getValue());
@@ -45,7 +45,7 @@ var_dump($privateStatic->getValue());
4545
$protected->setValue($a, 'i');
4646
$protectedStatic->setValue('j');
4747
$private->setValue($a, 'k');
48-
$privateStatic->setValue('l');
48+
$privateStatic->setValue(null, 'l');
4949

5050
var_dump($protected->getValue($a));
5151
var_dump($protectedStatic->getValue());
@@ -63,7 +63,7 @@ var_dump($protectedStatic->getValue());
6363
var_dump($private->getValue($b));
6464

6565
$protected->setValue($b, 'e');
66-
$protectedStatic->setValue('f');
66+
$protectedStatic->setValue(null, 'f');
6767
$private->setValue($b, 'g');
6868

6969
var_dump($protected->getValue($b));
@@ -79,14 +79,14 @@ var_dump($protectedStatic->getValue());
7979
var_dump($private->getValue($b));
8080

8181
$protected->setValue($b, 'h');
82-
$protectedStatic->setValue('i');
82+
$protectedStatic->setValue(null, 'i');
8383
$private->setValue($b, 'j');
8484

8585
var_dump($protected->getValue($b));
8686
var_dump($protectedStatic->getValue());
8787
var_dump($private->getValue($b));
8888
?>
89-
--EXPECT--
89+
--EXPECTF--
9090
string(1) "a"
9191
string(1) "b"
9292
string(1) "c"
@@ -99,6 +99,8 @@ string(1) "e"
9999
string(1) "f"
100100
string(1) "g"
101101
string(1) "h"
102+
103+
Deprecated: Calling ReflectionProperty::setValue() with a single argument is deprecated in %s on line %d
102104
string(1) "i"
103105
string(1) "j"
104106
string(1) "k"

ext/reflection/tests/ReflectionProperty_typed_static.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ try {
1919
echo $e->getMessage(), "\n";
2020
}
2121

22-
$rp->setValue("24");
22+
$rp->setValue(null, "24");
2323
var_dump($rp->getValue());
2424

2525
try {
26-
$rp->setValue("foo");
26+
$rp->setValue(null, "foo");
2727
} catch (TypeError $e) {
2828
echo $e->getMessage(), "\n";
2929
}
@@ -33,7 +33,7 @@ Test::$z =& Test::$y;
3333

3434
$rp = new ReflectionProperty('Test', 'z');
3535
try {
36-
$rp->setValue("foo");
36+
$rp->setValue(null, "foo");
3737
} catch (TypeError $e) {
3838
echo $e->getMessage(), "\n";
3939
}

ext/reflection/tests/bug30146.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var_dump($r->getValue());
1515
$r->setValue(3);
1616
var_dump($r->getValue());
1717
?>
18-
--EXPECT--
18+
--EXPECTF--
1919
int(1)
2020
int(2)
21+
22+
Deprecated: Calling ReflectionProperty::setValue() with a single argument is deprecated in %s on line %d
2123
int(3)

ext/reflection/tests/bug71018.phpt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ $Prop2->setValue(\T2::class, 'hello');
2828
var_dump("T2::self = " . T2::getDataBySelf());
2929
var_dump("T2::static = " . T2::getDataByStatic());
3030

31+
set_error_handler(function ($severity, $message, $file, $line) {
32+
throw new Exception($message);
33+
});
34+
try {
35+
$Prop2->setValue(\T2::class, 'hi');
36+
} catch (Exception $e) {
37+
echo $e->getMessage() . "\n";
38+
}
39+
40+
var_dump("T2::self = " . T2::getDataByStatic());
41+
3142
// #2
3243
// prints: hello, hello in both PHP5 and PHP7 - OK
3344
T1::$data = "world";
@@ -36,8 +47,13 @@ T2::$data = 'hello';
3647
var_dump("T2::self = " . T2::getDataBySelf());
3748
var_dump("T2::static = " . T2::getDataByStatic());
3849
?>
39-
--EXPECT--
50+
--EXPECTF--
51+
Deprecated: Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated in %s on line %d
52+
53+
Deprecated: Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated in %s on line %d
4054
string(16) "T2::self = hello"
4155
string(18) "T2::static = hello"
56+
Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated
57+
string(16) "T2::self = hello"
4258
string(16) "T2::self = hello"
4359
string(18) "T2::static = hello"

ext/reflection/tests/internal_static_property.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ zend_test
66
<?php
77

88
$rp = new ReflectionProperty('_ZendTestClass', '_StaticProp');
9-
$rp->setValue(42);
9+
$rp->setValue(new _ZendTestClass(), 42);
1010
var_dump($rp->getValue());
1111

1212
?>

0 commit comments

Comments
 (0)