class RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff
Hierarchy
- class \SlevomatCodingStandard\Sniffs\Operators\RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Operators/ RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff.php, line 22
Namespace
SlevomatCodingStandard\Sniffs\OperatorsView source
class RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff implements Sniff {
public const CODE_PRE_INCREMENT_OPERATOR_NOT_USED_STANDALONE = 'PreIncrementOperatorNotUsedStandalone';
public const CODE_POST_INCREMENT_OPERATOR_NOT_USED_STANDALONE = 'PostIncrementOperatorNotUsedStandalone';
public const CODE_PRE_DECREMENT_OPERATOR_NOT_USED_STANDALONE = 'PreDecrementOperatorNotUsedAsStandalone';
public const CODE_POST_DECREMENT_OPERATOR_NOT_USED_STANDALONE = 'PostDecrementOperatorNotUsedStandalone';
/**
* @return array<int, (int|string)>
*/
public function register() : array {
return [
T_DEC,
T_INC,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $operatorPointer
*/
public function process(File $phpcsFile, $operatorPointer) : void {
$tokens = $phpcsFile->getTokens();
/** @var int $nextPointer */
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $operatorPointer + 1);
$afterVariableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $nextPointer);
$isPostOperator = $afterVariableEndPointer === null;
if ($isPostOperator) {
/** @var int $beforeVariableEndPointer */
$beforeVariableEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $operatorPointer - 1);
/** @var int $instructionStartPointer */
$instructionStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $beforeVariableEndPointer);
$instructionEndPointer = $operatorPointer;
}
else {
$instructionStartPointer = $operatorPointer;
/** @var int $instructionEndPointer */
$instructionEndPointer = $afterVariableEndPointer;
}
if ($this->isStandalone($phpcsFile, $instructionStartPointer, $instructionEndPointer)) {
return;
}
if ($tokens[$operatorPointer]['code'] === T_INC) {
if ($isPostOperator) {
$code = self::CODE_POST_INCREMENT_OPERATOR_NOT_USED_STANDALONE;
$message = 'Post-increment operator should be used only as single instruction.';
}
else {
$code = self::CODE_PRE_INCREMENT_OPERATOR_NOT_USED_STANDALONE;
$message = 'Pre-increment operator should be used only as single instruction.';
}
}
else {
if ($isPostOperator) {
$code = self::CODE_POST_DECREMENT_OPERATOR_NOT_USED_STANDALONE;
$message = 'Post-decrement operator should be used only as single instruction.';
}
else {
$code = self::CODE_PRE_DECREMENT_OPERATOR_NOT_USED_STANDALONE;
$message = 'Pre-decrement operator should be used only as single instruction.';
}
}
$phpcsFile->addError($message, $operatorPointer, $code);
}
private function isStandalone(File $phpcsFile, int $instructionStartPointer, int $instructionEndPointer) : bool {
$tokens = $phpcsFile->getTokens();
$pointerBeforeInstructionStart = TokenHelper::findPreviousEffective($phpcsFile, $instructionStartPointer - 1);
if (!in_array($tokens[$pointerBeforeInstructionStart]['code'], [
T_SEMICOLON,
T_COLON,
T_OPEN_CURLY_BRACKET,
T_CLOSE_CURLY_BRACKET,
T_OPEN_TAG,
], true)) {
return false;
}
$pointerAfterInstructionEnd = TokenHelper::findNextEffective($phpcsFile, $instructionEndPointer + 1);
if ($tokens[$pointerAfterInstructionEnd]['code'] === T_SEMICOLON) {
return true;
}
if ($tokens[$pointerAfterInstructionEnd]['code'] === T_CLOSE_PARENTHESIS) {
return array_key_exists('parenthesis_owner', $tokens[$pointerAfterInstructionEnd]) && in_array($tokens[$tokens[$pointerAfterInstructionEnd]['parenthesis_owner']]['code'], [
T_FOR,
T_WHILE,
], true);
}
return false;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::CODE_POST_DECREMENT_OPERATOR_NOT_USED_STANDALONE | public | constant | ||
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::CODE_POST_INCREMENT_OPERATOR_NOT_USED_STANDALONE | public | constant | ||
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::CODE_PRE_DECREMENT_OPERATOR_NOT_USED_STANDALONE | public | constant | ||
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::CODE_PRE_INCREMENT_OPERATOR_NOT_USED_STANDALONE | public | constant | ||
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::isStandalone | private | function | ||
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::process | public | function | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * |
Overrides Sniff::process |
RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::register | public | function | * | Overrides Sniff::register |