Skip to content

Commit 8a0efd1

Browse files
committed
Deprecate ReflectionProperty::setValue() with an incorrect 1st arg type
1 parent 416f2b3 commit 8a0efd1

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5722,6 +5722,16 @@ ZEND_METHOD(ReflectionProperty, setValue)
57225722
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
57235723
RETURN_THROWS();
57245724
}
5725+
5726+
if (Z_TYPE_P(tmp) != IS_NULL && Z_TYPE_P(tmp) != IS_OBJECT) {
5727+
zend_string *method_name = get_active_function_or_method_name();
5728+
zend_error(E_DEPRECATED, "Calling %s() with a 1st argument which is not null or an object is deprecated", ZSTR_VAL(method_name));
5729+
zend_string_release(method_name);
5730+
}
5731+
} else {
5732+
zend_string *method_name = get_active_function_or_method_name();
5733+
zend_error(E_DEPRECATED, "Calling %s() with a single argument is deprecated", ZSTR_VAL(method_name));
5734+
zend_string_release(method_name);
57255735
}
57265736

57275737
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ $Prop2 = new ReflectionProperty(T2::class, 'data');
2424
// #1
2525
// prints: hello, hello in PHP5, but world, hello in PHP7 - not OK
2626
$Prop1->setValue(\T1::class, "world");
27-
$Prop2->setValue(\T2::class, 'hello');
27+
set_error_handler(function ($severity, $message, $file, $line) {
28+
throw new Exception($message);
29+
});
30+
try {
31+
$Prop2->setValue(\T2::class, 'hello');
32+
} catch (Exception $e) {
33+
echo $e->getMessage() . "\n";
34+
}
35+
2836
var_dump("T2::self = " . T2::getDataBySelf());
2937
var_dump("T2::static = " . T2::getDataByStatic());
3038

@@ -36,7 +44,9 @@ T2::$data = 'hello';
3644
var_dump("T2::self = " . T2::getDataBySelf());
3745
var_dump("T2::static = " . T2::getDataByStatic());
3846
?>
39-
--EXPECT--
47+
--EXPECTF--
48+
Deprecated: Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated in %s on line %d
49+
Calling ReflectionProperty::setValue() with a 1st argument which is not null or an object is deprecated
4050
string(16) "T2::self = hello"
4151
string(18) "T2::static = hello"
4252
string(16) "T2::self = 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)