-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add support for property initialization during cloning #6538
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
Zend/tests/clone_initializer/clone_initializer_error1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Test that the property initializer list is required when "with" is given | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public function withProperties() | ||
{ | ||
return clone $this with; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token ";", expecting "{" in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/clone_initializer/clone_initializer_error2.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Test that the property initializer list cannot contain integer keys | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public function withProperties() | ||
{ | ||
return clone $this with {1: "value"}; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected integer "1" in %s on line %d |
18 changes: 18 additions & 0 deletions
18
Zend/tests/clone_initializer/clone_initializer_error3.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Test that the property initializer list can only contain literals | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public function withProperties() | ||
{ | ||
$property = "string"; | ||
|
||
return clone $this with {$property: "value"}; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected variable "$property" in %s on line %d |
21 changes: 21 additions & 0 deletions
21
Zend/tests/clone_initializer/clone_initializer_error4.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Test that the clone property initializer respects visibility | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
private $bar; | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
try { | ||
$foo = clone $foo with {bar: 1}; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot access private property Foo::$bar |
19 changes: 19 additions & 0 deletions
19
Zend/tests/clone_initializer/clone_initializer_error5.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Test that the "clone with" doesn't support expressions as property names | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public $bar; | ||
} | ||
|
||
$property = "bar"; | ||
|
||
$foo = new Foo(); | ||
$foo = clone $foo with {$property: 1}; | ||
var_dump($foo); | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected variable "$property" in %s on line %d |
38 changes: 38 additions & 0 deletions
38
Zend/tests/clone_initializer/clone_initializer_success1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--TEST-- | ||
Test that declared properties can be initialized during cloning | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public int $property1; | ||
public string $property2; | ||
|
||
public function withProperties() | ||
{ | ||
return clone $this with { | ||
property1: 1, | ||
property2: "foo", | ||
}; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
var_dump($foo); | ||
$bar = $foo->withProperties(); | ||
var_dump($bar); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#1 (0) { | ||
["property1"]=> | ||
uninitialized(int) | ||
["property2"]=> | ||
uninitialized(string) | ||
} | ||
object(Foo)#2 (2) { | ||
["property1"]=> | ||
int(1) | ||
["property2"]=> | ||
string(3) "foo" | ||
} |
31 changes: 31 additions & 0 deletions
31
Zend/tests/clone_initializer/clone_initializer_success2.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
Test that dynamic properties can be initialized during cloning | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public function withProperties() | ||
{ | ||
return clone $this with { | ||
property1: 1, | ||
property2: "foo", | ||
}; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
var_dump($foo); | ||
$bar = $foo->withProperties(); | ||
var_dump($bar); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#1 (0) { | ||
} | ||
object(Foo)#2 (2) { | ||
["property1"]=> | ||
int(1) | ||
["property2"]=> | ||
string(3) "foo" | ||
} |
24 changes: 24 additions & 0 deletions
24
Zend/tests/clone_initializer/clone_initializer_success3.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
Test that the property initializer list can be empty | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public function withProperties() | ||
{ | ||
return clone $this with {}; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
var_dump($foo); | ||
$bar = $foo->withProperties(); | ||
var_dump($bar); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#1 (0) { | ||
} | ||
object(Foo)#2 (0) { | ||
} |
27 changes: 27 additions & 0 deletions
27
Zend/tests/clone_initializer/clone_initializer_success4.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
Test that the "clone with" is still chainable | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public $bar = 1; | ||
|
||
public function withProperties() | ||
{ | ||
return (clone $this with {})->bar; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
var_dump($foo); | ||
$bar = $foo->withProperties(); | ||
var_dump($bar); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#1 (1) { | ||
["bar"]=> | ||
int(1) | ||
} | ||
int(1) |
21 changes: 21 additions & 0 deletions
21
Zend/tests/clone_initializer/clone_initializer_success5.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Test that the "clone with" works with dynamic properties | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
} | ||
|
||
$foo = new Foo(); | ||
$foo = clone $foo with {bar: 1, baz: ""}; | ||
var_dump($foo); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#2 (2) { | ||
["bar"]=> | ||
int(1) | ||
["baz"]=> | ||
string(0) "" | ||
} |
24 changes: 24 additions & 0 deletions
24
Zend/tests/clone_initializer/clone_initializer_success6.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
Test that the "clone with" works with expressions as property values | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public $bar; | ||
public $baz; | ||
} | ||
|
||
$foo = new Foo(); | ||
$foo = clone $foo with {bar: new stdClass(), baz: strpos("abc", "b")}; | ||
var_dump($foo); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#2 (2) { | ||
["bar"]=> | ||
object(stdClass)#3 (0) { | ||
} | ||
["baz"]=> | ||
int(1) | ||
} |
20 changes: 20 additions & 0 deletions
20
Zend/tests/clone_initializer/clone_initializer_success7.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Test that the "clone with" works with expressions as clone operand | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
public static function create() | ||
{ | ||
return new Foo(); | ||
} | ||
} | ||
|
||
$foo = clone Foo::create() with {}; | ||
var_dump($foo); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#2 (0) { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could add a test of this with function calls, e.g.
clone $this with { strTypedProperty: throw new Exception("test") }
)strtolower("ABC")
are freed if there's a type error assigning to a typed propertyEDIT: Oh. So far, this is only implemented for constant operands in Zend/zend_vm_def.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ideas, I'll definitely add such tests!