-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Typed class constants #5815
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 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9e3271b
feat: typed class constants
moliata a63d0aa
style: reindent tests
moliata 837d5e9
style: use ? instead of |null
moliata b253197
fix: make class constants covariant
moliata 27bae60
style: remove vim foldings
moliata 8a08ef7
style: add vim folding
moliata 7043c0f
fix: make object type not allowed
moliata d282214
test: add reflection tests
moliata 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/type_declarations/typed_class_constants_001.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-- | ||
Typed class constants (declaration; simple) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const int A = 1; | ||
} | ||
|
||
echo A::A; | ||
?> | ||
--EXPECT-- | ||
1 |
10 changes: 10 additions & 0 deletions
10
Zend/tests/type_declarations/typed_class_constants_002.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,10 @@ | ||
--TEST-- | ||
Typed class constants (type mismatch; compile-time) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const string A = 1; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use int as value for class constant A::A of type string in %s on line %d | ||
moliata marked this conversation as resolved.
Show resolved
Hide resolved
|
17 changes: 17 additions & 0 deletions
17
Zend/tests/type_declarations/typed_class_constants_003.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-- | ||
Typed class constants (type mismatch; runtime) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const string A = A; | ||
} | ||
|
||
define('A', 1); | ||
|
||
echo A::A; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught TypeError: Cannot assign int to class constant A::A of type string in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/type_declarations/typed_class_constants_004.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,14 @@ | ||
--TEST-- | ||
Typed class constants (inheritance; simple) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const int A = 1; | ||
} | ||
|
||
class B extends A { | ||
public const string A = 'a'; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of B::A must be compatible with A::A in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/type_declarations/typed_class_constants_005.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,14 @@ | ||
--TEST-- | ||
Typed class constants (inheritance; missing type in child) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const int A = 1; | ||
} | ||
|
||
class B extends A { | ||
public const A = 0; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of B::A must be compatible with A::A in %s on line %d |
10 changes: 10 additions & 0 deletions
10
Zend/tests/type_declarations/typed_class_constants_007.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,10 @@ | ||
--TEST-- | ||
Typed class constants (type not allowed; void) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const void A = 1; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Class constant A::A cannot have type void in %s on line %d |
10 changes: 10 additions & 0 deletions
10
Zend/tests/type_declarations/typed_class_constants_008.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,10 @@ | ||
--TEST-- | ||
Typed class constants (type not allowed; callable) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const callable A = 1; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Class constant A::A cannot have type callable in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/type_declarations/typed_class_constants_009.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-- | ||
Typed class constants (inheritance; private constants) | ||
--FILE-- | ||
<?php | ||
class A { | ||
private const int A = 1; | ||
} | ||
|
||
class B extends A { | ||
public const string A = 'a'; | ||
} | ||
|
||
echo B::A; | ||
?> | ||
--EXPECT-- | ||
a |
10 changes: 10 additions & 0 deletions
10
Zend/tests/type_declarations/typed_class_constants_010.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,10 @@ | ||
--TEST-- | ||
Typed class constants (type not allowed; class name) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const self A = 1; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Class constant A::A cannot have type self in %s on line %d |
12 changes: 12 additions & 0 deletions
12
Zend/tests/type_declarations/typed_class_constants_011.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-- | ||
Typed class constants (declaration; nullable) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const ?int A = null; | ||
} | ||
|
||
var_dump(A::A); | ||
?> | ||
--EXPECT-- | ||
NULL |
15 changes: 15 additions & 0 deletions
15
Zend/tests/type_declarations/typed_class_constants_012.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-- | ||
Typed class constants (declaration; iterable) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const iterable A = [1]; | ||
} | ||
|
||
var_dump(A::A); | ||
?> | ||
--EXPECT-- | ||
array(1) { | ||
[0]=> | ||
int(1) | ||
} |
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
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.