Skip to content

Commit 287f803

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

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
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+
public function overrideAttribute($a): void
10+
{
11+
}
12+
13+
}
14+
15+
class Whatever extends ParentClass
616
{
717

818
/**
@@ -276,6 +286,15 @@ public function brokenParameterDescription(int $a, array $b)
276286
{
277287
}
278288

289+
/**
290+
* @param bool $a
291+
*/
292+
#[Override]
293+
public function overrideAttribute($a): void
294+
{
295+
}
296+
297+
279298
}
280299

281300
/**

0 commit comments

Comments
 (0)