Skip to content

Commit f7bcc7d

Browse files
committed
Migrate to clone function
1 parent 1239b26 commit f7bcc7d

10 files changed

+66
-19
lines changed

Zend/tests/clone/ast.phpt

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ try {
4242

4343
?>
4444
--EXPECT--
45-
assert(false && ($y = clone($x)))
46-
assert(false && ($y = clone($x)))
47-
assert(false && ($y = clone($x, foo: $foo, bar: $bar)))
48-
assert(false && ($y = clone($x, ...$array)))
49-
assert(false && ($y = clone($x, ...['foo' => $foo, 'bar' => $bar])))
45+
assert(false && ($y = \clone($x)))
46+
assert(false && ($y = \clone($x)))
47+
assert(false && ($y = \clone($x, foo: $foo, bar: $bar)))
48+
assert(false && ($y = \clone($x, ...$array)))
49+
assert(false && ($y = \clone($x, ...['foo' => $foo, 'bar' => $bar])))

Zend/tests/clone/bug36071.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ $a = clone 0;
88
$a[0]->b = 0;
99
?>
1010
--EXPECTF--
11-
Fatal error: Uncaught Error: __clone method called on non-object in %sbug36071.php:2
11+
Fatal error: Uncaught TypeError: clone(): Argument #1 ($object) must be of type object, int given in %s:%d
1212
Stack trace:
13-
#0 {main}
13+
#0 %s(%d): clone(0)
14+
#1 {main}
1415
thrown in %sbug36071.php on line 2

Zend/tests/clone/bug42817.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ $a = clone(null);
66
array_push($a->b, $c);
77
?>
88
--EXPECTF--
9-
Fatal error: Uncaught Error: __clone method called on non-object in %sbug42817.php:2
9+
Fatal error: Uncaught TypeError: clone(): Argument #1 ($object) must be of type object, null given in %s:%d
1010
Stack trace:
11-
#0 {main}
11+
#0 %s(%d): clone(NULL)
12+
#1 {main}
1213
thrown in %sbug42817.php on line 2

Zend/tests/clone/bug42818.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Bug #42818 ($foo = clone(array()); leaks memory)
55
$foo = clone(array());
66
?>
77
--EXPECTF--
8-
Fatal error: Uncaught Error: __clone method called on non-object in %sbug42818.php:2
8+
Fatal error: Uncaught TypeError: clone(): Argument #1 ($object) must be of type object, array given in %s:%d
99
Stack trace:
10-
#0 {main}
10+
#0 %s(%d): clone(Array)
11+
#1 {main}
1112
thrown in %sbug42818.php on line 2

Zend/tests/clone/callback.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
As Callback
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
private function __clone() {
8+
9+
}
10+
11+
public function clone_me() {
12+
return array_map(clone(...), [$this]);
13+
}
14+
}
15+
16+
$f = new Foo();
17+
18+
$clone = $f->clone_me()[0];
19+
20+
var_dump($f !== $clone);
21+
22+
?>
23+
--EXPECT--
24+
bool(true)

Zend/tests/clone/clone_001.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ $a = clone array();
77

88
?>
99
--EXPECTF--
10-
Fatal error: Uncaught Error: __clone method called on non-object in %s:%d
10+
Fatal error: Uncaught TypeError: clone(): Argument #1 ($object) must be of type object, array given in %s:%d
1111
Stack trace:
12-
#0 {main}
12+
#0 %s(%d): clone(Array)
13+
#1 {main}
1314
thrown in %s on line %d

Zend/tests/clone/clone_003.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ $a = clone $b;
99
--EXPECTF--
1010
Warning: Undefined variable $b in %s on line %d
1111

12-
Fatal error: Uncaught Error: __clone method called on non-object in %s:%d
12+
Fatal error: Uncaught TypeError: clone(): Argument #1 ($object) must be of type object, null given in %s:%d
1313
Stack trace:
14-
#0 {main}
14+
#0 %s(%d): clone(NULL)
15+
#1 {main}
1516
thrown in %s on line %d

Zend/tests/clone/clone_with_001.phpt

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ $array = [
1212
'arra' => [1, 2, 3],
1313
];
1414

15+
function gen() {
16+
yield 'from_gen' => 'value';
17+
}
18+
1519
var_dump(clone $x);
1620
var_dump(clone($x));
1721
var_dump(clone($x, foo: $foo, bar: $bar));
@@ -23,6 +27,8 @@ var_dump(clone($x, ...[
2327
"def",
2428
]));
2529

30+
var_dump(clone($x, ...gen()));
31+
2632
?>
2733
--EXPECTF--
2834
object(stdClass)#%d (0) {
@@ -59,3 +65,7 @@ object(stdClass)#%d (2) {
5965
["1"]=>
6066
string(3) "def"
6167
}
68+
object(stdClass)#%d (1) {
69+
["from_gen"]=>
70+
string(5) "value"
71+
}

Zend/tests/clone/clone_with_006.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ try {
1313

1414
?>
1515
--EXPECT--
16-
TypeError: Only arrays can be unpacked for clone, int given
16+
TypeError: Only arrays and Traversables can be unpacked, int given

Zend/zend_language_parser.y

+11-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
4646
%define api.pure full
4747
%define api.value.type {zend_parser_stack_elem}
4848
%define parse.error verbose
49-
%expect 0
49+
%expect 1
5050

5151
%destructor { zend_ast_destroy($$); } <ast>
5252
%destructor { if ($$) zend_string_release_ex($$, 0); } <str>
@@ -1214,8 +1214,16 @@ expr:
12141214
{ $$ = zend_ast_create(ZEND_AST_ASSIGN, $1, $3); }
12151215
| variable '=' ampersand variable
12161216
{ $$ = zend_ast_create(ZEND_AST_ASSIGN_REF, $1, $4); }
1217-
| T_CLONE '(' expr ',' non_empty_argument_list ')' { $$ = zend_ast_create(ZEND_AST_CLONE, $3, $5); }
1218-
| T_CLONE expr { $$ = zend_ast_create(ZEND_AST_CLONE, $2, NULL); }
1217+
| T_CLONE argument_list {
1218+
zend_ast *name = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_CLONE));
1219+
name->attr = ZEND_NAME_FQ;
1220+
$$ = zend_ast_create(ZEND_AST_CALL, name, $2);
1221+
}
1222+
| T_CLONE expr {
1223+
zend_ast *name = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_CLONE));
1224+
name->attr = ZEND_NAME_FQ;
1225+
$$ = zend_ast_create(ZEND_AST_CALL, name, zend_ast_create_list(1, ZEND_AST_ARG_LIST, $2));
1226+
}
12191227
| variable T_PLUS_EQUAL expr
12201228
{ $$ = zend_ast_create_assign_op(ZEND_ADD, $1, $3); }
12211229
| variable T_MINUS_EQUAL expr

0 commit comments

Comments
 (0)