-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add support for readonly classes #7305
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
7 commits
Select commit
Hold shift + click to select a range
ea938d4
Add support for readonly classes
kocsismate 809b093
Fix casing of isReadOnly() method
kocsismate b4dad15
Add test + improve error message for static properties of readonly cl…
kocsismate 40edde6
Update tests
kocsismate 1ea558c
Disallow the usage of the #[AllowDynamicProperties] attribute
kocsismate da1e5f3
Add test for final readonly classes
kocsismate c5efb74
Address review comment
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
12 changes: 12 additions & 0 deletions
12
Zend/tests/readonly_classes/readonly_class_duplicated_modifier.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,12 @@ | ||
--TEST-- | ||
The readonly class modifier can only be added once | ||
--FILE-- | ||
<?php | ||
|
||
readonly readonly class Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Multiple readonly modifiers are not allowed in %s on line %d |
20 changes: 20 additions & 0 deletions
20
Zend/tests/readonly_classes/readonly_class_dynamic_property.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-- | ||
Readonly classes cannot use dynamic properties | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
try { | ||
$foo->bar = 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot create dynamic property Foo::$bar |
13 changes: 13 additions & 0 deletions
13
Zend/tests/readonly_classes/readonly_class_dynamic_property_attribute.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,13 @@ | ||
--TEST-- | ||
Readonly classes cannot apply the #[AllowDynamicProperties] attribute | ||
--FILE-- | ||
<?php | ||
|
||
#[AllowDynamicProperties] | ||
readonly class Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class Foo in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/readonly_classes/readonly_class_final_modifier.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-- | ||
The readonly and final class modifiers can be defined in the same time | ||
--FILE-- | ||
<?php | ||
|
||
final readonly class Foo | ||
{ | ||
} | ||
|
||
readonly class Bar extends Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Class Bar cannot extend final class Foo in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/readonly_classes/readonly_class_inheritance_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-- | ||
Non-readonly class cannot extend a readonly class | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
} | ||
|
||
class Bar extends Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Non-readonly class Bar cannot extend readonly class Foo in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/readonly_classes/readonly_class_inheritance_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-- | ||
Readonly class cannot extend a non-readonly class | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
} | ||
|
||
readonly class Bar extends Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Readonly class Bar cannot extend non-readonly class Foo in %s on line %d |
15 changes: 15 additions & 0 deletions
15
Zend/tests/readonly_classes/readonly_class_inheritance_success.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,15 @@ | ||
--TEST-- | ||
Readonly class can extend a readonly class | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
} | ||
|
||
readonly class Bar extends Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECT-- |
13 changes: 13 additions & 0 deletions
13
Zend/tests/readonly_classes/readonly_class_missing_type1.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,13 @@ | ||
--TEST-- | ||
Normal properties of a readonly class must have type | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
public $bar; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Readonly property Foo::$bar must have type in %s on line %d |
15 changes: 15 additions & 0 deletions
15
Zend/tests/readonly_classes/readonly_class_missing_type2.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,15 @@ | ||
--TEST-- | ||
Promoted properties of a readonly class must have type | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
public function __construct( | ||
private $bar | ||
) {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Readonly property Foo::$bar must have type in %s on line %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,25 @@ | ||
--TEST-- | ||
Normal properties of a readonly class are implicitly declared as readonly | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
public int $bar; | ||
|
||
public function __construct() { | ||
$this->bar = 1; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
try { | ||
$foo->bar = 2; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Promoted properties of a readonly class are implicitly declared as readonly | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
public function __construct( | ||
public int $bar | ||
) {} | ||
} | ||
|
||
$foo = new Foo(1); | ||
|
||
try { | ||
$foo->bar = 2; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
Declaring static property for a readonly class is forbidden | ||
--FILE-- | ||
<?php | ||
|
||
readonly class Foo | ||
{ | ||
public static int $bar; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Readonly class Foo cannot declare static properties in %s on line %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,12 @@ | ||
--TEST-- | ||
Enums cannot be readonly | ||
--FILE-- | ||
<?php | ||
|
||
readonly enum Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token "enum", expecting "abstract" or "final" or "readonly" or "class" in %s on line %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,12 @@ | ||
--TEST-- | ||
Interfaces cannot be readonly | ||
--FILE-- | ||
<?php | ||
|
||
readonly interface Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token "interface", expecting "abstract" or "final" or "readonly" or "class" in %s on line %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,12 @@ | ||
--TEST-- | ||
Traits cannot be readonly | ||
--FILE-- | ||
<?php | ||
|
||
readonly trait Foo | ||
{ | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token "trait", expecting "abstract" or "final" or "readonly" or "class" in %s on line %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
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
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.