Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1909,15 +1909,7 @@ public function getMemberProperties(int $stackPtr)
}
}

$valid = [
T_STATIC => T_STATIC,
T_VAR => T_VAR,
T_READONLY => T_READONLY,
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
];

$valid += Tokens::SCOPE_MODIFIERS;
$valid = Tokens::PROPERTY_MODIFIERS;
$valid += Tokens::EMPTY_TOKENS;

$scope = 'public';
Expand Down Expand Up @@ -2068,13 +2060,12 @@ public function getClassProperties(int $stackPtr)
}

$valid = [
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
];

$valid += Tokens::CLASS_MODIFIERS;

$isAbstract = false;
$isFinal = false;
$isReadonly = false;
Expand Down
17 changes: 3 additions & 14 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ public function register()
{
$targets = self::TARGET_TOKENS;

// Register scope modifiers to filter out property type declarations.
$targets += Tokens::SCOPE_MODIFIERS;
$targets[] = T_VAR;
$targets[] = T_STATIC;
$targets[] = T_READONLY;
$targets[] = T_FINAL;
$targets[] = T_ABSTRACT;
// Register property modifiers to filter out property type declarations.
$targets += Tokens::PROPERTY_MODIFIERS;

// Register function keywords to filter out param/return type declarations.
$targets[] = T_FUNCTION;
Expand Down Expand Up @@ -119,13 +114,7 @@ public function process(File $phpcsFile, int $stackPtr)
* declarations, in which case, it is correct to skip over them.
*/

if (isset(Tokens::SCOPE_MODIFIERS[$tokens[$stackPtr]['code']]) === true
|| $tokens[$stackPtr]['code'] === T_VAR
|| $tokens[$stackPtr]['code'] === T_STATIC
|| $tokens[$stackPtr]['code'] === T_READONLY
|| $tokens[$stackPtr]['code'] === T_FINAL
|| $tokens[$stackPtr]['code'] === T_ABSTRACT
) {
if (isset(Tokens::PROPERTY_MODIFIERS[$tokens[$stackPtr]['code']]) === true) {
$skipOver = (Tokens::EMPTY_TOKENS + self::PROPERTY_TYPE_TOKENS);
$skipTo = $phpcsFile->findNext($skipOver, ($stackPtr + 1), null, true);
if ($skipTo !== false) {
Expand Down
9 changes: 3 additions & 6 deletions src/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class ClassCommentSniff extends FileCommentSniff
{
Expand Down Expand Up @@ -47,12 +48,8 @@ public function process(File $phpcsFile, int $stackPtr)
$type = strtolower($tokens[$stackPtr]['content']);
$errorData = [$type];

$find = [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
];
$find = [T_WHITESPACE => T_WHITESPACE];
$find += Tokens::CLASS_MODIFIERS;

for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
if (isset($find[$tokens[$commentEnd]['code']]) === true) {
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function searchForConflict(File $phpcsFile, int $start, int $end, array

// Ignore function/class prefixes.
if (isset(Tokens::METHOD_MODIFIERS[$tokens[$i]['code']]) === true
|| $tokens[$i]['code'] === T_READONLY
|| isset(Tokens::CLASS_MODIFIERS[$tokens[$i]['code']]) === true
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function getHeaderLines(File $phpcsFile, int $stackPtr)

if (isset($commentOpeners[$tokens[$docToken]['code']]) === false
&& isset(Tokens::METHOD_MODIFIERS[$tokens[$docToken]['code']]) === false
&& $tokens[$docToken]['code'] !== T_READONLY
&& isset(Tokens::CLASS_MODIFIERS[$tokens[$docToken]['code']]) === false
) {
// Check for an @var annotation.
$annotation = false;
Expand Down
13 changes: 1 addition & 12 deletions src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@
class ClassDeclarationSniff extends PEARClassDeclarationSniff
{

/**
* Modifier keywords which can be used in class declarations.
*
* @var array<int|string, int|string>
*/
private const CLASS_MODIFIERS = [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
];

/**
* The number of spaces code should be indented.
*
Expand Down Expand Up @@ -79,7 +68,7 @@ public function processOpen(File $phpcsFile, int $stackPtr)
$prevNonSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);

if (isset(self::CLASS_MODIFIERS[$tokens[$prevNonEmpty]['code']]) === true) {
if (isset(Tokens::CLASS_MODIFIERS[$tokens[$prevNonEmpty]['code']]) === true) {
$spaces = 0;
$errorCode = 'SpaceBeforeKeyword';
if ($tokens[$prevNonEmpty]['line'] !== $tokens[$stackPtr]['line']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ protected function processMemberVar(File $phpcsFile, int $stackPtr)
// Detect multiple properties defined at the same time. Throw an error
// for this, but also only process the first property in the list so we don't
// repeat errors.
$find = Tokens::SCOPE_MODIFIERS;
$find = Tokens::PROPERTY_MODIFIERS;
$find[] = T_VARIABLE;
$find[] = T_VAR;
$find[] = T_READONLY;
$find[] = T_FINAL;
$find[] = T_ABSTRACT;
$find[] = T_SEMICOLON;
$find[] = T_OPEN_CURLY_BRACKET;

Expand Down
5 changes: 1 addition & 4 deletions src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public function processOpen(File $phpcsFile, int $stackPtr)
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
$spaces = strlen($blankSpace);

if ($tokens[($stackPtr - 2)]['code'] !== T_ABSTRACT
&& $tokens[($stackPtr - 2)]['code'] !== T_FINAL
&& $tokens[($stackPtr - 2)]['code'] !== T_READONLY
) {
if (isset(Tokens::CLASS_MODIFIERS[$tokens[($stackPtr - 2)]['code']]) === false) {
if ($spaces !== 0) {
$type = strtolower($tokens[$stackPtr]['content']);
$error = 'Expected 0 spaces before %s keyword; %s found';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ class LowercaseClassKeywordsSniff implements Sniff
public function register()
{
$targets = Tokens::OO_SCOPE_TOKENS;
$targets += Tokens::CLASS_MODIFIERS;
$targets[] = T_EXTENDS;
$targets[] = T_IMPLEMENTS;
$targets[] = T_ABSTRACT;
$targets[] = T_FINAL;
$targets[] = T_READONLY;
$targets[] = T_VAR;
$targets[] = T_CONST;

Expand Down
10 changes: 4 additions & 6 deletions src/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class ClassCommentSniff implements Sniff
{
Expand Down Expand Up @@ -48,12 +49,9 @@ public function register()
public function process(File $phpcsFile, int $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$find = [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
];

$find = [T_WHITESPACE => T_WHITESPACE];
$find += Tokens::CLASS_MODIFIERS;

$previousContent = null;
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ protected function processMemberVar(File $phpcsFile, int $stackPtr)

$endOfPreviousStatement = $phpcsFile->findPrevious($stopPoints, ($stackPtr - 1), null, false, null, true);

$validPrefixes = Tokens::SCOPE_MODIFIERS;
$validPrefixes[] = T_STATIC;
$validPrefixes[] = T_FINAL;
$validPrefixes[] = T_VAR;
$validPrefixes[] = T_READONLY;
$validPrefixes[] = T_ABSTRACT;
$validPrefixes = Tokens::PROPERTY_MODIFIERS;

$startOfStatement = $phpcsFile->findNext($validPrefixes, ($endOfPreviousStatement + 1), $stackPtr, false, null, true);
if ($startOfStatement === false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Ensure there is a single space after scope keywords.
* Ensure there is a single space after all modifier keywords (not just scope modifiers).
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
Expand All @@ -27,7 +27,8 @@ public function register()
{
$register = Tokens::METHOD_MODIFIERS;
$register += Tokens::SCOPE_MODIFIERS;
$register[T_READONLY] = T_READONLY;
$register += Tokens::PROPERTY_MODIFIERS;
$register += Tokens::CLASS_MODIFIERS;
return $register;
}

Expand Down
11 changes: 3 additions & 8 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2294,8 +2294,8 @@ protected function tokenize(string $code)
|| $tokenType === T_FN
|| isset(Tokens::METHOD_MODIFIERS[$tokenType]) === true
|| isset(Tokens::SCOPE_MODIFIERS[$tokenType]) === true
|| $tokenType === T_VAR
|| $tokenType === T_READONLY
|| isset(Tokens::PROPERTY_MODIFIERS[$tokenType]) === true
|| isset(Tokens::CLASS_MODIFIERS[$tokenType]) === true
) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
StatusWriter::write("* token $stackPtr changed from ? to T_NULLABLE", 2);
Expand Down Expand Up @@ -3544,12 +3544,7 @@ protected function processAdditional()
}

if ($suspectedType === 'property or parameter'
&& (isset(Tokens::SCOPE_MODIFIERS[$this->tokens[$x]['code']]) === true
|| $this->tokens[$x]['code'] === T_VAR
|| $this->tokens[$x]['code'] === T_STATIC
|| $this->tokens[$x]['code'] === T_READONLY
|| $this->tokens[$x]['code'] === T_FINAL
|| $this->tokens[$x]['code'] === T_ABSTRACT)
&& isset(Tokens::PROPERTY_MODIFIERS[$this->tokens[$x]['code']]) === true
) {
// This will also confirm constructor property promotion parameters, but that's fine.
$confirmed = true;
Expand Down
24 changes: 24 additions & 0 deletions src/Util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,30 @@ final class Tokens
T_FINAL => T_FINAL,
];

/**
* Tokens that can prefix a class name.
*
* @var array<int|string, int|string>
*/
public const CLASS_MODIFIERS = [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
];

/**
* Tokens that can prefix a property name.
*
* @var array<int|string, int|string>
*/
public const PROPERTY_MODIFIERS = (self::SCOPE_MODIFIERS + [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
T_STATIC => T_STATIC,
T_VAR => T_VAR,
]);

/**
* Tokens that open code blocks.
*
Expand Down