-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHP 8.1: Added support for "enum" keyword
- Loading branch information
Showing
5 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,88 @@ | ||
<?php | ||
|
||
/* testPureEnum */ | ||
enum Foo | ||
{ | ||
case SOME_CASE; | ||
} | ||
|
||
/* testBackedIntEnum */ | ||
enum Boo: int { | ||
case ONE = 1; | ||
case TWO = 1; | ||
} | ||
|
||
/* testBackedStringEnum */ | ||
enum Hoo: string | ||
{ | ||
case ONE = 'one'; | ||
case TWO = 'two'; | ||
} | ||
|
||
/* testComplexEnum */ | ||
enum ComplexEnum: int implements SomeInterface | ||
{ | ||
use SomeTrait { | ||
traitMethod as enumMethod; | ||
} | ||
|
||
const SOME_CONSTANT = true; | ||
|
||
case ONE = 1; | ||
|
||
public function someMethod(): bool | ||
{ | ||
} | ||
} | ||
|
||
/* testEnumWithEnumAsClassName */ | ||
enum /* testEnumAsClassNameAfterEnumKeyword */ Enum {} | ||
|
||
/* testEnumUsedAsClassName */ | ||
class Enum { | ||
/* testEnumUsedAsClassConstantName */ | ||
const ENUM = 'enum'; | ||
|
||
/* testEnumUsedAsMethodName */ | ||
public function enum() { | ||
// Do something. | ||
|
||
/* testEnumUsedAsPropertyName */ | ||
$this->enum = 'foo'; | ||
} | ||
} | ||
|
||
/* testEnumUsedAsFunctionName */ | ||
function enum() | ||
{ | ||
} | ||
|
||
/* testDeclarationContainingComment */ | ||
enum /* comment */ Name | ||
{ | ||
case SOME_CASE; | ||
} | ||
|
||
/* testEnumUsedAsNamespaceName */ | ||
namespace Enum; | ||
/* testEnumUsedAsPartOfNamespaceName */ | ||
namespace My\Enum\Collection; | ||
/* testEnumUsedInObjectInitialization */ | ||
$obj = new Enum; | ||
/* testEnumAsFunctionCall */ | ||
$var = enum($a, $b); | ||
/* testEnumAsFunctionCallWithNamespace */ | ||
var = namespace\enum(); | ||
/* testClassConstantFetchWithEnumAsClassName */ | ||
echo Enum::CONSTANT; | ||
/* testClassConstantFetchWithEnumAsConstantName */ | ||
echo ClassName::ENUM; | ||
|
||
/* testParseErrorMissingName */ | ||
enum { | ||
case SOME_CASE; | ||
} | ||
|
||
/* testParseErrorLiveCoding */ | ||
// This must be the last test in the file. | ||
enum |
This file contains 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,163 @@ | ||
<?php | ||
/** | ||
* Tests the support of PHP 8.1 "enum" keyword. | ||
* | ||
* @author Jaroslav Hanslík <kukulich@kukulich.cz> | ||
* @copyright 2021 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer; | ||
|
||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; | ||
|
||
class EnumTest extends AbstractMethodUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Test that the "enum" keyword is tokenized as such. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param int $expectedScopeOpenerLine Expected line of scope opener. | ||
* @param int $expectedScopeCloserLine Expected line of scope closer. | ||
* | ||
* @dataProvider dataEnums | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testEnums($testMarker, $expectedScopeOpenerLine, $expectedScopeCloserLine) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$enum = $this->getTargetToken($testMarker, [T_ENUM, T_STRING]); | ||
|
||
$this->assertSame(T_ENUM, $tokens[$enum]['code']); | ||
$this->assertSame('T_ENUM', $tokens[$enum]['type']); | ||
|
||
$this->assertArrayHasKey('scope_condition', $tokens[$enum]); | ||
$this->assertArrayHasKey('scope_opener', $tokens[$enum]); | ||
$this->assertArrayHasKey('scope_closer', $tokens[$enum]); | ||
|
||
$this->assertSame($enum, $tokens[$enum]['scope_condition']); | ||
|
||
$scopeOpener = $tokens[$enum]['scope_opener']; | ||
$scopeCloser = $tokens[$enum]['scope_closer']; | ||
|
||
$this->assertSame($expectedScopeOpenerLine, $tokens[$scopeOpener]['line']); | ||
$this->assertArrayHasKey('scope_condition', $tokens[$scopeOpener]); | ||
$this->assertArrayHasKey('scope_opener', $tokens[$scopeOpener]); | ||
$this->assertArrayHasKey('scope_closer', $tokens[$scopeOpener]); | ||
$this->assertSame($enum, $tokens[$scopeOpener]['scope_condition']); | ||
$this->assertSame($scopeOpener, $tokens[$scopeOpener]['scope_opener']); | ||
$this->assertSame($scopeCloser, $tokens[$scopeOpener]['scope_closer']); | ||
|
||
$this->assertSame($expectedScopeCloserLine, $tokens[$scopeCloser]['line']); | ||
$this->assertArrayHasKey('scope_condition', $tokens[$scopeCloser]); | ||
$this->assertArrayHasKey('scope_opener', $tokens[$scopeCloser]); | ||
$this->assertArrayHasKey('scope_closer', $tokens[$scopeCloser]); | ||
$this->assertSame($enum, $tokens[$scopeCloser]['scope_condition']); | ||
$this->assertSame($scopeOpener, $tokens[$scopeCloser]['scope_opener']); | ||
$this->assertSame($scopeCloser, $tokens[$scopeCloser]['scope_closer']); | ||
|
||
}//end testEnums() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testEnums() | ||
* | ||
* @return array | ||
*/ | ||
public function dataEnums() | ||
{ | ||
return [ | ||
[ | ||
'/* testPureEnum */', | ||
5, | ||
7, | ||
], | ||
[ | ||
'/* testBackedIntEnum */', | ||
10, | ||
13, | ||
], | ||
[ | ||
'/* testBackedStringEnum */', | ||
17, | ||
20, | ||
], | ||
[ | ||
'/* testComplexEnum */', | ||
24, | ||
36, | ||
], | ||
[ | ||
'/* testEnumWithEnumAsClassName */', | ||
39, | ||
39, | ||
], | ||
[ | ||
'/* testDeclarationContainingComment */', | ||
62, | ||
64, | ||
], | ||
]; | ||
|
||
}//end dataEnums() | ||
|
||
|
||
/** | ||
* Test that "enum" when not used as the keyword is still tokenized as `T_STRING`. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* | ||
* @dataProvider dataNotEnums | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testNotEnums($testMarker) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$target = $this->getTargetToken($testMarker, [T_ENUM, T_STRING]); | ||
$this->assertSame(T_STRING, $tokens[$target]['code']); | ||
$this->assertSame('T_STRING', $tokens[$target]['type']); | ||
|
||
}//end testNotEnums() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testNotEnums() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNotEnums() | ||
{ | ||
return [ | ||
['/* testEnumAsClassNameAfterEnumKeyword */'], | ||
['/* testEnumUsedAsClassName */'], | ||
['/* testEnumUsedAsClassConstantName */'], | ||
['/* testEnumUsedAsMethodName */'], | ||
['/* testEnumUsedAsPropertyName */'], | ||
['/* testEnumUsedAsFunctionName */'], | ||
['/* testEnumUsedAsNamespaceName */'], | ||
['/* testEnumUsedAsPartOfNamespaceName */'], | ||
['/* testEnumUsedInObjectInitialization */'], | ||
['/* testEnumAsFunctionCall */'], | ||
['/* testEnumAsFunctionCallWithNamespace */'], | ||
['/* testClassConstantFetchWithEnumAsClassName */'], | ||
['/* testClassConstantFetchWithEnumAsConstantName */'], | ||
['/* testParseErrorMissingName */'], | ||
['/* testParseErrorLiveCoding */'], | ||
]; | ||
|
||
}//end dataNotEnums() | ||
|
||
|
||
}//end class |