clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,120 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Attributes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AttributeHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\IndentationHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function sprintf;
use function str_repeat;
use const T_ATTRIBUTE;
use const T_COMMENT;
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();
}
}
@@ -0,0 +1,197 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Attributes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AttributeHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\IndentationHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use UnexpectedValueException;
use function array_keys;
use function asort;
use function count;
use function ltrim;
use function strnatcmp;
use function strpos;
use function substr;
use function trim;
use function uasort;
use const T_ATTRIBUTE;
use const T_ATTRIBUTE_END;
class AttributesOrderSniff implements Sniff
{
public const CODE_INCORRECT_ORDER = 'IncorrectOrder';
/** @var list<string> */
public $order = [];
/** @var bool */
public $orderAlphabetically = 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
{
if (!AttributeHelper::isValidAttribute($phpcsFile, $attributeOpenerPointer)) {
return;
}
if ($this->order === [] && !$this->orderAlphabetically) {
throw new UnexpectedValueException('Neither manual or alphabetical order is set.');
}
if ($this->order !== [] && $this->orderAlphabetically) {
throw new UnexpectedValueException('Only one order can be set.');
}
$this->order = $this->normalizeOrder($this->order);
$tokens = $phpcsFile->getTokens();
$pointerBefore = TokenHelper::findPreviousNonWhitespace($phpcsFile, $attributeOpenerPointer - 1);
if ($tokens[$pointerBefore]['code'] === T_ATTRIBUTE_END) {
return;
}
$attributesGroups = [AttributeHelper::getAttributes($phpcsFile, $attributeOpenerPointer)];
$lastAttributeCloserPointer = $tokens[$attributeOpenerPointer]['attribute_closer'];
do {
$nextPointer = TokenHelper::findNextNonWhitespace($phpcsFile, $lastAttributeCloserPointer + 1);
if ($tokens[$nextPointer]['code'] !== T_ATTRIBUTE) {
break;
}
$attributesGroups[] = AttributeHelper::getAttributes($phpcsFile, $nextPointer);
$lastAttributeCloserPointer = $tokens[$nextPointer]['attribute_closer'];
} while (true);
if ($this->orderAlphabetically) {
$actualOrder = $attributesGroups;
$expectedOrder = $actualOrder;
uasort($expectedOrder, static function (array $attributesGroup1, array $attributesGroup2): int {
return strnatcmp($attributesGroup1[0]->getName(), $attributesGroup2[0]->getName());
});
} else {
$actualOrder = [];
foreach ($attributesGroups as $attributesGroupNo => $attributesGroup) {
$attributeName = $this->normalizeAttributeName($attributesGroup[0]->getName());
foreach ($this->order as $orderPosition => $attributeNameOnPosition) {
if (
$attributeName === $attributeNameOnPosition
|| (
substr($attributeNameOnPosition, -1) === '\\'
&& strpos($attributeName, $attributeNameOnPosition) === 0
)
|| (
substr($attributeNameOnPosition, -1) === '*'
&& strpos($attributeName, substr($attributeNameOnPosition, 0, -1)) === 0
)
) {
$actualOrder[$attributesGroupNo] = $orderPosition;
continue 2;
}
}
// Unknown order - add to the end
$actualOrder[$attributesGroupNo] = 999;
}
$expectedOrder = $actualOrder;
asort($expectedOrder);
}
if ($expectedOrder === $actualOrder) {
return;
}
$fix = $phpcsFile->addFixableError('Incorrect order of attributes.', $attributeOpenerPointer, self::CODE_INCORRECT_ORDER);
if (!$fix) {
return;
}
$attributesGroupsContent = [];
foreach ($attributesGroups as $attributesGroupNo => $attributesGroup) {
$attributesGroupsContent[$attributesGroupNo] = TokenHelper::getContent(
$phpcsFile,
$attributesGroup[0]->getAttributePointer(),
$tokens[$attributesGroup[0]->getAttributePointer()]['attribute_closer']
);
}
$areOnSameLine = $tokens[$attributeOpenerPointer]['line'] === $tokens[$lastAttributeCloserPointer]['line'];
$attributesStartPointer = $attributeOpenerPointer;
$attributesEndPointer = $lastAttributeCloserPointer;
$indentation = IndentationHelper::getIndentation($phpcsFile, $attributeOpenerPointer);
$phpcsFile->fixer->beginChangeset();
FixerHelper::removeBetweenIncluding($phpcsFile, $attributesStartPointer, $attributesEndPointer);
foreach (array_keys($expectedOrder) as $position => $attributesGroupNo) {
if ($areOnSameLine) {
if ($position !== 0) {
$phpcsFile->fixer->addContent($attributesStartPointer, ' ');
}
$phpcsFile->fixer->addContent($attributesStartPointer, $attributesGroupsContent[$attributesGroupNo]);
} else {
if ($position !== 0) {
$phpcsFile->fixer->addContent($attributesStartPointer, $indentation);
}
$phpcsFile->fixer->addContent($attributesStartPointer, $attributesGroupsContent[$attributesGroupNo]);
if ($position !== count($attributesGroups) - 1) {
$phpcsFile->fixer->addNewline($attributesStartPointer);
}
}
}
$phpcsFile->fixer->endChangeset();
}
/**
* @param list<string> $order
* @return list<string>
*/
private function normalizeOrder(array $order): array
{
foreach ($order as $itemNo => $item) {
$order[$itemNo] = $this->normalizeAttributeName(trim($item));
}
return $order;
}
private function normalizeAttributeName(string $name): string
{
return ltrim($name, '\\');
}
}
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Attributes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AttributeHelper;
use function count;
use function sprintf;
use const T_ATTRIBUTE;
class DisallowAttributesJoiningSniff implements Sniff
{
public const CODE_DISALLOWED_ATTRIBUTES_JOINING = 'DisallowedAttributesJoining';
/**
* @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;
}
$attributes = AttributeHelper::getAttributes($phpcsFile, $attributeOpenerPointer);
$attributeCount = count($attributes);
if ($attributeCount === 1) {
return;
}
$fix = $phpcsFile->addFixableError(
sprintf('%d attributes are joined.', $attributeCount),
$attributeOpenerPointer,
self::CODE_DISALLOWED_ATTRIBUTES_JOINING
);
if (!$fix) {
return;
}
$phpcsFile->fixer->beginChangeset();
for ($i = 1; $i < count($attributes); $i++) {
$previousAttribute = $attributes[$i - 1];
$attribute = $attributes[$i];
$phpcsFile->fixer->addContent($previousAttribute->getEndPointer(), ']');
for ($j = $previousAttribute->getEndPointer() + 1; $j < $attribute->getStartPointer(); $j++) {
if ($phpcsFile->fixer->getTokenContent($j) === ',') {
$phpcsFile->fixer->replaceToken($j, '');
}
}
$phpcsFile->fixer->addContentBefore($attribute->getStartPointer(), '#[');
}
$phpcsFile->fixer->endChangeset();
}
}
@@ -0,0 +1,82 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Attributes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AttributeHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\IndentationHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_ATTRIBUTE;
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();
}
}
@@ -0,0 +1,86 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Attributes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AttributeHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_ATTRIBUTE;
use const T_ATTRIBUTE_END;
use const T_DOC_COMMENT_OPEN_TAG;
use const T_WHITESPACE;
class RequireAttributeAfterDocCommentSniff implements Sniff
{
public const CODE_ATTRIBUTE_BEFORE_DOC_COMMENT = 'AttributeBeforeDocComment';
/**
* @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();
$docCommentOpenerPointer = TokenHelper::findNextExcluding(
$phpcsFile,
T_WHITESPACE,
$tokens[$attributeOpenerPointer]['attribute_closer'] + 1
);
if ($tokens[$docCommentOpenerPointer]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
return;
}
$docCommentStartPointer = TokenHelper::findFirstTokenOnLine($phpcsFile, $docCommentOpenerPointer);
$docCommentEndPointer = TokenHelper::findLastTokenOnLine($phpcsFile, $tokens[$docCommentOpenerPointer]['comment_closer']);
$docComment = TokenHelper::getContent($phpcsFile, $docCommentStartPointer, $docCommentEndPointer);
$firstAttributeOpenerPointer = $attributeOpenerPointer;
do {
$nonWhitespacePointerBefore = TokenHelper::findPreviousNonWhitespace($phpcsFile, $firstAttributeOpenerPointer - 1);
if ($tokens[$nonWhitespacePointerBefore]['code'] !== T_ATTRIBUTE_END) {
break;
}
$firstAttributeOpenerPointer = $tokens[$nonWhitespacePointerBefore]['attribute_opener'];
} while (true);
$attributeStartPointer = TokenHelper::findFirstTokenOnLine($phpcsFile, $firstAttributeOpenerPointer);
$fix = $phpcsFile->addFixableError(
'Attribute should be placed after documentation comment.',
$attributeOpenerPointer,
self::CODE_ATTRIBUTE_BEFORE_DOC_COMMENT
);
if (!$fix) {
return;
}
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContentBefore($attributeStartPointer, $docComment);
FixerHelper::removeBetweenIncluding($phpcsFile, $docCommentStartPointer, $docCommentEndPointer);
$phpcsFile->fixer->endChangeset();
}
}