Skip to content
28 changes: 14 additions & 14 deletions src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
class ObjectOperatorIndentSniff implements Sniff
{

/**
* Tokens to listen for.
*
* @var array<int|string>
*/
private const TARGET_TOKENS = [
T_OBJECT_OPERATOR,
T_NULLSAFE_OBJECT_OPERATOR,
];

/**
* The number of spaces code should be indented.
*
Expand All @@ -29,16 +39,6 @@ class ObjectOperatorIndentSniff implements Sniff
*/
public $multilevel = false;

/**
* Tokens to listen for.
*
* @var array
*/
private $targets = [
T_OBJECT_OPERATOR,
T_NULLSAFE_OBJECT_OPERATOR,
];


/**
* Returns an array of tokens this test wants to listen for.
Expand All @@ -47,7 +47,7 @@ class ObjectOperatorIndentSniff implements Sniff
*/
public function register()
{
return $this->targets;
return self::TARGET_TOKENS;

}//end register()

Expand All @@ -67,14 +67,14 @@ public function process(File $phpcsFile, $stackPtr)

// Make sure this is the first object operator in a chain of them.
$start = $phpcsFile->findStartOfStatement($stackPtr);
$prev = $phpcsFile->findPrevious($this->targets, ($stackPtr - 1), $start);
$prev = $phpcsFile->findPrevious(self::TARGET_TOKENS, ($stackPtr - 1), $start);
if ($prev !== false) {
return;
}

// Make sure this is a chained call.
$end = $phpcsFile->findEndOfStatement($stackPtr);
$next = $phpcsFile->findNext($this->targets, ($stackPtr + 1), $end);
$next = $phpcsFile->findNext(self::TARGET_TOKENS, ($stackPtr + 1), $end);
if ($next === false) {
// Not a chained call.
return;
Expand Down Expand Up @@ -189,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr)
}//end if

