Skip to content

Commit 4ef8e3e

Browse files
committed
SlevomatCodingStandard.TypeHints.ReturnTypeHint: Don't report only some errors when method is inherited
1 parent ea06193 commit 4ef8e3e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ public function process(File $phpcsFile, int $pointer): void
110110
return;
111111
}
112112

113-
if (AttributeHelper::hasAttribute($phpcsFile, $pointer, '\Override')) {
114-
return;
115-
}
116-
117-
if (DocCommentHelper::hasInheritdocAnnotation($phpcsFile, $pointer)) {
118-
return;
119-
}
120-
121113
$token = $phpcsFile->getTokens()[$pointer];
122114

123115
if ($token['code'] === T_FUNCTION) {
@@ -150,6 +142,12 @@ private function checkFunctionTypeHint(
150142
array $prefixedReturnAnnotations
151143
): void
152144
{
145+
$isInherited = AttributeHelper::hasAttribute(
146+
$phpcsFile,
147+
$functionPointer,
148+
'\Override',
149+
) || DocCommentHelper::hasInheritdocAnnotation($phpcsFile, $functionPointer);
150+
153151
$suppressNameAnyTypeHint = $this->getSniffName(self::CODE_MISSING_ANY_TYPE_HINT);
154152
$isSuppressedAnyTypeHint = SuppressHelper::isSniffSuppressed($phpcsFile, $functionPointer, $suppressNameAnyTypeHint);
155153

@@ -237,7 +235,7 @@ private function checkFunctionTypeHint(
237235
return;
238236
}
239237

240-
if (!$isSuppressedAnyTypeHint) {
238+
if (!$isSuppressedAnyTypeHint && !$isInherited) {
241239
$phpcsFile->addError(
242240
sprintf(
243241
'%s %s() does not have return type hint nor @return annotation for its return value.',
@@ -259,7 +257,7 @@ private function checkFunctionTypeHint(
259257
|| $isAnnotationReturnTypeVoidOrNever
260258
)
261259
) {
262-
if (!$isSuppressedNativeTypeHint) {
260+
if (!$isSuppressedNativeTypeHint && !$isInherited) {
263261
$message = !$hasReturnAnnotation
264262
? sprintf(
265263
'%s %s() does not have void return type hint.',
@@ -293,7 +291,7 @@ private function checkFunctionTypeHint(
293291
return;
294292
}
295293

296-
if (!$isSuppressedNativeTypeHint && $returnsValue && $isAnnotationReturnTypeVoidOrNever) {
294+
if (!$isSuppressedNativeTypeHint && !$isInherited && $returnsValue && $isAnnotationReturnTypeVoidOrNever) {
297295
$message = sprintf(
298296
'%s %s() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "%s".',
299297
FunctionHelper::getTypeLabel($phpcsFile, $functionPointer),
@@ -444,7 +442,7 @@ private function checkFunctionTypeHint(
444442
$nullableReturnTypeHint = true;
445443
}
446444

447-
if ($isSuppressedNativeTypeHint) {
445+
if ($isSuppressedNativeTypeHint || $isInherited) {
448446
return;
449447
}
450448

@@ -497,6 +495,12 @@ private function checkFunctionTraversableTypeHintSpecification(
497495
array $prefixedReturnAnnotations
498496
): void
499497
{
498+
$isInherited = AttributeHelper::hasAttribute(
499+
$phpcsFile,
500+
$functionPointer,
501+
'\Override',
502+
) || DocCommentHelper::hasInheritdocAnnotation($phpcsFile, $functionPointer);
503+
500504
$suppressName = $this->getSniffName(self::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
501505
$isSuppressed = SuppressHelper::isSniffSuppressed($phpcsFile, $functionPointer, $suppressName);
502506

@@ -510,7 +514,7 @@ private function checkFunctionTraversableTypeHintSpecification(
510514
return;
511515
}
512516

513-
if (!$isSuppressed) {
517+
if (!$isSuppressed && !$isInherited) {
514518
$phpcsFile->addError(
515519
sprintf(
516520
'%s %s() does not have @return annotation for its traversable return value.',
@@ -551,7 +555,7 @@ private function checkFunctionTraversableTypeHintSpecification(
551555
return;
552556
}
553557

554-
if ($isSuppressed) {
558+
if ($isSuppressed || $isInherited) {
555559
return;
556560
}
557561

0 commit comments

Comments
 (0)