class DisallowMultipleAttributesPerLineSniff
Hierarchy
- class \SlevomatCodingStandard\Sniffs\Attributes\DisallowMultipleAttributesPerLineSniff implements \PHP_CodeSniffer\Sniffs\Sniff
Expanded class hierarchy of DisallowMultipleAttributesPerLineSniff
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Attributes/ DisallowMultipleAttributesPerLineSniff.php, line 13
Namespace
SlevomatCodingStandard\Sniffs\AttributesView source
class DisallowMultipleAttributesPerLineSniff implements Sniff {
public const CODE_DISALLOWED_MULTIPLE_ATTRIBUTES_PER_LINE = 'DisallowedMultipleAttributesPerLine';
/**
* @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 {
if (!AttributeHelper::isValidAttribute($phpcsFile, $attributeOpenerPointer)) {
return;
}
$tokens = $phpcsFile->getTokens();
$attributeCloserPointer = $tokens[$attributeOpenerPointer]['attribute_closer'];
$nextAttributeOpenerPointer = TokenHelper::findNext($phpcsFile, T_ATTRIBUTE, $attributeCloserPointer + 1);
if ($nextAttributeOpenerPointer === null) {
return;
}
if ($tokens[$attributeCloserPointer]['line'] !== $tokens[$nextAttributeOpenerPointer]['line']) {
return;
}
$attributeTargetPointer = AttributeHelper::getAttributeTarget($phpcsFile, $attributeOpenerPointer);
$nextAttributeTargetPointer = AttributeHelper::getAttributeTarget($phpcsFile, $nextAttributeOpenerPointer);
if ($attributeTargetPointer !== $nextAttributeTargetPointer) {
return;
}
$fix = $phpcsFile->addFixableError('Multiple attributes per line are disallowed.', $nextAttributeOpenerPointer, self::CODE_DISALLOWED_MULTIPLE_ATTRIBUTES_PER_LINE);
if (!$fix) {
return;
}
$nonWhitespacePointerBefore = TokenHelper::findPreviousNonWhitespace($phpcsFile, $nextAttributeOpenerPointer - 1);
$indentation = IndentationHelper::getIndentation($phpcsFile, TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $attributeOpenerPointer));
$phpcsFile->fixer
->beginChangeset();
FixerHelper::removeBetween($phpcsFile, $nonWhitespacePointerBefore, $nextAttributeOpenerPointer);
$phpcsFile->fixer
->addContentBefore($nextAttributeOpenerPointer, $phpcsFile->eolChar . $indentation);
$phpcsFile->fixer
->endChangeset();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
DisallowMultipleAttributesPerLineSniff::CODE_DISALLOWED_MULTIPLE_ATTRIBUTES_PER_LINE | public | constant | ||
DisallowMultipleAttributesPerLineSniff::process | public | function | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * |
Overrides Sniff::process |
DisallowMultipleAttributesPerLineSniff::register | public | function | * | Overrides Sniff::register |