Skip to content

Commit 1e60915

Browse files
committed
TypeHintDeclarationSniff: return annotation is not useless when return type hint is missing
1 parent 1741355 commit 1e60915

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/TypeHintDeclarationSniff.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,6 @@ private function typeHintEqualsAnnotation(\PHP_CodeSniffer\Files\File $phpcsFile
948948

949949
private function isReturnAnnotationUseless(\PHP_CodeSniffer\Files\File $phpcsFile, int $functionPointer, ?ReturnTypeHint $returnTypeHint = null, ?Annotation $returnAnnotation = null): bool
950950
{
951-
if (!FunctionHelper::isAbstract($phpcsFile, $functionPointer) && !FunctionHelper::returnsValue($phpcsFile, $functionPointer) && $returnTypeHint === null) {
952-
return true;
953-
}
954-
955951
if ($returnTypeHint === null || $returnAnnotation === null || $returnAnnotation->getContent() === null) {
956952
return false;
957953
}

tests/Sniffs/TypeHints/TypeHintDeclarationSniffTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,17 @@ public function testEnabledVoidTypeHintErrors(): void
145145
'enableObjectTypeHint' => false,
146146
]);
147147

148-
self::assertSame(8, $report->getErrorCount());
148+
self::assertSame(9, $report->getErrorCount());
149149

150150
self::assertSniffError($report, 3, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
151151
self::assertSniffError($report, 14, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
152152
self::assertSniffError($report, 16, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
153153
self::assertSniffError($report, 24, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);
154-
self::assertSniffError($report, 31, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
155-
self::assertSniffError($report, 35, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
154+
self::assertSniffError($report, 32, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
156155
self::assertSniffError($report, 39, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
157-
self::assertSniffError($report, 51, TypeHintDeclarationSniff::CODE_INCORRECT_RETURN_TYPE_HINT);
156+
self::assertSniffError($report, 43, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
157+
self::assertSniffError($report, 47, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
158+
self::assertSniffError($report, 59, TypeHintDeclarationSniff::CODE_INCORRECT_RETURN_TYPE_HINT);
158159
}
159160

160161
public function testEnabledNullableTypeHintsNoErrors(): void

tests/Sniffs/TypeHints/data/enabledVoidTypeHintErrors.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ public function method()
2121
/**
2222
* @return void
2323
*/
24-
public function withSuppress(): void
24+
public function bothReturnTypeAndAnnotation(): void
2525
{
2626
return;
2727
}
2828

29+
/**
30+
* @return void
31+
*/
32+
public function onlyReturnAnnotation()
33+
{
34+
35+
}
36+
2937
}
3038

3139
function () {

0 commit comments

Comments
 (0)