class AttributeAndTargetSpacingSniff
Hierarchy
- class \SlevomatCodingStandard\Sniffs\Attributes\AttributeAndTargetSpacingSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of AttributeAndTargetSpacingSniff
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Attributes/ AttributeAndTargetSpacingSniff.php, line 17
Namespace
SlevomatCodingStandard\Sniffs\AttributesView source
class AttributeAndTargetSpacingSniff implements Sniff {
public const CODE_INCORRECT_LINES_COUNT_BETWEEN_ATTRIBUTE_AND_TARGET = 'IncorrectLinesCountBetweenAttributeAndTarget';
/** @var int */
public $linesCount = 0;
/** @var bool */
public $allowOnSameLine = false;
/**
* @return array<int, (int|string)>
*/
public function register() : array {
return [
T_ATTRIBUTE,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $attributeOpenerPointer
*/
public function process(File $phpcsFile, $attributeOpenerPointer) : void {
$this->linesCount = SniffSettingsHelper::normalizeInteger($this->linesCount);
if (!AttributeHelper::isValidAttribute($phpcsFile, $attributeOpenerPointer)) {
return;
}
$tokens = $phpcsFile->getTokens();
$attributeCloserPointer = $tokens[$attributeOpenerPointer]['attribute_closer'];
$pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $attributeCloserPointer + 1);
while ($tokens[$pointerAfter]['code'] === T_COMMENT) {
$pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $pointerAfter + 1);
}
if ($tokens[$pointerAfter]['code'] === T_ATTRIBUTE) {
return;
}
$areOnSameLine = $tokens[$pointerAfter]['line'] === $tokens[$attributeCloserPointer]['line'];
if ($areOnSameLine) {
if ($this->allowOnSameLine) {
return;
}
$errorMessage = $this->linesCount === 1 ? 'Expected 1 blank line between attribute and its target, both are on same line.' : sprintf('Expected %1$d blank lines between attribute and its target, both are on same line.', $this->linesCount);
}
else {
$actualLinesCount = $tokens[$pointerAfter]['line'] - $tokens[$attributeCloserPointer]['line'] - 1;
if ($this->linesCount === $actualLinesCount) {
return;
}
$errorMessage = $this->linesCount === 1 ? sprintf('Expected 1 blank line between attribute and its target, found %1$d.', $actualLinesCount) : sprintf('Expected %1$d blank lines between attribute and its target, found %2$d.', $this->linesCount, $actualLinesCount);
}
$fix = $phpcsFile->addFixableError($errorMessage, $attributeOpenerPointer, self::CODE_INCORRECT_LINES_COUNT_BETWEEN_ATTRIBUTE_AND_TARGET);
if (!$fix) {
return;
}
if ($areOnSameLine) {
$indentation = IndentationHelper::getIndentation($phpcsFile, TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $pointerAfter));
$phpcsFile->fixer
->beginChangeset();
FixerHelper::removeWhitespaceAfter($phpcsFile, $attributeCloserPointer);
$phpcsFile->fixer
->addContentBefore($pointerAfter, str_repeat($phpcsFile->eolChar, $this->linesCount + 1) . $indentation);
$phpcsFile->fixer
->endChangeset();
return;
}
$firstTokenOnLine = TokenHelper::findFirstTokenOnLine($phpcsFile, $pointerAfter);
$phpcsFile->fixer
->beginChangeset();
FixerHelper::removeBetween($phpcsFile, $attributeCloserPointer, $firstTokenOnLine);
$phpcsFile->fixer
->addContentBefore($firstTokenOnLine, str_repeat($phpcsFile->eolChar, $this->linesCount + 1));
$phpcsFile->fixer
->endChangeset();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
AttributeAndTargetSpacingSniff::$allowOnSameLine | public | property | @var bool | |
AttributeAndTargetSpacingSniff::$linesCount | public | property | @var int | |
AttributeAndTargetSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ATTRIBUTE_AND_TARGET | public | constant | ||
AttributeAndTargetSpacingSniff::process | public | function | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * |
Overrides Sniff::process |
AttributeAndTargetSpacingSniff::register | public | function | * | Overrides Sniff::register |