Skip to content

RFC: Clone with v2 #18747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/assert/expect_015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ assert(0 && ($a = function () {
$x = $a ?? $b;
[$a, $b, $c] = [1, 2 => 'x', 'z' => 'c'];
@foo();
$y = clone $x;
$y = \clone($x);
yield 1 => 2;
yield from $x;
}))
Expand Down
87 changes: 87 additions & 0 deletions Zend/tests/clone/ast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
Ast Printing
--FILE--
<?php

$x = new stdClass();


try {
assert(false && $y = clone $x);
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone($x));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone($x, [ "foo" => $foo, "bar" => $bar ]));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone($x, $array));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone($x, $array, $extraParameter, $trailingComma, ));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone(object: $x, withProperties: [ "foo" => $foo, "bar" => $bar ]));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone($x, withProperties: [ "foo" => $foo, "bar" => $bar ]));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone(object: $x));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone(object: $x, [ "foo" => $foo, "bar" => $bar ]));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone(...["object" => $x, "withProperties" => [ "foo" => $foo, "bar" => $bar ]]));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
assert(false && $y = clone(...));
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
assert(false && ($y = \clone($x)))
assert(false && ($y = \clone($x)))
assert(false && ($y = \clone($x, ['foo' => $foo, 'bar' => $bar])))
assert(false && ($y = \clone($x, $array)))
assert(false && ($y = \clone($x, $array, $extraParameter, $trailingComma)))
assert(false && ($y = \clone(object: $x, withProperties: ['foo' => $foo, 'bar' => $bar])))
assert(false && ($y = \clone($x, withProperties: ['foo' => $foo, 'bar' => $bar])))
assert(false && ($y = \clone(object: $x)))
assert(false && ($y = \clone(object: $x, ['foo' => $foo, 'bar' => $bar])))
assert(false && ($y = \clone(...['object' => $x, 'withProperties' => ['foo' => $foo, 'bar' => $bar]])))
assert(false && ($y = \clone(...)))
15 changes: 8 additions & 7 deletions Zend/tests/clone/bug36071.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ Bug #36071 (Engine Crash related with 'clone')
error_reporting=4095
--FILE--
<?php
$a = clone 0;
$a[0]->b = 0;
try {
$a = clone 0;
$a[0]->b = 0;
} catch (Error $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
Fatal error: Uncaught Error: __clone method called on non-object in %sbug36071.php:2
Stack trace:
#0 {main}
thrown in %sbug36071.php on line 2
--EXPECT--
TypeError: clone(): Argument #1 ($object) must be of type object, int given
15 changes: 8 additions & 7 deletions Zend/tests/clone/bug42817.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Bug #42817 (clone() on a non-object does not result in a fatal error)
--FILE--
<?php
$a = clone(null);
array_push($a->b, $c);
try {
$a = clone(null);
array_push($a->b, $c);
} catch (Error $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
Fatal error: Uncaught Error: __clone method called on non-object in %sbug42817.php:2
Stack trace:
#0 {main}
thrown in %sbug42817.php on line 2
--EXPECT--
TypeError: clone(): Argument #1 ($object) must be of type object, null given
13 changes: 7 additions & 6 deletions Zend/tests/clone/bug42818.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
Bug #42818 ($foo = clone(array()); leaks memory)
--FILE--
<?php
$foo = clone(array());
try {
$foo = clone(array());
} catch (Error $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
Fatal error: Uncaught Error: __clone method called on non-object in %sbug42818.php:2
Stack trace:
#0 {main}
thrown in %sbug42818.php on line 2
--EXPECT--
TypeError: clone(): Argument #1 ($object) must be of type object, array given
13 changes: 7 additions & 6 deletions Zend/tests/clone/clone_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ Using clone statement on non-object
--FILE--
<?php

$a = clone array();
try {
$a = clone array();
} catch (Error $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}

?>
--EXPECTF--
Fatal error: Uncaught Error: __clone method called on non-object in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
--EXPECT--
TypeError: clone(): Argument #1 ($object) must be of type object, array given
12 changes: 6 additions & 6 deletions Zend/tests/clone/clone_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Using clone statement on undefined variable
--FILE--
<?php

$a = clone $b;
try {
$a = clone $b;
} catch (Error $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}

?>
--EXPECTF--
Warning: Undefined variable $b in %s on line %d

Fatal error: Uncaught Error: __clone method called on non-object in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
TypeError: clone(): Argument #1 ($object) must be of type object, null given
55 changes: 55 additions & 0 deletions Zend/tests/clone/clone_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Clone as a function.
--FILE--
<?php

$x = new stdClass();

\var_dump(\clone($x));
\var_dump(\array_map('clone', [$x, $x, $x]));
\var_dump(\array_map(clone(...), [$x, $x, $x]));

class Foo {
private function __clone() {

}

public function clone_me() {
// Verify visibility when going through array_map().
return array_map(\clone(...), [$this]);
}
}

$f = new Foo();

$clone = $f->clone_me()[0];

var_dump($f !== $clone);

?>
--EXPECTF--
object(stdClass)#%d (0) {
}
array(3) {
[0]=>
object(stdClass)#%d (0) {
}
[1]=>
object(stdClass)#%d (0) {
}
[2]=>
object(stdClass)#%d (0) {
}
}
array(3) {
[0]=>
object(stdClass)#%d (0) {
}
[1]=>
object(stdClass)#%d (0) {
}
[2]=>
object(stdClass)#%d (0) {
}
}
bool(true)
71 changes: 71 additions & 0 deletions Zend/tests/clone/clone_with_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
Clone with basic
--FILE--
<?php

class Dummy { }

$x = new stdClass();

$foo = 'FOO';
$bar = new Dummy();
$array = [
'baz' => 'BAZ',
'array' => [1, 2, 3],
];

var_dump(clone $x);
var_dump(clone($x));
var_dump(clone($x, [ 'foo' => $foo, 'bar' => $bar ]));
var_dump(clone($x, $array));
var_dump(clone($x, [ 'obj' => $x ]));

var_dump(clone($x, [
'abc',
'def',
new Dummy(),
'named' => 'value',
]));

?>
--EXPECTF--
object(stdClass)#%d (0) {
}
object(stdClass)#%d (0) {
}
object(stdClass)#%d (2) {
["foo"]=>
string(3) "FOO"
["bar"]=>
object(Dummy)#%d (0) {
}
}
object(stdClass)#%d (2) {
["baz"]=>
string(3) "BAZ"
["array"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
object(stdClass)#%d (1) {
["obj"]=>
object(stdClass)#%d (0) {
}
}
object(stdClass)#%d (4) {
["0"]=>
string(3) "abc"
["1"]=>
string(3) "def"
["2"]=>
object(Dummy)#%d (0) {
}
["named"]=>
string(5) "value"
}
Loading