-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Allow readonly properties to be reinitialized once during cloning #10389
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d59718c
Allow readonly properties to be reinitialized once during cloning
kocsismate 217f83d
Some review fixes
kocsismate c2e48fc
Review fixes
kocsismate 556ed13
Make it possible to reinitialize readonly properties after a type error
kocsismate 75d4d9d
Fix tests
kocsismate 2cf102c
Use bit-shift
kocsismate 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
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,36 @@ | ||
--TEST-- | ||
Readonly property cannot be reset twice during cloning | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly int $bar | ||
) {} | ||
|
||
public function __clone() | ||
{ | ||
$this->bar = 2; | ||
var_dump($this); | ||
$this->bar = 3; | ||
} | ||
} | ||
|
||
$foo = new Foo(1); | ||
|
||
try { | ||
clone $foo; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
echo "done"; | ||
|
||
?> | ||
--EXPECT-- | ||
object(Foo)#2 (1) { | ||
["bar"]=> | ||
int(2) | ||
} | ||
Cannot modify readonly property Foo::$bar | ||
done |
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,42 @@ | ||
--TEST-- | ||
Readonly property cannot be reset after cloning when there is no custom clone handler | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly int $bar, | ||
public readonly int $baz | ||
) {} | ||
|
||
public function wrongCloneOld() | ||
{ | ||
$instance = clone $this; | ||
$this->bar++; | ||
} | ||
|
||
public function wrongCloneNew() | ||
{ | ||
$instance = clone $this; | ||
$instance->baz++; | ||
} | ||
} | ||
|
||
$foo = new Foo(1, 1); | ||
|
||
try { | ||
$foo->wrongCloneOld(); | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->wrongCloneNew(); | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot modify readonly property Foo::$bar | ||
Cannot modify readonly property Foo::$baz |
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,44 @@ | ||
--TEST-- | ||
Readonly property cannot be reset after cloning when there is a custom clone handler | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly int $bar, | ||
public readonly int $baz | ||
) {} | ||
|
||
public function __clone() {} | ||
|
||
public function wrongCloneOld() | ||
{ | ||
$instance = clone $this; | ||
$this->bar++; | ||
} | ||
|
||
public function wrongCloneNew() | ||
{ | ||
$instance = clone $this; | ||
$instance->baz++; | ||
} | ||
} | ||
|
||
$foo = new Foo(1, 1); | ||
|
||
try { | ||
$foo->wrongCloneOld(); | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->wrongCloneNew(); | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot modify readonly property Foo::$bar | ||
Cannot modify readonly property Foo::$baz |
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,39 @@ | ||
--TEST-- | ||
Readonly property can be reset once during cloning | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly int $bar | ||
) {} | ||
|
||
public function __clone() | ||
{ | ||
$this->bar++; | ||
} | ||
} | ||
|
||
$foo = new Foo(1); | ||
|
||
var_dump(clone $foo); | ||
|
||
$foo2 = clone $foo; | ||
var_dump($foo2); | ||
|
||
var_dump(clone $foo2); | ||
|
||
?> | ||
--EXPECTF-- | ||
object(Foo)#%d (%d) { | ||
["bar"]=> | ||
int(2) | ||
} | ||
object(Foo)#%d (%d) { | ||
["bar"]=> | ||
int(2) | ||
} | ||
object(Foo)#%d (%d) { | ||
["bar"]=> | ||
int(3) | ||
} |
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,46 @@ | ||
--TEST-- | ||
Test that __clone() unset and reassign properties | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly stdClass $bar, | ||
) {} | ||
|
||
public function __clone() | ||
{ | ||
unset($this->bar); | ||
var_dump($this); | ||
$this->bar = new stdClass(); | ||
} | ||
} | ||
|
||
$foo = new Foo(new stdClass()); | ||
var_dump($foo); | ||
$foo2 = clone $foo; | ||
|
||
var_dump($foo); | ||
var_dump($foo2); | ||
|
||
?> | ||
--EXPECTF-- | ||
object(Foo)#1 (%d) { | ||
["bar"]=> | ||
object(stdClass)#2 (%d) { | ||
} | ||
} | ||
object(Foo)#3 (%d) { | ||
["bar"]=> | ||
uninitialized(stdClass) | ||
} | ||
object(Foo)#1 (%d) { | ||
["bar"]=> | ||
object(stdClass)#2 (%d) { | ||
} | ||
} | ||
object(Foo)#3 (%d) { | ||
["bar"]=> | ||
object(stdClass)#4 (%d) { | ||
} | ||
} |
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,37 @@ | ||
--TEST-- | ||
__clone() can indirectly modify unlocked readonly properties | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly array $bar | ||
) {} | ||
|
||
public function __clone() | ||
{ | ||
$this->bar['bar'] = 'bar'; | ||
} | ||
} | ||
|
||
$foo = new Foo([]); | ||
// First call fills the cache slot | ||
var_dump(clone $foo); | ||
var_dump(clone $foo); | ||
|
||
?> | ||
--EXPECTF-- | ||
object(Foo)#2 (%d) { | ||
["bar"]=> | ||
array(1) { | ||
["bar"]=> | ||
string(3) "bar" | ||
} | ||
} | ||
object(Foo)#2 (%d) { | ||
["bar"]=> | ||
array(1) { | ||
["bar"]=> | ||
string(3) "bar" | ||
} | ||
} |
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,39 @@ | ||
--TEST-- | ||
Readonly property can be reset once during cloning even after a type error | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public function __construct( | ||
public readonly int $bar | ||
) {} | ||
|
||
public function __clone() | ||
{ | ||
try { | ||
$this->bar = "foo"; | ||
} catch (Error $e) { | ||
echo $e->getMessage() . "\n"; | ||
} | ||
|
||
$this->bar = 2; | ||
} | ||
} | ||
|
||
$foo = new Foo(1); | ||
|
||
var_dump(clone $foo); | ||
var_dump(clone $foo); | ||
|
||
?> | ||
--EXPECTF-- | ||
Cannot assign string to property Foo::$bar of type int | ||
object(Foo)#%d (%d) { | ||
["bar"]=> | ||
int(2) | ||
} | ||
Cannot assign string to property Foo::$bar of type int | ||
object(Foo)#%d (%d) { | ||
["bar"]=> | ||
int(2) | ||
} |
23 changes: 23 additions & 0 deletions
23
Zend/tests/readonly_props/readonly_coercion_type_error.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,23 @@ | ||
--TEST-- | ||
Test failing readonly assignment with coercion | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public readonly string $bar; | ||
|
||
public function __construct() { | ||
$this->bar = 'bar'; | ||
try { | ||
$this->bar = 42; | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
} | ||
|
||
new Foo(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Cannot modify readonly property Foo::$bar |
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.