Skip to content

Commit 4607562

Browse files
committed
ReturnTypeHintSniff: It knows unofficial "void" type hints
1 parent 751e737 commit 4607562

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

SlevomatCodingStandard/Helpers/TypeHintHelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public static function isUnofficialUnionTypeHint(string $typeHint): bool
7676
return in_array($typeHint, ['scalar', 'numeric'], true);
7777
}
7878

79+
public static function isVoidTypeHint(string $typeHint): bool
80+
{
81+
return in_array($typeHint, ['void', 'never', 'never-return', 'never-returns', 'no-return'], true);
82+
}
83+
7984
/**
8085
* @param string $typeHint
8186
* @return string[]
@@ -358,7 +363,7 @@ private static function normalize(string $typeHint): string
358363

359364
if (count($convertedParts) > 1) {
360365
$convertedParts = array_map(static function (string $part): string {
361-
return $part === 'void' ? 'null' : $part;
366+
return TypeHintHelper::isVoidTypeHint($part) ? 'null' : $part;
362367
}, $convertedParts);
363368
}
364369

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ private function checkFunctionTypeHint(
164164

165165
$hasReturnAnnotation = $this->hasReturnAnnotation($returnAnnotation);
166166
$returnTypeNode = $this->getReturnTypeNode($returnAnnotation);
167-
$isAnnotationReturnTypeVoid = $returnTypeNode instanceof IdentifierTypeNode && strtolower($returnTypeNode->name) === 'void';
167+
$isAnnotationReturnTypeVoid = $returnTypeNode instanceof IdentifierTypeNode && TypeHintHelper::isVoidTypeHint(
168+
strtolower($returnTypeNode->name)
169+
);
168170
$isAbstract = FunctionHelper::isAbstract($phpcsFile, $functionPointer);
169171
$returnsValue = $isAbstract
170172
? ($hasReturnAnnotation && !$isAnnotationReturnTypeVoid)
@@ -353,7 +355,7 @@ private function checkFunctionTypeHint(
353355
return;
354356
}
355357

356-
$typeHintsWithConvertedUnion[$typeHintNo] = $typeHint === 'void'
358+
$typeHintsWithConvertedUnion[$typeHintNo] = TypeHintHelper::isVoidTypeHint($typeHint)
357359
? 'null'
358360
: TypeHintHelper::convertLongSimpleTypeHintToShort($typeHint);
359361
}

tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testErrors(): void
2727
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
2828
]);
2929

30-
self::assertSame(49, $report->getErrorCount());
30+
self::assertSame(50, $report->getErrorCount());
3131

3232
self::assertSniffError($report, 6, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
3333
self::assertSniffError($report, 14, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -82,6 +82,7 @@ public function testErrors(): void
8282
self::assertSniffError($report, 298, ReturnTypeHintSniff::CODE_USELESS_SUPPRESS);
8383

8484
self::assertSniffError($report, 305, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
85+
self::assertSniffError($report, 313, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
8586

8687
self::assertAllFixedInFile($report);
8788
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,11 @@ public function voidButReturnsValue()
297297
return true;
298298
}
299299

300+
/**
301+
* @return no-return
302+
*/
303+
public function noReturnTypeHint(): void
304+
{
305+
}
306+
300307
}

tests/Sniffs/TypeHints/data/returnTypeHintErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,11 @@ public function voidButReturnsValue()
307307
return true;
308308
}
309309

310+
/**
311+
* @return no-return
312+
*/
313+
public function noReturnTypeHint()
314+
{
315+
}
316+
310317
}

0 commit comments

Comments
 (0)