-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[RFC] Allow #[\Override] on class constants
#20478
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
Open
DanielEScherzer
wants to merge
8
commits into
php:master
Choose a base branch
from
DanielEScherzer:override-constants
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f84ea8
[RFC] Allow `#[\Override]` on class constants
DanielEScherzer 4b52d75
Only on exact class
DanielEScherzer c78307a
Tests for enums
DanielEScherzer 9942f3e
Enum cases override interface constants
DanielEScherzer a0b0657
Clarify test names
DanielEScherzer 9af924d
Expand basic tests
DanielEScherzer a2cd023
Clarify comment
DanielEScherzer e083a2c
Another comment
DanielEScherzer 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
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/delayed_target_validation/with_Override_error_constant.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-- | ||
| #[\DelayedTargetValidation] with #[\Override]: non-overrides still error (class constant) | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class DemoClass { | ||
|
|
||
| #[DelayedTargetValidation] | ||
| #[Override] // Does something here | ||
| public const CLASS_CONSTANT = 'FOO'; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: DemoClass::CLASS_CONSTANT has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/delayed_target_validation/with_Override_error_enum_case.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-- | ||
| #[\DelayedTargetValidation] with #[\Override]: non-overrides still error (enum case) | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| enum DemoEnum { | ||
|
|
||
| #[DelayedTargetValidation] | ||
| #[Override] // Does something here | ||
| case MyCase; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: DemoEnum::MyCase has #[\Override] attribute, but no matching parent constant exists 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
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/override/constants/anon_failure.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-- | ||
| #[\Override]: Constants - anonymous class, no interface or parent class | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| new class () { | ||
| #[\Override] | ||
| public const C = 'C'; | ||
| }; | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: class@anonymous::C has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
19 changes: 19 additions & 0 deletions
19
Zend/tests/attributes/override/constants/anon_interface.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-- | ||
| #[\Override]: Constants - anonymous class overrides interface | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface IFace { | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| new class () implements IFace { | ||
| #[\Override] | ||
| public const I = 'Changed'; | ||
| }; | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| 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,19 @@ | ||
| --TEST-- | ||
| #[\Override]: Constants - anonymous class overrides parent class | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Base { | ||
| public const C = 'C'; | ||
| } | ||
|
|
||
| new class () extends Base { | ||
| #[\Override] | ||
| public const C = 'Changed'; | ||
| }; | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| 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,57 @@ | ||
| --TEST-- | ||
| #[\Override]: Constants - basic | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface I { | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| interface II extends I { | ||
| #[\Override] | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| class P { | ||
| public const C1 = 'C1'; | ||
| public const C2 = 'C2'; | ||
| public const C3 = 'C3'; | ||
| public const C4 = 'C4'; | ||
| } | ||
|
|
||
| class PP extends P { | ||
| #[\Override] | ||
| public const C1 = 'C1'; | ||
| public const C2 = 'C2'; | ||
| #[\Override] | ||
| public const C3 = 'C3'; | ||
| } | ||
|
|
||
| class C extends PP implements I { | ||
| #[\Override] | ||
| public const I = 'I'; | ||
| #[\Override] | ||
| public const C1 = 'C1'; | ||
| #[\Override] | ||
| public const C2 = 'C2'; | ||
| public const C3 = 'C3'; | ||
| #[\Override] | ||
| public const C4 = 'C4'; | ||
| public const C = 'C'; | ||
| } | ||
|
|
||
| enum E implements I { | ||
| #[\Override] | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| enum WithCase implements I { | ||
| #[\Override] | ||
| case I; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Done | ||
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/override/constants/enum_failure.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-- | ||
| #[\Override]: Constants - no interface for enum constant | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| enum Demo { | ||
| #[\Override] | ||
| public const C = 'C'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Demo::C has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/override/constants/enum_failure_case.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-- | ||
| #[\Override]: Constants - no interface for enum case | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| enum Demo { | ||
| #[\Override] | ||
| case C; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Demo::C has #[\Override] attribute, but no matching parent constant exists 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,15 @@ | ||
| --TEST-- | ||
| #[\Override]: Constants - no interface or parent class | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Demo { | ||
| #[\Override] | ||
| public const C = 'C'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Demo::C has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/override/constants/interface_failure.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-- | ||
| #[\Override]: Constants - no parent interface | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface IFace { | ||
| #[\Override] | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: IFace::I has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
19 changes: 19 additions & 0 deletions
19
Zend/tests/attributes/override/constants/trait_failure.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-- | ||
| #[\Override]: Constants - on a trait, no interface or parent class | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait DemoTrait { | ||
| #[\Override] | ||
| public const T = 'T'; | ||
| } | ||
|
|
||
| class UsesTrait { | ||
| use DemoTrait; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: UsesTrait::T has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
23 changes: 23 additions & 0 deletions
23
Zend/tests/attributes/override/constants/trait_interface.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-- | ||
| #[\Override]: Constants - on a trait, overrides interface | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait DemoTrait { | ||
| #[\Override] | ||
| public const C = 'Changed'; | ||
| } | ||
|
|
||
| interface IFace { | ||
| public const C = 'C'; | ||
| } | ||
|
|
||
| class UsesTrait implements IFace { | ||
| use DemoTrait; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Done |
23 changes: 23 additions & 0 deletions
23
Zend/tests/attributes/override/constants/trait_parent.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-- | ||
| #[\Override]: Constants - on a trait, overrides parent class | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait DemoTrait { | ||
| #[\Override] | ||
| public const C = 'Changed'; | ||
| } | ||
|
|
||
| class Base { | ||
| public const C = 'C'; | ||
| } | ||
|
|
||
| class UsesTrait extends Base { | ||
| use DemoTrait; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Done |
21 changes: 21 additions & 0 deletions
21
Zend/tests/attributes/override/constants/trait_redeclared.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-- | ||
| #[\Override]: Constants - trait constant redeclared, not overridden | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait DemoTrait { | ||
| public const T = 'T'; | ||
| } | ||
|
|
||
| class UsesTrait { | ||
| use DemoTrait; | ||
|
|
||
| #[\Override] | ||
| public const T = 'T'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: UsesTrait::T has #[\Override] attribute, but no matching parent constant exists in %s on line %d |
25 changes: 25 additions & 0 deletions
25
Zend/tests/attributes/override/constants/trait_redeclared_interface.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-- | ||
| #[\Override]: Constants - trait constant redeclared, overrides interface | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| interface IFace { | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| trait DemoTrait { | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| class UsesTrait implements IFace { | ||
| use DemoTrait; | ||
|
|
||
| #[\Override] | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Done |
25 changes: 25 additions & 0 deletions
25
Zend/tests/attributes/override/constants/trait_redeclared_parent.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-- | ||
| #[\Override]: Constants - trait constant redeclared, overrides parent class | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Base { | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| trait DemoTrait { | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| class UsesTrait extends Base { | ||
| use DemoTrait; | ||
|
|
||
| #[\Override] | ||
| public const I = 'I'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Done |
15 changes: 15 additions & 0 deletions
15
Zend/tests/attributes/override/constants/trait_unused.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-- | ||
| #[\Override]: Constants - on a trait, unused | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait Demo { | ||
| #[\Override] | ||
| public const T = 'T'; | ||
| } | ||
|
|
||
| echo "Done"; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Done |
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.
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.
C2 is not tested at all. The original
Zend/tests/attributes/override/001.phpttest specifically had 4 cases to test all situations: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.
I added the cases from the origin test file which I think now covers everything