-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[RFC] Allow Readonly Property Defaults #22588
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
8496c6b
Prepared unserializer for readonly property defaults
NickSdot f19b8bc
Allowed defaults on readonly properties
NickSdot 66e6639
Updated NEWS and UPGRADING
NickSdot fd99ad8
review: reduced serialize lock scope
NickSdot aa70661
review: improved tests
NickSdot cdc1c38
review: improved tests
NickSdot 8eed191
review: improved tests
NickSdot 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
36 changes: 36 additions & 0 deletions
36
Zend/tests/readonly_classes/readonly_with_property_default.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,36 @@ | ||
| --TEST-- | ||
| Properties of a readonly class may have default values | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| readonly class Foo | ||
| { | ||
| public int $bar = 1; | ||
| public ?string $nullable = null; | ||
|
|
||
| public function __construct() | ||
| { | ||
| try { | ||
| $this->bar = 2; | ||
| } catch (Error $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $foo = new Foo(); | ||
| var_dump($foo->bar); | ||
| var_dump($foo->nullable); | ||
|
|
||
| try { | ||
| $foo->bar = 3; | ||
| } catch (Error $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Error: Cannot modify readonly property Foo::$bar | ||
| int(1) | ||
| NULL | ||
| Error: Cannot modify readonly property Foo::$bar |
25 changes: 25 additions & 0 deletions
25
Zend/tests/readonly_classes/readonly_with_property_default_trait.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,25 @@ | ||
| --TEST-- | ||
| Readonly class may use readonly trait property with default value | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait TDefault { | ||
| public readonly int $prop = 2; | ||
| } | ||
|
|
||
| readonly class A { | ||
| use TDefault; | ||
| } | ||
|
|
||
| var_dump(new A()->prop); | ||
|
|
||
| class B { | ||
| use TDefault; | ||
| } | ||
|
|
||
| var_dump(new B()->prop); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| int(2) | ||
| int(2) |
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
18 changes: 18 additions & 0 deletions
18
Zend/tests/readonly_props/readonly_with_default_abstract_get_set_implicit_set.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-- | ||
| Readonly property with default value has restricted set visibility for get/set abstract property | ||
| --DESCRIPTION-- | ||
| The error message should be improved, the set access level comes from readonly. | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| abstract class P { | ||
| public abstract int $prop { get; set; } | ||
| } | ||
|
|
||
| class C extends P { | ||
| public readonly int $prop = 42; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Set access level of C::$prop must be omitted (as in class P) in %s on line %d | ||
34 changes: 34 additions & 0 deletions
34
Zend/tests/readonly_props/readonly_with_default_asymmetric_visibility.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,34 @@ | ||
| --TEST-- | ||
| Readonly property with default value and asymmetric visibility | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Test { | ||
| public readonly int $default = 1; | ||
| public private(set) readonly int $private = 2; | ||
| public protected(set) readonly int $protected = 3; | ||
| public public(set) readonly int $public = 4; | ||
| } | ||
|
|
||
| $test = new Test(); | ||
|
|
||
| foreach (['default', 'private', 'protected', 'public'] as $prop) { | ||
| $before = $test->$prop; | ||
| try { | ||
| $test->$prop = 42; | ||
| } catch (Error $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
| echo "$$prop before $before, after {$test->$prop}", PHP_EOL; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Error: Cannot modify readonly property Test::$default | ||
| $default before 1, after 1 | ||
| Error: Cannot modify readonly property Test::$private | ||
| $private before 2, after 2 | ||
| Error: Cannot modify readonly property Test::$protected | ||
| $protected before 3, after 3 | ||
| Error: Cannot modify readonly property Test::$public | ||
| $public before 4, after 4 |
40 changes: 40 additions & 0 deletions
40
Zend/tests/readonly_props/readonly_with_default_inheritance.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,40 @@ | ||
| --TEST-- | ||
| Readonly property with default value and inheritance | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class ParentDefault { | ||
| public readonly int $prop = 1; | ||
| } | ||
|
|
||
| class ChildInherits extends ParentDefault {} | ||
|
|
||
| class ChildOverrides extends ParentDefault { | ||
| public readonly int $prop = 2; | ||
| } | ||
|
|
||
| class PrivateParent { | ||
| private readonly int $prop = 3; | ||
|
|
||
| public function getParentProp(): int { | ||
| return $this->prop; | ||
| } | ||
| } | ||
|
|
||
| class PrivateChild extends PrivateParent { | ||
| public readonly int $prop = 4; | ||
| } | ||
|
|
||
| var_dump(new ChildInherits()->prop); | ||
| var_dump(new ChildOverrides()->prop); | ||
|
|
||
| $privateChild = new PrivateChild(); | ||
| var_dump($privateChild->getParentProp()); | ||
| var_dump($privateChild->prop); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| int(1) | ||
| int(2) | ||
| int(3) | ||
| int(4) |
17 changes: 17 additions & 0 deletions
17
Zend/tests/readonly_props/readonly_with_default_interface_get_only.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,17 @@ | ||
| --TEST-- | ||
| Readonly property with default value satisfies get-only interface property | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface I { | ||
| public int $prop { get; } | ||
| } | ||
|
|
||
| class C implements I { | ||
| public readonly int $prop = 42; | ||
| } | ||
|
|
||
| var_dump(new C()->prop); | ||
| ?> | ||
| --EXPECT-- | ||
| int(42) |
16 changes: 16 additions & 0 deletions
16
Zend/tests/readonly_props/readonly_with_default_interface_get_set.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 public(set) property with default value does not satisfy get/set interface property | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface I { | ||
| public int $prop { get; set; } | ||
| } | ||
|
|
||
| // does not satisfy set | ||
| class C implements I { | ||
| public public(set) readonly int $prop = 42; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Class C contains 1 abstract method and must therefore be declared abstract or implement the remaining method (I::$prop::set) in %s on line %d |
17 changes: 17 additions & 0 deletions
17
Zend/tests/readonly_props/readonly_with_default_interface_get_set_implicit_set.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,17 @@ | ||
| --TEST-- | ||
| Readonly property with default value has restricted set visibility for get/set interface property | ||
| --DESCRIPTION-- | ||
| The error message should be improved, the set access level comes from readonly. Ref: Zend/tests/property_hooks/interface_get_set_readonly.phpt | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface I { | ||
| public int $prop { get; set; } | ||
| } | ||
|
|
||
| class C implements I { | ||
| public readonly int $prop = 42; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Set access level of C::$prop must be omitted (as in class I) in %s on line %d | ||
|
NickSdot marked this conversation as resolved.
|
||
20 changes: 20 additions & 0 deletions
20
Zend/tests/readonly_props/readonly_with_default_trait_mismatch.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 trait property default value mismatch | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait T1 { | ||
| public readonly int $prop = 1; | ||
| } | ||
|
|
||
| trait T2 { | ||
| public readonly int $prop = 2; | ||
| } | ||
|
|
||
| class C { | ||
| use T1, T2; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: T1 and T2 define the same property ($prop) in the composition of C. However, the definition differs and is considered incompatible. Class was composed in %s on line %d |
Oops, something went wrong.
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.