Skip to content

Commit 31ed4d5

Browse files
committed
Cleanup
1 parent 387cd37 commit 31ed4d5

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

SlevomatCodingStandard/Helpers/AttributeHelper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use InvalidArgumentException;
66
use PHP_CodeSniffer\Files\File;
7+
use function array_map;
8+
use function in_array;
79
use function sprintf;
810
use const T_ANON_CLASS;
911
use const T_ATTRIBUTE;
@@ -40,6 +42,23 @@ class AttributeHelper
4042
T_VARIABLE,
4143
];
4244

45+
public static function hasAttribute(File $phpcsFile, int $docCommentOpenPointer, string $attributeName): bool
46+
{
47+
$tokens = $phpcsFile->getTokens();
48+
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $docCommentOpenPointer + 1);
49+
50+
if ($nextPointer === null || $tokens[$nextPointer]['code'] !== T_ATTRIBUTE) {
51+
return false;
52+
}
53+
54+
$attributeNames = array_map(
55+
static fn (Attribute $name): string => $name->getFullyQualifiedName(),
56+
self::getAttributes($phpcsFile, $nextPointer),
57+
);
58+
59+
return in_array($attributeName, $attributeNames, true);
60+
}
61+
4362
/**
4463
* @return list<Attribute>
4564
*/

SlevomatCodingStandard/Sniffs/TypeHints/DisallowMixedTypeHintSniff.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
88
use SlevomatCodingStandard\Helpers\AnnotationHelper;
9-
use SlevomatCodingStandard\Helpers\Attribute;
109
use SlevomatCodingStandard\Helpers\AttributeHelper;
1110
use SlevomatCodingStandard\Helpers\SuppressHelper;
12-
use SlevomatCodingStandard\Helpers\TokenHelper;
13-
use function array_map;
14-
use function in_array;
1511
use function sprintf;
1612
use function strtolower;
17-
use const T_ATTRIBUTE;
1813
use const T_DOC_COMMENT_OPEN_TAG;
1914

2015
class DisallowMixedTypeHintSniff implements Sniff
@@ -48,7 +43,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
4843
return;
4944
}
5045

51-
if ($this->targetHasOverrideAttribute($phpcsFile, $docCommentOpenPointer)) {
46+
if (AttributeHelper::hasAttribute($phpcsFile, $docCommentOpenPointer, '\Override')) {
5247
return;
5348
}
5449

@@ -78,21 +73,4 @@ private function getSniffName(string $sniffName): string
7873
return sprintf('%s.%s', self::NAME, $sniffName);
7974
}
8075

81-
private function targetHasOverrideAttribute(File $phpcsFile, int $docCommentOpenPointer): bool
82-
{
83-
$tokens = $phpcsFile->getTokens();
84-
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $docCommentOpenPointer + 1);
85-
86-
if ($nextPointer === null || $tokens[$nextPointer]['code'] !== T_ATTRIBUTE) {
87-
return false;
88-
}
89-
90-
$attributeNames = array_map(
91-
static fn (Attribute $name): string => $name->getFullyQualifiedName(),
92-
AttributeHelper::getAttributes($phpcsFile, $nextPointer),
93-
);
94-
95-
return in_array('\Override', $attributeNames, true);
96-
}
97-
9876
}

0 commit comments

Comments
 (0)