Skip to content

Commit 72b4009

Browse files
vranakukulich
authored andcommitted
ReturnTypeHint: report missing type hint in abstract methods
1 parent c9b154a commit 72b4009

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private function checkFunctionTypeHint(
205205
? ($hasReturnAnnotation && !$isAnnotationReturnTypeVoidOrNever)
206206
: FunctionHelper::returnsValue($phpcsFile, $functionPointer);
207207

208-
if ($returnsValue && !$hasReturnAnnotation) {
208+
if (($returnsValue || $isAbstract) && !$hasReturnAnnotation) {
209209
if (count($prefixedReturnAnnotations) !== 0) {
210210
$this->reportUselessSuppress($phpcsFile, $functionPointer, $isSuppressedAnyTypeHint, $suppressNameAnyTypeHint);
211211
return;

tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testErrors(): void
3333
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
3434
]);
3535

36-
self::assertSame(65, $report->getErrorCount());
36+
self::assertSame(67, $report->getErrorCount());
3737

3838
self::assertSniffError($report, 6, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
3939
self::assertSniffError($report, 14, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -108,6 +108,9 @@ public function testErrors(): void
108108
self::assertSniffError($report, 383, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
109109
self::assertSniffError($report, 388, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
110110

111+
self::assertSniffError($report, 392, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
112+
self::assertSniffError($report, 397, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
113+
111114
self::assertAllFixedInFile($report);
112115
}
113116

tests/Sniffs/TypeHints/data/returnTypeHintErrors.fixed.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,10 @@ public function returnNonEmptyArray(): array
376376
{
377377
}
378378

379+
abstract public function returnWhoKnows();
380+
381+
}
382+
383+
interface Whateverable {
384+
function returnWhoKnows();
379385
}

tests/Sniffs/TypeHints/data/returnTypeHintErrors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,10 @@ public function returnNonEmptyArray()
389389
{
390390
}
391391

392+
abstract public function returnWhoKnows();
393+
394+
}
395+
396+
interface Whateverable {
397+
function returnWhoKnows();
392398
}

0 commit comments

Comments
 (0)