clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use function sprintf;
use const T_IS_EQUAL;
use const T_IS_NOT_EQUAL;
class DisallowEqualOperatorsSniff implements Sniff
{
public const CODE_DISALLOWED_EQUAL_OPERATOR = 'DisallowedEqualOperator';
public const CODE_DISALLOWED_NOT_EQUAL_OPERATOR = 'DisallowedNotEqualOperator';
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_IS_EQUAL,
T_IS_NOT_EQUAL,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $operatorPointer
*/
public function process(File $phpcsFile, $operatorPointer): void
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$operatorPointer]['code'] === T_IS_EQUAL) {
$fix = $phpcsFile->addFixableError(
'Operator == is disallowed, use === instead.',
$operatorPointer,
self::CODE_DISALLOWED_EQUAL_OPERATOR
);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($operatorPointer, '===');
$phpcsFile->fixer->endChangeset();
}
} else {
$fix = $phpcsFile->addFixableError(sprintf(
'Operator %s is disallowed, use !== instead.',
$tokens[$operatorPointer]['content']
), $operatorPointer, self::CODE_DISALLOWED_NOT_EQUAL_OPERATOR);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($operatorPointer, '!==');
$phpcsFile->fixer->endChangeset();
}
}
}
}

View File

@@ -0,0 +1,65 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_DEC;
use const T_INC;
class DisallowIncrementAndDecrementOperatorsSniff implements Sniff
{
public const CODE_DISALLOWED_PRE_INCREMENT_OPERATOR = 'DisallowedPreIncrementOperator';
public const CODE_DISALLOWED_POST_INCREMENT_OPERATOR = 'DisallowedPostIncrementOperator';
public const CODE_DISALLOWED_PRE_DECREMENT_OPERATOR = 'DisallowedPreDecrementOperator';
public const CODE_DISALLOWED_POST_DECREMENT_OPERATOR = 'DisallowedPostDecrementOperator';
/**
* @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 ($tokens[$operatorPointer]['code'] === T_INC) {
if ($isPostOperator) {
$code = self::CODE_DISALLOWED_POST_INCREMENT_OPERATOR;
$message = 'Use of post-increment operator is disallowed.';
} else {
$code = self::CODE_DISALLOWED_PRE_INCREMENT_OPERATOR;
$message = 'Use of pre-increment operator is disallowed.';
}
} else {
if ($isPostOperator) {
$code = self::CODE_DISALLOWED_POST_DECREMENT_OPERATOR;
$message = 'Use of post-decrement operator is disallowed.';
} else {
$code = self::CODE_DISALLOWED_PRE_DECREMENT_OPERATOR;
$message = 'Use of pre-decrement operator is disallowed.';
}
}
$phpcsFile->addError($message, $operatorPointer, $code);
}
}

View File

@@ -0,0 +1,125 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_merge;
use function in_array;
use function sprintf;
use function strlen;
use const T_CLASS_C;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSE_SHORT_ARRAY;
use const T_CLOSE_SQUARE_BRACKET;
use const T_CONSTANT_ENCAPSED_STRING;
use const T_DIR;
use const T_DNUMBER;
use const T_ENCAPSED_AND_WHITESPACE;
use const T_FILE;
use const T_FUNC_C;
use const T_LINE;
use const T_LNUMBER;
use const T_METHOD_C;
use const T_MINUS;
use const T_NS_C;
use const T_NUM_STRING;
use const T_TRAIT_C;
use const T_VARIABLE;
use const T_WHITESPACE;
class NegationOperatorSpacingSniff implements Sniff
{
public const CODE_INVALID_SPACE_AFTER_MINUS = 'InvalidSpaceAfterMinus';
/** @var int */
public $spacesCount = 0;
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [T_MINUS];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $pointer
*/
public function process(File $phpcsFile, $pointer): void
{
$this->spacesCount = SniffSettingsHelper::normalizeInteger($this->spacesCount);
$tokens = $phpcsFile->getTokens();
$previousEffective = TokenHelper::findPreviousEffective($phpcsFile, $pointer - 1);
$possibleOperandTypes = array_merge(
TokenHelper::getOnlyNameTokenCodes(),
[
T_CONSTANT_ENCAPSED_STRING,
T_CLASS_C,
T_CLOSE_PARENTHESIS,
T_CLOSE_SHORT_ARRAY,
T_CLOSE_SQUARE_BRACKET,
T_DIR,
T_DNUMBER,
T_ENCAPSED_AND_WHITESPACE,
T_FILE,
T_FUNC_C,
T_LINE,
T_LNUMBER,
T_METHOD_C,
T_NS_C,
T_NUM_STRING,
T_TRAIT_C,
T_VARIABLE,
]
);
if (in_array($tokens[$previousEffective]['code'], $possibleOperandTypes, true)) {
return;
}
$possibleVariableStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $previousEffective);
if ($possibleVariableStartPointer !== null) {
return;
}
$whitespacePointer = $pointer + 1;
$numberOfSpaces = $tokens[$whitespacePointer]['code'] !== T_WHITESPACE ? 0 : strlen($tokens[$whitespacePointer]['content']);
if ($numberOfSpaces === $this->spacesCount) {
return;
}
$fix = $phpcsFile->addFixableError(
sprintf(
'Expected exactly %d space after "%s", %d found.',
$this->spacesCount,
$tokens[$pointer]['content'],
$numberOfSpaces
),
$pointer,
self::CODE_INVALID_SPACE_AFTER_MINUS
);
if (!$fix) {
return;
}
if ($this->spacesCount > $numberOfSpaces) {
$phpcsFile->fixer->addContent($pointer, ' ');
return;
}
$phpcsFile->fixer->replaceToken($whitespacePointer, '');
}
}

