class ReferenceSpacingSniff
Hierarchy
- class \SlevomatCodingStandard\Sniffs\PHP\ReferenceSpacingSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of ReferenceSpacingSniff
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ PHP/ ReferenceSpacingSniff.php, line 25
Namespace
SlevomatCodingStandard\Sniffs\PHPView source
class ReferenceSpacingSniff implements Sniff {
public const CODE_INCORRECT_SPACES_AFTER_REFERENCE = 'IncorrectSpacesAfterReference';
/** @var int */
public $spacesCountAfterReference = 0;
/**
* @return array<int, (int|string)>
*/
public function register() : array {
return [
T_BITWISE_AND,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $referencePointer
*/
public function process(File $phpcsFile, $referencePointer) : void {
$this->spacesCountAfterReference = SniffSettingsHelper::normalizeInteger($this->spacesCountAfterReference);
if (!$this->isReference($phpcsFile, $referencePointer)) {
return;
}
$pointerAfterWhitespace = TokenHelper::findNextNonWhitespace($phpcsFile, $referencePointer + 1);
$whitespace = TokenHelper::getContent($phpcsFile, $referencePointer + 1, $pointerAfterWhitespace - 1);
$actualSpacesCount = strlen($whitespace);
if ($this->spacesCountAfterReference === $actualSpacesCount) {
return;
}
$errorMessage = $this->spacesCountAfterReference === 0 ? 'There must be no whitespace after reference.' : sprintf('There must be exactly %d whitespace%s after reference.', $this->spacesCountAfterReference, $this->spacesCountAfterReference !== 1 ? 's' : '');
$fix = $phpcsFile->addFixableError($errorMessage, $referencePointer, self::CODE_INCORRECT_SPACES_AFTER_REFERENCE);
if (!$fix) {
return;
}
$phpcsFile->fixer
->beginChangeset();
$phpcsFile->fixer
->addContent($referencePointer, str_repeat(' ', $this->spacesCountAfterReference));
FixerHelper::removeBetween($phpcsFile, $referencePointer, $pointerAfterWhitespace);
$phpcsFile->fixer
->endChangeset();
}
private function isReference(File $phpcsFile, int $referencePointer) : bool {
$tokens = $phpcsFile->getTokens();
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $referencePointer - 1);
if (in_array($tokens[$previousPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
return true;
}
$previousParenthesisOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $referencePointer - 1);
if ($previousParenthesisOpenerPointer !== null && $tokens[$previousParenthesisOpenerPointer]['parenthesis_closer'] > $referencePointer) {
if (array_key_exists('parenthesis_owner', $tokens[$previousParenthesisOpenerPointer])) {
$parenthesisOwnerPointer = $tokens[$previousParenthesisOpenerPointer]['parenthesis_owner'];
if (in_array($tokens[$parenthesisOwnerPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
return true;
}
}
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $previousParenthesisOpenerPointer - 1);
if ($pointerBeforeParenthesisOpener !== null && $tokens[$pointerBeforeParenthesisOpener]['code'] === T_USE) {
return true;
}
}
/** @var int $variableStartPointer */
$variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $referencePointer + 1);
$variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);
if ($variableEndPointer === null) {
return false;
}
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $referencePointer - 1);
return in_array($tokens[$previousPointer]['code'], [
T_EQUAL,
T_DOUBLE_ARROW,
T_OPEN_SHORT_ARRAY,
T_COMMA,
T_AS,
], true);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ReferenceSpacingSniff::$spacesCountAfterReference | public | property | @var int | |
ReferenceSpacingSniff::CODE_INCORRECT_SPACES_AFTER_REFERENCE | public | constant | ||
ReferenceSpacingSniff::isReference | private | function | ||
ReferenceSpacingSniff::process | public | function | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * |
Overrides Sniff::process |
ReferenceSpacingSniff::register | public | function | * | Overrides Sniff::register |