-
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
4 changed files
with
247 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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 | ||
{ | ||
} | ||
} | ||
|
||
/* testEnumUsedAsClassName */ | ||
class Enum { | ||
/* testEnumUsedAsClassConstantName */ | ||
const ENUM = 'enum'; | ||
|
||
/* testEnumUsedAsMethodName */ | ||
public function enum() { | ||
// Do something. | ||
|
||
/* testEnumUsedAsPropertyName */ | ||
$this->enum = 'foo'; | ||
} | ||
} | ||
|
||
/* testEnumUsedAsFunctionName */ | ||
function 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,147 @@ | ||
<?php | ||
/** | ||
* Tests the support of PHP 8.1 enums | ||
* | ||
* @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; | ||
use function array_key_exists; | ||
use const T_CLOSE_PARENTHESIS; | ||
use const T_OPEN_PARENTHESIS; | ||
use const T_STRING; | ||
|
||
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, | ||
], | ||
]; | ||
|
||
}//end dataEnums() | ||
|
||
|
||
/** | ||
* Test that 'enum' when not used as the enum 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 [ | ||
['/* testEnumUsedAsClassName */'], | ||
['/* testEnumUsedAsClassConstantName */'], | ||
['/* testEnumUsedAsMethodName */'], | ||
['/* testEnumUsedAsPropertyName */'], | ||
['/* testEnumUsedAsFunctionName */'], | ||
]; | ||
|
||
}//end dataNotEnums() | ||
|
||
|
||
}//end class |