View File

@@ -0,0 +1,149 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_key_exists;
use function in_array;
use function sprintf;
use const T_BITWISE_AND;
use const T_BITWISE_OR;
use const T_BITWISE_XOR;
use const T_CLOSE_SQUARE_BRACKET;
use const T_CONSTANT_ENCAPSED_STRING;
use const T_DIVIDE;
use const T_DNUMBER;
use const T_DOUBLE_QUOTED_STRING;
use const T_EQUAL;
use const T_LNUMBER;
use const T_MINUS;
use const T_MODULUS;
use const T_MULTIPLY;
use const T_PLUS;
use const T_POW;
use const T_SEMICOLON;
use const T_SL;
use const T_SR;
use const T_START_HEREDOC;
use const T_START_NOWDOC;
use const T_STRING_CONCAT;
class RequireCombinedAssignmentOperatorSniff implements Sniff
{
public const CODE_REQUIRED_COMBINED_ASSIGNMENT_OPERATOR = 'RequiredCombinedAssignmentOperator';
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_EQUAL,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $equalPointer
*/
public function process(File $phpcsFile, $equalPointer): void
{
$tokens = $phpcsFile->getTokens();
/** @var int $variableStartPointer */
$variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $equalPointer + 1);
$variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);
if ($variableEndPointer === null) {
return;
}
$operatorPointer = TokenHelper::findNextEffective($phpcsFile, $variableEndPointer + 1);
$operators = [
T_BITWISE_AND => '&=',
T_BITWISE_OR => '|=',
T_STRING_CONCAT => '.=',
T_DIVIDE => '/=',
T_MINUS => '-=',
T_POW => '**=',
T_MODULUS => '%=',
T_MULTIPLY => '*=',
T_PLUS => '+=',
T_SL => '<<=',
T_SR => '>>=',
T_BITWISE_XOR => '^=',
];
if (!array_key_exists($tokens[$operatorPointer]['code'], $operators)) {
return;
}
$isFixable = true;
if ($tokens[$variableEndPointer]['code'] === T_CLOSE_SQUARE_BRACKET) {
$pointerAfterOperator = TokenHelper::findNextEffective($phpcsFile, $operatorPointer + 1);
if (in_array(
$tokens[$pointerAfterOperator]['code'],
[T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING, T_START_HEREDOC, T_START_NOWDOC],
true
)) {
return;
}
$isFixable = in_array($tokens[$pointerAfterOperator]['code'], [T_LNUMBER, T_DNUMBER], true);
}
$variableContent = IdentificatorHelper::getContent($phpcsFile, $variableStartPointer, $variableEndPointer);
/** @var int $beforeEqualEndPointer */
$beforeEqualEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $equalPointer - 1);
$beforeEqualStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $beforeEqualEndPointer);
if ($beforeEqualStartPointer === null) {
return;
}
$beforeEqualVariableContent = IdentificatorHelper::getContent($phpcsFile, $beforeEqualStartPointer, $beforeEqualEndPointer);
if ($beforeEqualVariableContent !== $variableContent) {
return;
}
$semicolonPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $equalPointer + 1);
if (TokenHelper::findNext($phpcsFile, Tokens::$operators, $operatorPointer + 1, $semicolonPointer) !== null) {
return;
}
$errorMessage = sprintf(
'Use "%s" operator instead of "=" and "%s".',
$operators[$tokens[$operatorPointer]['code']],
$tokens[$operatorPointer]['content']
);
if (!$isFixable) {
$phpcsFile->addError($errorMessage, $equalPointer, self::CODE_REQUIRED_COMBINED_ASSIGNMENT_OPERATOR);
return;
}
$fix = $phpcsFile->addFixableError($errorMessage, $equalPointer, self::CODE_REQUIRED_COMBINED_ASSIGNMENT_OPERATOR);
if (!$fix) {
return;
}
$phpcsFile->fixer->beginChangeset();
FixerHelper::change($phpcsFile, $equalPointer, $operatorPointer, $operators[$tokens[$operatorPointer]['code']]);
$phpcsFile->fixer->endChangeset();
}
}

