Skip to content

Commit cda88e6

Browse files
committed
SlevomatCodingStandard.TypeHints.ParameterTypeHint: Don't report missing native type hint when method has #[Override] attribute
1 parent 31ed4d5 commit cda88e6

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

SlevomatCodingStandard/Helpers/AttributeHelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ class AttributeHelper
4242
T_VARIABLE,
4343
];
4444

45-
public static function hasAttribute(File $phpcsFile, int $docCommentOpenPointer, string $attributeName): bool
45+
public static function hasAttribute(File $phpcsFile, int $pointer, string $attributeName): bool
4646
{
47+
$docCommentOpenPointer = DocCommentHelper::findDocCommentOpenPointer($phpcsFile, $pointer);
48+
if ($docCommentOpenPointer === null) {
49+
return false;
50+
}
51+
4752
$tokens = $phpcsFile->getTokens();
4853
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $docCommentOpenPointer + 1);
4954

SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use SlevomatCodingStandard\Helpers\Annotation;
2323
use SlevomatCodingStandard\Helpers\AnnotationHelper;
2424
use SlevomatCodingStandard\Helpers\AnnotationTypeHelper;
25+
use SlevomatCodingStandard\Helpers\AttributeHelper;
2526
use SlevomatCodingStandard\Helpers\DocCommentHelper;
2627
use SlevomatCodingStandard\Helpers\FixerHelper;
2728
use SlevomatCodingStandard\Helpers\FunctionHelper;
@@ -209,6 +210,10 @@ private function checkTypeHints(
209210
continue;
210211
}
211212

213+
if (AttributeHelper::hasAttribute($phpcsFile, $functionPointer, '\Override')) {
214+
continue;
215+
}
216+
212217
$parameterTypeNode = $parametersAnnotations[$parameterName]->getValue()->type;
213218

214219
if (

tests/Sniffs/TypeHints/data/parameterTypeHintNoErrors.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
<?php // lint >= 8.0
22

33
use Doctrine\Common\Collections\ArrayCollection;
4+
use Override;
45

5-
class Whatever
6+
class ParentClass
7+
{
8+
9+
/**
10+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
11+
*/
12+
public function overrideAttribute($a): void
13+
{
14+
}
15+
16+
}
17+
18+
class Whatever extends ParentClass
619
{
720

821
/**
@@ -276,6 +289,15 @@ public function brokenParameterDescription(int $a, array $b)
276289
{
277290
}
278291

292+
/**
293+
* @param bool $a
294+
*/
295+
#[Override]
296+
public function overrideAttribute($a): void
297+
{
298+
}
299+
300+
279301
}
280302

281303
/**

0 commit comments

Comments
 (0)