Skip to content

Commit fae26ee

Browse files
greg0irekukulich
authored andcommitted
Bail out earlier on #[Override]
- `SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification`: do not report missing native type hint when method has `#[Override]` attribute. - `SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint`: do not report missing native type hint when method has `#[Override]` attribute. This time I took a more holistic approach and search for occurrences of `DocCommentHelper::hasInheritdocAnnotation`, and checked if there was a similar check for the override attribute.
1 parent 4ef8e3e commit fae26ee

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ public function process(File $phpcsFile, int $functionPointer): void
111111
return;
112112
}
113113

114-
if (DocCommentHelper::hasInheritdocAnnotation($phpcsFile, $functionPointer)) {
115-
return;
116-
}
117-
118114
$parametersTypeHints = FunctionHelper::getParametersTypeHints($phpcsFile, $functionPointer);
119115
$parametersAnnotations = FunctionHelper::getValidParametersAnnotations($phpcsFile, $functionPointer);
120116
$prefixedParametersAnnotations = FunctionHelper::getValidPrefixedParametersAnnotations($phpcsFile, $functionPointer);
@@ -143,6 +139,9 @@ private function checkTypeHints(
143139
array $prefixedParametersAnnotations
144140
): void
145141
{
142+
$isInherited = AttributeHelper::hasAttribute($phpcsFile, $functionPointer, '\Override')
143+
|| DocCommentHelper::hasInheritdocAnnotation($phpcsFile, $functionPointer);
144+
146145
$suppressNameAnyTypeHint = self::getSniffName(self::CODE_MISSING_ANY_TYPE_HINT);
147146
$isSuppressedAnyTypeHint = SuppressHelper::isSniffSuppressed($phpcsFile, $functionPointer, $suppressNameAnyTypeHint);
148147

@@ -192,6 +191,10 @@ private function checkTypeHints(
192191
continue;
193192
}
194193

194+
if ($isInherited) {
195+
continue;
196+
}
197+
195198
$phpcsFile->addError(
196199
sprintf(
197200
'%s %s() does not have parameter type hint nor @param annotation for its parameter %s.',
@@ -206,10 +209,6 @@ private function checkTypeHints(
206209
continue;
207210
}
208211

209-
if (AttributeHelper::hasAttribute($phpcsFile, $functionPointer, '\Override')) {
210-
continue;
211-
}
212-
213212
$parameterTypeNode = $parametersAnnotations[$parameterName]->getValue()->type;
214213

215214
if (
@@ -363,6 +362,10 @@ private function checkTypeHints(
363362
continue;
364363
}
365364

365+
if ($isInherited) {
366+
continue;
367+
}
368+
366369
$fix = $phpcsFile->addFixableError(
367370
sprintf(
368371
'%s %s() does not have native type hint for its parameter %s but it should be possible to add it based on @param annotation "%s".',
@@ -456,6 +459,9 @@ private function checkTraversableTypeHintSpecification(
456459
array $prefixedParametersAnnotations
457460
): void
458461
{
462+
$isInherited = AttributeHelper::hasAttribute($phpcsFile, $functionPointer, '\Override')
463+
|| DocCommentHelper::hasInheritdocAnnotation($phpcsFile, $functionPointer);
464+
459465
$suppressName = self::getSniffName(self::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
460466
$isSniffSuppressed = SuppressHelper::isSniffSuppressed($phpcsFile, $functionPointer, $suppressName);
461467
$suppressUseless = true;
@@ -490,7 +496,7 @@ private function checkTraversableTypeHintSpecification(
490496
if ($hasTraversableTypeHint && !array_key_exists($parameterName, $parametersAnnotations)) {
491497
$suppressUseless = false;
492498

493-
if (!$isSniffSuppressed) {
499+
if (!$isSniffSuppressed && !$isInherited) {
494500
$phpcsFile->addError(
495501
sprintf(
496502
'%s %s() does not have @param annotation for its traversable parameter %s.',
@@ -538,7 +544,7 @@ private function checkTraversableTypeHintSpecification(
538544

539545
$suppressUseless = false;
540546

541-
if ($isSniffSuppressed) {
547+
if ($isSniffSuppressed || $isInherited) {
542548
continue;
543549
}
544550

SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ private function checkTypeHint(
184184
array $prefixedPropertyAnnotations
185185
): void
186186
{
187+
$isInherited = AttributeHelper::hasAttribute($phpcsFile, $propertyPointer, '\Override');
188+
187189
$suppressNameAnyTypeHint = $this->getSniffName(self::CODE_MISSING_ANY_TYPE_HINT);
188190
$isSuppressedAnyTypeHint = SuppressHelper::isSniffSuppressed($phpcsFile, $propertyPointer, $suppressNameAnyTypeHint);
189191

@@ -204,7 +206,7 @@ private function checkTypeHint(
204206

205207
if (
206208
!$isSuppressedAnyTypeHint
207-
&& !AttributeHelper::hasAttribute($phpcsFile, $propertyPointer, '\Override')
209+
&& !$isInherited
208210
) {
209211
$phpcsFile->addError(
210212
sprintf(
@@ -225,10 +227,6 @@ private function checkTypeHint(
225227
return;
226228
}
227229

228-
if (AttributeHelper::hasAttribute($phpcsFile, $propertyPointer, '\Override')) {
229-
return;
230-
}
231-
232230
$typeNode = $propertyAnnotation->getValue()->type;
233231
$originalTypeNode = $typeNode;
234232
if ($typeNode instanceof NullableTypeNode) {
@@ -365,7 +363,7 @@ private function checkTypeHint(
365363
$nullableTypeHint = true;
366364
}
367365

368-
if ($isSuppressedNativeTypeHint) {
366+
if ($isSuppressedNativeTypeHint || $isInherited) {
369367
return;
370368
}
371369

@@ -429,6 +427,8 @@ private function checkTraversableTypeHintSpecification(
429427
array $prefixedPropertyAnnotations
430428
): void
431429
{
430+
$isInherited = AttributeHelper::hasAttribute($phpcsFile, $propertyPointer, '\Override');
431+
432432
$suppressName = $this->getSniffName(self::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
433433
$isSuppressed = SuppressHelper::isSniffSuppressed($phpcsFile, $propertyPointer, $suppressName);
434434

@@ -444,7 +444,7 @@ private function checkTraversableTypeHintSpecification(
444444

445445
if (
446446
!$isSuppressed
447-
&& !AttributeHelper::hasAttribute($phpcsFile, $propertyPointer, '\Override')
447+
&& !$isInherited
448448
) {
449449
$phpcsFile->addError(
450450
sprintf(

tests/Sniffs/TypeHints/data/parameterTypeHintNoErrors.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class ParentClass
88

99
/**
1010
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
11+
* @param array{} $b
1112
*/
12-
public function overrideAttribute($a): void
13+
public function overrideAttribute($a, array $b): void
1314
{
1415
}
1516

@@ -293,7 +294,7 @@ public function brokenParameterDescription(int $a, array $b)
293294
* @param bool $a
294295
*/
295296
#[Override]
296-
public function overrideAttribute($a): void
297+
public function overrideAttribute($a, array $b): void
297298
{
298299
}
299300

tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeNoErrors.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ class Whatever extends ParentClass
200200
*/
201201
public WeakMap $objectShapeInItems;
202202

203-
/**
204-
* @var string[]
205-
*/
206203
#[Override]
207204
public $arrayTypeHint = ['hello'];
208205

0 commit comments

Comments
 (0)