View File

@@ -0,0 +1,118 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_key_exists;
use function in_array;
use const T_CLOSE_CURLY_BRACKET;
use const T_CLOSE_PARENTHESIS;
use const T_COLON;
use const T_DEC;
use const T_FOR;
use const T_INC;
use const T_OPEN_CURLY_BRACKET;
use const T_OPEN_TAG;
use const T_SEMICOLON;
use const T_WHILE;
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;
}
}

View File

@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function sprintf;
use function str_repeat;
use function strlen;
use const T_ELLIPSIS;
class SpreadOperatorSpacingSniff implements Sniff
{
public const CODE_INCORRECT_SPACES_AFTER_OPERATOR = 'IncorrectSpacesAfterOperator';
/** @var int */
public $spacesCountAfterOperator = 0;
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_ELLIPSIS,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $spreadOperatorPointer
*/
public function process(File $phpcsFile, $spreadOperatorPointer): void
{
$this->spacesCountAfterOperator = SniffSettingsHelper::normalizeInteger($this->spacesCountAfterOperator);
$pointerAfterWhitespace = TokenHelper::findNextNonWhitespace($phpcsFile, $spreadOperatorPointer + 1);
$whitespace = TokenHelper::getContent($phpcsFile, $spreadOperatorPointer + 1, $pointerAfterWhitespace - 1);
if ($this->spacesCountAfterOperator === strlen($whitespace)) {
return;
}
$errorMessage = $this->spacesCountAfterOperator === 0
? 'There must be no whitespace after spread operator.'
: sprintf(
'There must be exactly %d whitespace%s after spread operator.',
$this->spacesCountAfterOperator,
$this->spacesCountAfterOperator !== 1 ? 's' : ''
);
$fix = $phpcsFile->addFixableError($errorMessage, $spreadOperatorPointer, self::CODE_INCORRECT_SPACES_AFTER_OPERATOR);
if (!$fix) {
return;
}
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($spreadOperatorPointer, str_repeat(' ', $this->spacesCountAfterOperator));
FixerHelper::removeBetween($phpcsFile, $spreadOperatorPointer, $pointerAfterWhitespace);
$phpcsFile->fixer->endChangeset();
}
}