-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement enums #6489
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
Implement enums #6489
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
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,29 @@ | ||
--TEST-- | ||
Enum __call | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function __call(string $name, array $args) | ||
{ | ||
return [$name, $args]; | ||
} | ||
} | ||
|
||
var_dump(Foo::Bar->baz('qux', 'quux')); | ||
|
||
?> | ||
--EXPECT-- | ||
array(2) { | ||
[0]=> | ||
string(3) "baz" | ||
[1]=> | ||
array(2) { | ||
[0]=> | ||
string(3) "qux" | ||
[1]=> | ||
string(4) "quux" | ||
} | ||
} |
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,27 @@ | ||
--TEST-- | ||
Enum __callStatic | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
public static function __callStatic(string $name, array $args) | ||
{ | ||
return [$name, $args]; | ||
} | ||
} | ||
|
||
var_dump(Foo::bar('baz', 'qux')); | ||
|
||
?> | ||
--EXPECT-- | ||
array(2) { | ||
[0]=> | ||
string(3) "bar" | ||
[1]=> | ||
array(2) { | ||
[0]=> | ||
string(3) "baz" | ||
[1]=> | ||
string(3) "qux" | ||
} | ||
} |
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-- | ||
Enum __CLASS__ | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function printClass() | ||
{ | ||
echo __CLASS__ . "\n"; | ||
} | ||
} | ||
|
||
Foo::Bar->printClass(); | ||
|
||
?> | ||
--EXPECT-- | ||
Foo |
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-- | ||
Enum __FUNCTION__ | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function printFunction() | ||
{ | ||
echo __FUNCTION__ . "\n"; | ||
} | ||
} | ||
|
||
Foo::Bar->printFunction(); | ||
|
||
?> | ||
--EXPECT-- | ||
printFunction |
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-- | ||
Enum __get | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function __get(string $name) | ||
{ | ||
return '__get'; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Enum may not include __get 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,24 @@ | ||
--TEST-- | ||
Enum __invoke | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function __invoke(...$args) | ||
{ | ||
return $args; | ||
} | ||
} | ||
|
||
var_dump((Foo::Bar)('baz', 'qux')); | ||
|
||
?> | ||
--EXPECT-- | ||
array(2) { | ||
[0]=> | ||
string(3) "baz" | ||
[1]=> | ||
string(3) "qux" | ||
} |
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-- | ||
Enum __isset | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function __isset($property) { | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Enum may not include __isset 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,19 @@ | ||
--TEST-- | ||
Enum __METHOD__ | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
|
||
public function printMethod() | ||
{ | ||
echo __METHOD__ . "\n"; | ||
} | ||
} | ||
|
||
Foo::Bar->printMethod(); | ||
|
||
?> | ||
--EXPECT-- | ||
Foo::printMethod |
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,48 @@ | ||
--TEST-- | ||
Enum AST dumper | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
assert((function () { | ||
enum Foo { | ||
case Bar; | ||
} | ||
|
||
#[EnumAttr] | ||
enum IntFoo: int { | ||
#[CaseAttr] | ||
case Bar = 1 << 0; | ||
case Baz = 1 << 1; | ||
|
||
public function self() { | ||
return $this; | ||
} | ||
} | ||
|
||
return false; | ||
})()); | ||
} catch (Error $e) { | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
assert(function () { | ||
enum Foo { | ||
case Bar; | ||
} | ||
|
||
#[EnumAttr] | ||
enum IntFoo: int { | ||
#[CaseAttr] | ||
case Bar = 1 << 0; | ||
case Baz = 1 << 1; | ||
public function self() { | ||
return $this; | ||
} | ||
|
||
} | ||
|
||
return false; | ||
}()) |
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,26 @@ | ||
--TEST-- | ||
Int backed enums with can list cases | ||
--FILE-- | ||
<?php | ||
|
||
enum Suit: int { | ||
case Hearts = 2; | ||
case Diamonds = 1; | ||
case Clubs = 4; | ||
case Spades = 3; | ||
} | ||
|
||
var_dump(Suit::cases()); | ||
|
||
?> | ||
--EXPECT-- | ||
array(4) { | ||
[0]=> | ||
enum(Suit::Hearts) | ||
[1]=> | ||
enum(Suit::Diamonds) | ||
[2]=> | ||
enum(Suit::Clubs) | ||
[3]=> | ||
enum(Suit::Spades) | ||
} |
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,26 @@ | ||
--TEST-- | ||
String backed enums can list cases | ||
--FILE-- | ||
<?php | ||
|
||
enum Suit: string { | ||
case Hearts = 'H'; | ||
case Diamonds = 'D'; | ||
case Clubs = 'C'; | ||
case Spades = 'S'; | ||
} | ||
|
||
var_dump(Suit::cases()); | ||
|
||
?> | ||
--EXPECT-- | ||
array(4) { | ||
[0]=> | ||
enum(Suit::Hearts) | ||
[1]=> | ||
enum(Suit::Diamonds) | ||
[2]=> | ||
enum(Suit::Clubs) | ||
[3]=> | ||
enum(Suit::Spades) | ||
} |
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-- | ||
Backed enums reject duplicate int values | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo: int { | ||
case Bar = 0; | ||
case Baz = 0; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate value in enum Foo for cases Bar and Baz in %s on line %s |
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-- | ||
Backed enums reject duplicate string values | ||
--FILE-- | ||
<?php | ||
|
||
enum Suit: string { | ||
case Hearts = 'H'; | ||
case Diamonds = 'D'; | ||
case Clubs = 'C'; | ||
case Spades = 'H'; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate value in enum Suit for cases Hearts and Spades in %s on line %s |
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-- | ||
BackedEnum::from() reject invalid int | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo: int { | ||
case Bar = 0; | ||
case Baz = 1; | ||
} | ||
|
||
try { | ||
var_dump(Foo::from(2)); | ||
} catch (Error $e) { | ||
echo $e->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
2 is not a valid backing value for enum "Foo" |
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-- | ||
BackedEnum::from() reject invalid string | ||
--FILE-- | ||
<?php | ||
|
||
enum Suit: string { | ||
case Hearts = 'H'; | ||
case Diamonds = 'D'; | ||
case Clubs = 'C'; | ||
case Spades = 'S'; | ||
} | ||
|
||
try { | ||
var_dump(Suit::from('A')); | ||
} catch (Error $e) { | ||
echo $e->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
"A" is not a valid backing value for enum "Suit" |
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.