-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Disallow assigning reference to unset readonly property #8188
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
GH-7942: Disallow assigning reference to unset readonly property | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public readonly int $bar; | ||
public function __construct(int &$bar) { | ||
$this->bar = &$bar; | ||
} | ||
} | ||
|
||
try { | ||
$i = 42; | ||
new Foo($i); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot indirectly 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--TEST-- | ||
Readonly variations | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public readonly int $prop; | ||
|
||
public function init() { | ||
$this->prop = 1; | ||
} | ||
|
||
public function r() { | ||
echo $this->prop; | ||
} | ||
|
||
public function w() { | ||
$this->prop = 1; | ||
echo 'done'; | ||
} | ||
|
||
public function rw() { | ||
$this->prop += 1; | ||
echo 'done'; | ||
} | ||
|
||
public function im() { | ||
$this->prop[] = 1; | ||
echo 'done'; | ||
} | ||
|
||
public function is() { | ||
echo (int) isset($this->prop); | ||
} | ||
|
||
public function us() { | ||
unset($this->prop); | ||
echo 'done'; | ||
} | ||
} | ||
|
||
function r($test) { | ||
echo $test->prop; | ||
} | ||
|
||
function w($test) { | ||
$test->prop = 0; | ||
echo 'done'; | ||
} | ||
|
||
function rw($test) { | ||
$test->prop += 1; | ||
echo 'done'; | ||
} | ||
|
||
function im($test) { | ||
$test->prop[] = 1; | ||
echo 'done'; | ||
} | ||
|
||
function is($test) { | ||
echo (int) isset($test->prop); | ||
} | ||
|
||
function us($test) { | ||
unset($test->prop); | ||
echo 'done'; | ||
} | ||
|
||
foreach ([true, false] as $init) { | ||
foreach ([true, false] as $scope) { | ||
foreach (['r', 'w', 'rw', 'im', 'is', 'us'] as $op) { | ||
$test = new Test(); | ||
if ($init) { | ||
$test->init(); | ||
} | ||
|
||
echo 'Init: ' . ((int) $init) . ', scope: ' . ((int) $scope) . ', op: ' . $op . ": "; | ||
try { | ||
if ($scope) { | ||
$test->{$op}(); | ||
} else { | ||
$op($test); | ||
} | ||
} catch (Error $e) { | ||
echo $e->getMessage(); | ||
} | ||
echo "\n"; | ||
} | ||
} | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Init: 1, scope: 1, op: r: 1 | ||
Init: 1, scope: 1, op: w: Cannot modify readonly property Test::$prop | ||
Init: 1, scope: 1, op: rw: Cannot modify readonly property Test::$prop | ||
Init: 1, scope: 1, op: im: Cannot modify readonly property Test::$prop | ||
Init: 1, scope: 1, op: is: 1 | ||
Init: 1, scope: 1, op: us: Cannot unset readonly property Test::$prop | ||
Init: 1, scope: 0, op: r: 1 | ||
Init: 1, scope: 0, op: w: Cannot modify readonly property Test::$prop | ||
Init: 1, scope: 0, op: rw: Cannot modify readonly property Test::$prop | ||
Init: 1, scope: 0, op: im: Cannot modify readonly property Test::$prop | ||
Init: 1, scope: 0, op: is: 1 | ||
Init: 1, scope: 0, op: us: Cannot unset readonly property Test::$prop | ||
Init: 0, scope: 1, op: r: Typed property Test::$prop must not be accessed before initialization | ||
Init: 0, scope: 1, op: w: done | ||
Init: 0, scope: 1, op: rw: Typed property Test::$prop must not be accessed before initialization | ||
Init: 0, scope: 1, op: im: Cannot indirectly modify readonly property Test::$prop | ||
Init: 0, scope: 1, op: is: 0 | ||
Init: 0, scope: 1, op: us: done | ||
Init: 0, scope: 0, op: r: Typed property Test::$prop must not be accessed before initialization | ||
Init: 0, scope: 0, op: w: Cannot initialize readonly property Test::$prop from global scope | ||
Init: 0, scope: 0, op: rw: Typed property Test::$prop must not be accessed before initialization | ||
Init: 0, scope: 0, op: im: Cannot indirectly modify readonly property Test::$prop | ||
Init: 0, scope: 0, op: is: 0 | ||
Init: 0, scope: 0, op: us: Cannot unset readonly property Test::$prop from global scope |
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,77 @@ | ||
--TEST-- | ||
Readonly nested variations | ||
--FILE-- | ||
<?php | ||
|
||
class Inner { | ||
public int $prop = 1; | ||
public array $array = []; | ||
} | ||
|
||
class Test { | ||
public readonly Inner $prop; | ||
|
||
public function init() { | ||
$this->prop = new Inner(); | ||
} | ||
} | ||
|
||
function r($test) { | ||
echo $test->prop->prop; | ||
} | ||
|
||
function w($test) { | ||
$test->prop->prop = 0; | ||
echo 'done'; | ||
} | ||
|
||
function rw($test) { | ||
$test->prop->prop += 1; | ||
echo 'done'; | ||
} | ||
|
||
function im($test) { | ||
$test->prop->array[] = 1; | ||
echo 'done'; | ||
} | ||
|
||
function is($test) { | ||
echo (int) isset($test->prop->prop); | ||
} | ||
|
||
function us($test) { | ||
unset($test->prop->prop); | ||
echo 'done'; | ||
} | ||
|
||
foreach ([true, false] as $init) { | ||
foreach (['r', 'w', 'rw', 'im', 'is', 'us'] as $op) { | ||
$test = new Test(); | ||
if ($init) { | ||
$test->init(); | ||
} | ||
|
||
echo 'Init: ' . ((int) $init) . ', op: ' . $op . ": "; | ||
try { | ||
$op($test); | ||
} catch (Error $e) { | ||
echo $e->getMessage(); | ||
} | ||
echo "\n"; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Init: 1, op: r: 1 | ||
Init: 1, op: w: done | ||
Init: 1, op: rw: done | ||
Init: 1, op: im: done | ||
Init: 1, op: is: 1 | ||
Init: 1, op: us: done | ||
Init: 0, op: r: Typed property Test::$prop must not be accessed before initialization | ||
Init: 0, op: w: Cannot indirectly modify readonly property Test::$prop | ||
Init: 0, op: rw: Typed property Test::$prop must not be accessed before initialization | ||
Init: 0, op: im: Cannot indirectly modify readonly property Test::$prop | ||
Init: 0, op: is: 0 | ||
Init: 0, op: us: 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
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
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.
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.
Add a string concatenation variation maybe? And maybe
++
,--
as IIRC those are different opcodesThere 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.
+=
and.=
both result inASSIGN_OP
.++
might be worth checking. I'll adjust the test.