-
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 "readonly" keyword
- Loading branch information
Showing
5 changed files
with
208 additions
and
1 deletion.
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,53 @@ | ||
<?php | ||
|
||
class Foo | ||
{ | ||
/* testReadonlyProperty */ | ||
readonly int $readonlyProperty; | ||
/* testPublicReadonlyProperty */ | ||
public readonly int $publicReadonlyProperty; | ||
/* testProtectedReadonlyProperty */ | ||
protected readonly int $protectedReadonlyProperty; | ||
/* testPrivateReadonlyProperty */ | ||
private readonly int $privateReadonlyProperty; | ||
/* testPublicReadonlyPropertyWithReadonlyFirst */ | ||
readonly public int $publicReadonlyProperty; | ||
/* testProtectedReadonlyPropertyWithReadonlyFirst */ | ||
readonly protected int $protectedReadonlyProperty; | ||
/* testPrivateReadonlyPropertyWithReadonlyFirst */ | ||
readonly private int $privateReadonlyProperty; | ||
/* testReadonlyWithCommentsInDeclaration */ | ||
private /* Comment */ readonly /* Comment */ int /* Comment */ $readonlyPropertyWithCommentsInDeclaration; | ||
|
||
} | ||
|
||
class ClassName { | ||
/* testReadonlyUsedAsClassConstantName */ | ||
const READONLY = 'readonly'; | ||
|
||
/* testReadonlyUsedAsMethodName */ | ||
public function readonly() { | ||
// Do something. | ||
|
||
/* testReadonlyUsedAsPropertyName */ | ||
$this->readonly = 'foo'; | ||
} | ||
} | ||
|
||
/* testReadonlyUsedAsFunctionName */ | ||
function readonly() | ||
{ | ||
} | ||
|
||
/* testReadonlyUsedAsNamespaceName */ | ||
namespace Readonly; | ||
/* testReadonlyUsedAsPartOfNamespaceName */ | ||
namespace My\Readonly\Collection; | ||
/* testReadonlyAsFunctionCall */ | ||
$var = readonly($a, $b); | ||
/* testClassConstantFetchWithReadonlyAsConstantName */ | ||
echo ClassName::READONLY; | ||
|
||
/* testParseErrorLiveCoding */ | ||
// This must be the last test in the file. | ||
readonly |
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,107 @@ | ||
<?php | ||
/** | ||
* Tests the support of PHP 8.1 "readonly" 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 ReadonlyTest extends AbstractMethodUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Test that the "readonly" keyword is tokenized as such. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* | ||
* @dataProvider dataReadonly | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testReadonly($testMarker) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$target = $this->getTargetToken($testMarker, [T_READONLY, T_STRING]); | ||
$this->assertSame(T_READONLY, $tokens[$target]['code']); | ||
$this->assertSame('T_READONLY', $tokens[$target]['type']); | ||
|
||
}//end testReadonly() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testReadonly() | ||
* | ||
* @return array | ||
*/ | ||
public function dataReadonly() | ||
{ | ||
return [ | ||
['/* testReadonlyProperty */'], | ||
['/* testPublicReadonlyProperty */'], | ||
['/* testProtectedReadonlyProperty */'], | ||
['/* testPrivateReadonlyProperty */'], | ||
['/* testPublicReadonlyPropertyWithReadonlyFirst */'], | ||
['/* testProtectedReadonlyPropertyWithReadonlyFirst */'], | ||
['/* testPrivateReadonlyPropertyWithReadonlyFirst */'], | ||
['/* testReadonlyWithCommentsInDeclaration */'], | ||
['/* testParseErrorLiveCoding */'], | ||
]; | ||
|
||
}//end dataReadonly() | ||
|
||
|
||
/** | ||
* Test that "readonly" 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 dataNotReadonly | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testNotReadonly($testMarker) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$target = $this->getTargetToken($testMarker, [T_READONLY, T_STRING]); | ||
$this->assertSame(T_STRING, $tokens[$target]['code']); | ||
$this->assertSame('T_STRING', $tokens[$target]['type']); | ||
|
||
}//end testNotReadonly() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testNotReadonly() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNotReadonly() | ||
{ | ||
return [ | ||
['/* testReadonlyUsedAsClassConstantName */'], | ||
['/* testReadonlyUsedAsMethodName */'], | ||
['/* testReadonlyUsedAsPropertyName */'], | ||
['/* testReadonlyUsedAsFunctionName */'], | ||
['/* testReadonlyUsedAsNamespaceName */'], | ||
['/* testReadonlyUsedAsPartOfNamespaceName */'], | ||
['/* testReadonlyAsFunctionCall */'], | ||
['/* testClassConstantFetchWithReadonlyAsConstantName */'], | ||
]; | ||
|
||
}//end dataNotReadonly() | ||
|
||
|
||
}//end class |