Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ae6f52

Browse files
committed
Pass method args as an array to allow optional pass-by-reference
1 parent 2a2f1b8 commit 8ae6f52

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

tests/test_classes/example_class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ public function test_set_reference_method(string &$target = null, string $value)
5454
$target = $value;
5555
}
5656

57+
public function test_value_method(string $value) {
58+
return $value;
59+
}
60+
5761
}

tests/utils/ReflectionTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function testSetStaticProperty() {
177177
* @dataProvider dataCallMethod
178178
*/
179179
public function testCallMethod(string $object_property, string $method_name, int $input, int $expected) {
180-
$this->assertSame($expected, reflection::call_method($this->{$object_property}, $method_name, $input));
180+
$this->assertSame($expected, reflection::call_method($this->{$object_property}, $method_name, [$input]));
181181
}
182182

183183
public function dataCallMethod(): array {
@@ -191,7 +191,7 @@ public function dataCallMethod(): array {
191191
* @dataProvider dataCallStaticMethod
192192
*/
193193
public function testCallStaticMethod(string $class_name, string $method_name, int $input, int $expected) {
194-
$this->assertSame($expected, reflection::call_method($class_name, $method_name, $input));
194+
$this->assertSame($expected, reflection::call_method($class_name, $method_name, [$input]));
195195
}
196196

197197
public function dataCallStaticMethod(): array {
@@ -205,19 +205,26 @@ public function testCallMethodWithReference() {
205205
$test_value = 10;
206206
$expected_value = ($test_value * 2);
207207

208-
reflection::call_method($this->example_object, 'test_reference_method', $test_value);
208+
reflection::call_method($this->example_object, 'test_reference_method', [&$test_value]);
209209

210210
$this->assertSame($expected_value, $test_value);
211211
}
212212

213213
public function testCallMethodSetReferenceValue() {
214214
$expected_value = 'wow such test';
215215

216-
reflection::call_method($this->example_object, 'test_set_reference_method', $actual_value, $expected_value);
216+
reflection::call_method($this->example_object, 'test_set_reference_method', [&$actual_value, &$expected_value]);
217217

218218
$this->assertSame($expected_value, $actual_value);
219219
}
220220

221+
public function testCallMethodWithValue() {
222+
// Not using a variable here as we need to test a value vs a reference
223+
$result = reflection::call_method($this->example_object, 'test_value_method', ['some string']);
224+
225+
$this->assertSame('some string', $result);
226+
}
227+
221228
//
222229

223230
}

utils/reflection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ public static function set_property($object, string $property_name, $value) {
106106
*
107107
* @param mixed $object The object or class name that the function is from
108108
* @param string $method_name The name of the method to be called
109-
* @param mixed ...$args The arguments to pass to the function
109+
* @param array $args The arguments to pass to the function
110110
*
111111
* @return mixed The value returned by the function
112112
*/
113-
public static function call_method($object, string $method_name, &...$args) {
113+
public static function call_method($object, string $method_name, array $args) {
114114
$class_name = (is_object($object)) ? get_class($object) : $object;
115115
$class_name = static::resolve_method_class($class_name, $method_name);
116116

0 commit comments

Comments
 (0)