$next = $phpcsFile->findNext(
$this->targets,
self::TARGET_TOKENS,
($next + 1),
$end
);
Expand Down
42 changes: 26 additions & 16 deletions src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@
class SideEffectsSniff implements Sniff
{

/**
* Tokens which represent symbols in the context of this sniff.
*
* @var array<int|string, int|string>
*/
private const SYMBOL_TOKENS = [
T_CLASS => T_CLASS,
T_INTERFACE => T_INTERFACE,
T_TRAIT => T_TRAIT,
T_ENUM => T_ENUM,
T_FUNCTION => T_FUNCTION,
];

/**
* Condition tokens within which symbols may be defined.
*
* @var array<int|string, int|string>
*/
private const CONDITION_TOKENS = [
T_IF => T_IF,
T_ELSE => T_ELSE,
T_ELSEIF => T_ELSEIF,
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -78,20 +102,6 @@ public function process(File $phpcsFile, $stackPtr)
*/
private function searchForConflict($phpcsFile, $start, $end, $tokens)
{
$symbols = [
T_CLASS => T_CLASS,
T_INTERFACE => T_INTERFACE,
T_TRAIT => T_TRAIT,
T_ENUM => T_ENUM,
T_FUNCTION => T_FUNCTION,
];

$conditions = [
T_IF => T_IF,
T_ELSE => T_ELSE,
T_ELSEIF => T_ELSEIF,
];

$checkAnnotations = $phpcsFile->config->annotations;

$firstSymbol = null;
Expand Down Expand Up @@ -192,7 +202,7 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
}

// Detect and skip over symbols.
if (isset($symbols[$tokens[$i]['code']]) === true
if (isset(self::SYMBOL_TOKENS[$tokens[$i]['code']]) === true
&& isset($tokens[$i]['scope_closer']) === true
) {
if ($firstSymbol === null) {
Expand Down Expand Up @@ -251,7 +261,7 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
// Conditional statements are allowed in symbol files as long as the
// contents is only a symbol definition. So don't count these as effects
// in this case.
if (isset($conditions[$tokens[$i]['code']]) === true) {
if (isset(self::CONDITION_TOKENS[$tokens[$i]['code']]) === true) {
if (isset($tokens[$i]['scope_opener']) === false) {
// Probably an "else if", so just ignore.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
class BooleanOperatorPlacementSniff implements Sniff
{

/**
* Boolean operator tokens.
*
* @var array<int|string>
*/
private const BOOLEAN_OPERATORS = [
T_BOOLEAN_AND,
T_BOOLEAN_OR,
];

/**
* Used to restrict the placement of the boolean operator.
*
Expand Down Expand Up @@ -70,11 +80,6 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$find = [
T_BOOLEAN_AND,
T_BOOLEAN_OR,
];

if ($this->allowOnly === 'first' || $this->allowOnly === 'last') {
$position = $this->allowOnly;
} else {
Expand All @@ -86,7 +91,7 @@ public function process(File $phpcsFile, $stackPtr)
$operators = [];

do {
$operator = $phpcsFile->findNext($find, ($operator + 1), $parenCloser);
$operator = $phpcsFile->findNext(self::BOOLEAN_OPERATORS, ($operator + 1), $parenCloser);
if ($operator === false) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,25 @@

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

class NullableTypeDeclarationSniff implements Sniff
{

/**
* An array of valid tokens after `T_NULLABLE` occurrences.
*
* @var array
* @var array<int|string, int|string>
*/
private $validTokens = [
T_STRING => true,
T_NAME_QUALIFIED => true,
T_NAME_FULLY_QUALIFIED => true,
T_NAME_RELATIVE => true,
T_CALLABLE => true,
T_SELF => true,
T_PARENT => true,
T_STATIC => true,
T_NULL => true,
T_FALSE => true,
T_TRUE => true,
];
private const VALID_TOKENS = (Tokens::NAME_TOKENS + [
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_STATIC => T_STATIC,
T_NULL => T_NULL,
T_FALSE => T_FALSE,
T_TRUE => T_TRUE,
]);


/**
Expand Down Expand Up @@ -66,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)

$tokens = $phpcsFile->getTokens();
$nextNonEmptyCode = $tokens[$nextNonEmptyPtr]['code'];
$validTokenFound = isset($this->validTokens[$nextNonEmptyCode]);
$validTokenFound = isset(self::VALID_TOKENS[$nextNonEmptyCode]);

if ($validTokenFound === true && $nextNonEmptyPtr === ($stackPtr + 1)) {
// Valid structure.
Expand Down
19 changes: 12 additions & 7 deletions src/Standards/PSR12/Sniffs/Properties/ConstantVisibilitySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
class ConstantVisibilitySniff implements Sniff
{

/**
* Visibility tokens which are valid for class constants.
*
* @var array<int, int>
*/
private const VALID_VISIBILITY = [
T_PRIVATE => T_PRIVATE,
T_PUBLIC => T_PUBLIC,
T_PROTECTED => T_PROTECTED,
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -50,14 +61,8 @@ public function process(File $phpcsFile, $stackPtr)
$ignore = Tokens::EMPTY_TOKENS;
$ignore[] = T_FINAL;

$validVisibility = [
T_PRIVATE => T_PRIVATE,
T_PUBLIC => T_PUBLIC,
T_PROTECTED => T_PROTECTED,
];

$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
if (isset($validVisibility[$tokens[$prev]['code']]) === true) {
if (isset(self::VALID_VISIBILITY[$tokens[$prev]['code']]) === true) {
return;
}

Expand Down
19 changes: 12 additions & 7 deletions src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
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 @@ -65,16 +76,10 @@ public function processOpen(File $phpcsFile, $stackPtr)
$stackPtrType = strtolower($tokens[$stackPtr]['content']);

// Check alignment of the keyword and braces.
$classModifiers = [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
];

$prevNonSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);

if (isset($classModifiers[$tokens[$prevNonEmpty]['code']]) === true) {
if (isset(self::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 @@ -16,6 +16,20 @@
class SwitchDeclarationSniff implements Sniff
{

/**
* Tokens which can terminate a "case".
*
* @var array<int|string, int|string>
*/
private const CASE_TERMINATING_TOKENS = [
T_RETURN => T_RETURN,
T_BREAK => T_BREAK,
T_CONTINUE => T_CONTINUE,
T_THROW => T_THROW,
T_EXIT => T_EXIT,
T_GOTO => T_GOTO,
];

/**
* The number of spaces code should be indented.
*
Expand Down Expand Up @@ -382,17 +396,8 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
} else if ($tokens[$lastToken]['code'] === T_SEMICOLON) {
// We found the last statement of the CASE. Now we want to
// check whether it is a terminating one.
$terminators = [
T_RETURN => T_RETURN,
T_BREAK => T_BREAK,
T_CONTINUE => T_CONTINUE,
T_THROW => T_THROW,
T_EXIT => T_EXIT,
T_GOTO => T_GOTO,
];

$terminator = $phpcsFile->findStartOfStatement(($lastToken - 1));
if (isset($terminators[$tokens[$terminator]['code']]) === true) {
if (isset(self::CASE_TERMINATING_TOKENS[$tokens[$terminator]['code']]) === true) {
return $terminator;
}
}//end if
Expand Down
Loading