Skip to content

Commit d471704

Browse files
committed
Take a regular array as second parameter in clone()
1 parent 08dd075 commit d471704

12 files changed

+32
-56
lines changed

Zend/tests/clone/clone_with_001.phpt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ function gen() {
2020

2121
var_dump(clone $x);
2222
var_dump(clone($x));
23-
var_dump(clone($x, foo: $foo, bar: $bar));
24-
var_dump(clone($x, ...$array));
25-
var_dump(clone($x, ...['obj' => $x]));
23+
var_dump(clone($x, [ 'foo' => $foo, 'bar' => $bar ]));
24+
var_dump(clone($x, $array));
25+
var_dump(clone($x, [ 'obj' => $x ]));
2626

27-
var_dump(clone($x, ...[
27+
var_dump(clone($x, [
2828
'abc',
2929
'def',
3030
new Dummy(),
3131
'named' => 'value',
3232
]));
3333

34-
var_dump(clone($x, ...gen()));
35-
3634
?>
3735
--EXPECTF--
3836
object(stdClass)#%d (0) {
@@ -75,7 +73,3 @@ object(stdClass)#%d (4) {
7573
["named"]=>
7674
string(5) "value"
7775
}
78-
object(stdClass)#%d (1) {
79-
["from_gen"]=>
80-
string(5) "value"
81-
}

Zend/tests/clone/clone_with_002.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ class P {
1010
public private(set) string $d = 'default';
1111

1212
public function m1() {
13-
return clone($this, a: 'updated A', b: 'updated B', c: 'updated C', d: 'updated D');
13+
return clone($this, [ 'a' => 'updated A', 'b' => 'updated B', 'c' => 'updated C', 'd' => 'updated D' ]);
1414
}
1515
}
1616

1717
class C extends P {
1818
public function m2() {
19-
return clone($this, a: 'updated A', b: 'updated B', c: 'dynamic C');
19+
return clone($this, [ 'a' => 'updated A', 'b' => 'updated B', 'c' => 'dynamic C' ]);
2020
}
2121

2222
public function m3() {
23-
return clone($this, d: 'inaccessible');
23+
return clone($this, [ 'd' => 'inaccessible' ]);
2424
}
2525
}
2626

2727
class Unrelated {
2828
public function m3(P $p) {
29-
return clone($p, b: 'inaccessible');
29+
return clone($p, [ 'b' => 'inaccessible' ]);
3030
}
3131
}
3232

3333
$p = new P();
3434

35-
var_dump(clone($p, a: 'updated A'));
35+
var_dump(clone($p, [ 'a' => 'updated A' ]));
3636
var_dump($p->m1());
3737

3838
$c = new C();
@@ -45,13 +45,13 @@ try {
4545
}
4646

4747
try {
48-
var_dump(clone($p, b: 'inaccessible'));
48+
var_dump(clone($p, [ 'b' => 'inaccessible' ]));
4949
} catch (Error $e) {
5050
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
5151
}
5252

5353
try {
54-
var_dump(clone($p, d: 'inaccessible'));
54+
var_dump(clone($p, [ 'd' => 'inaccessible' ]));
5555
} catch (Error $e) {
5656
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
5757
}

Zend/tests/clone/clone_with_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Clazz {
1313

1414
$c = new Clazz();
1515

16-
var_dump(clone($c, hooked: 'updated'));
16+
var_dump(clone($c, [ 'hooked' => 'updated' ]));
1717

1818
?>
1919
--EXPECTF--

Zend/tests/clone/clone_with_004.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class Clazz {
3939

4040
$c = new Clazz();
4141

42-
var_dump(clone($c, hooked: 'updated'));
42+
var_dump(clone($c, [ 'hooked' => 'updated' ]));
4343
echo PHP_EOL;
44-
var_dump(clone($c, hooked: 'updated', maxLength: 'abc', minLength: 'abcdef'));
44+
var_dump(clone($c, [ 'hooked' => 'updated', 'maxLength' => 'abc', 'minLength' => 'abcdef' ]));
4545
echo PHP_EOL;
46-
var_dump(clone($c, minLength: 'abcdef', hooked: 'updated', maxLength: 'abc'));
46+
var_dump(clone($c, [ 'minLength' => 'abcdef', 'hooked' => 'updated', 'maxLength' => 'abc' ]));
4747

4848
?>
4949
--EXPECTF--

Zend/tests/clone/clone_with_005.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ class Clazz {
4040
$c = new Clazz();
4141

4242
try {
43-
var_dump(clone($c, hooked: 'updated', maxLength: 'abcdef', minLength: 'abc'));
43+
var_dump(clone($c, [ 'hooked' => 'updated', 'maxLength' => 'abcdef', 'minLength' => 'abc' ]));
4444
} catch (Throwable $e) {
4545
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
4646
}
4747

4848
echo PHP_EOL;
4949

5050
try {
51-
var_dump(clone($c, hooked: 'updated', minLength: 'abc', maxLength: 'abcdef'));
51+
var_dump(clone($c, [ 'hooked' => 'updated', 'minLength' => 'abc', 'maxLength' => 'abcdef' ]));
5252
} catch (Throwable $e) {
5353
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
5454
}

Zend/tests/clone/clone_with_007.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Clazz {
1717

1818
$c = new Clazz('foo', 'bar');
1919

20-
var_dump(clone($c, foo: 'foo updated in clone-with'));
20+
var_dump(clone($c, [ 'foo' => 'foo updated in clone-with' ]));
2121

2222
?>
2323
--EXPECTF--

Zend/tests/clone/clone_with_008.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ readonly class Clazz {
1616

1717
$c = new Clazz('default', 'default');
1818

19-
var_dump(clone($c, a: "with"));
19+
var_dump(clone($c, [ 'a' => "with" ]));
2020

2121
try {
22-
var_dump(clone($c, b: "with"));
22+
var_dump(clone($c, [ 'b' => "with" ]));
2323
} catch (Throwable $e) {
2424
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
2525
}

Zend/tests/clone/clone_with_009.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function test(string $name, object $obj) {
1414
printf("# %s:\n", $name);
1515

1616
$reflector = new ReflectionClass($obj::class);
17-
$clone = clone($obj, a: 2);
17+
$clone = clone($obj, [ 'a' => 2 ]);
1818

1919
var_dump($reflector->isUninitializedLazyObject($obj));
2020
var_dump($obj);

Zend/tests/clone/clone_with_010.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Clone with native classes
44
<?php
55

66
try {
7-
var_dump(clone(new \Random\Engine\Secure(), with: "something"));
7+
var_dump(clone(new \Random\Engine\Secure(), [ 'with' => "something" ]));
88
} catch (Throwable $e) {
99
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
1010
}
1111

1212
try {
13-
var_dump(clone(new \Random\Engine\Xoshiro256StarStar(), with: "something"));
13+
var_dump(clone(new \Random\Engine\Xoshiro256StarStar(), [ 'with' => "something" ]));
1414
} catch (Throwable $e) {
1515
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
1616
}

Zend/zend_builtin_functions.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ zend_result zend_startup_builtin_functions(void) /* {{{ */
7272
ZEND_FUNCTION(clone)
7373
{
7474
zend_object *zobj;
75-
zval *args;
76-
uint32_t argc;
77-
HashTable *named_params;
75+
HashTable *with = NULL;
7876

79-
ZEND_PARSE_PARAMETERS_START(1, -1)
77+
ZEND_PARSE_PARAMETERS_START(1, 2)
8078
Z_PARAM_OBJ(zobj)
81-
Z_PARAM_VARIADIC_WITH_NAMED(args, argc, named_params);
79+
Z_PARAM_OPTIONAL
80+
Z_PARAM_ARRAY_HT(with)
8281
ZEND_PARSE_PARAMETERS_END();
8382

8483
zend_class_entry *scope = zend_get_executed_scope();
@@ -107,26 +106,9 @@ ZEND_FUNCTION(clone)
107106

108107
zend_object *cloned;
109108
if (zobj->handlers->clone_obj_with) {
110-
if (UNEXPECTED(argc > 0)) {
111-
HashTable params;
112-
zend_hash_init(&params, argc + (named_params ? zend_hash_num_elements(named_params) : 0), NULL, NULL, false);
113-
for (uint32_t i = 0; i < argc; i++) {
114-
zend_hash_index_add_new(&params, i, &args[i]);
115-
}
116-
if (named_params != NULL) {
117-
zend_string *key;
118-
zval *val;
119-
ZEND_HASH_FOREACH_STR_KEY_VAL(named_params, key, val) {
120-
zend_hash_update(&params, key, val);
121-
} ZEND_HASH_FOREACH_END();
122-
}
123-
cloned = zobj->handlers->clone_obj_with(zobj, scope, &params);
124-
zend_hash_destroy(&params);
125-
} else {
126-
cloned = zobj->handlers->clone_obj_with(zobj, scope, named_params);
127-
}
109+
cloned = zobj->handlers->clone_obj_with(zobj, scope, with);
128110
} else {
129-
if (UNEXPECTED(named_params || argc > 0)) {
111+
if (UNEXPECTED(with != NULL && zend_hash_num_elements(with) > 0)) {
130112
zend_throw_error(NULL, "Trying to clone an object with updated properties that is not compatible %s", ZSTR_VAL(ce->name));
131113
RETURN_THROWS();
132114
}

Zend/zend_builtin_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class stdClass
77
{
88
}
99

10-
function _clone(object $object, mixed ...$withProperties): object {}
10+
function _clone(object $object, array $withProperties = []): object {}
1111

1212
function exit(string|int $status = 0): never {}
1313

Zend/zend_builtin_functions_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)