Skip to content

Commit 61db21f

Browse files
kkmuffmekukulich
authored andcommitted
Fix void cannot be changed to null and never/void cannot be used in a union
Fix #1773
1 parent 03face2 commit 61db21f

File tree

5 files changed

+15
-22
lines changed

5 files changed

+15
-22
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,12 @@ private function checkFunctionTypeHint(
406406
return;
407407
}
408408

409-
$typeHintsWithConvertedUnion[$typeHintNo] = TypeHintHelper::isVoidTypeHint($typeHint)
410-
? 'null'
411-
: TypeHintHelper::convertLongSimpleTypeHintToShort($typeHint);
409+
if (TypeHintHelper::isVoidTypeHint($typeHint) || TypeHintHelper::isNeverTypeHint($typeHint)) {
410+
$this->reportUselessSuppress($phpcsFile, $functionPointer, $isSuppressedNativeTypeHint, $suppressNameNativeTypeHint);
411+
return;
412+
}
413+
414+
$typeHintsWithConvertedUnion[$typeHintNo] = TypeHintHelper::convertLongSimpleTypeHintToShort($typeHint);
412415
}
413416

414417
if ($originalReturnTypeNode instanceof NullableTypeNode) {

tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testWithUnionErrors(): void
137137
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
138138
]);
139139

140-
self::assertSame(17, $report->getErrorCount());
140+
self::assertSame(15, $report->getErrorCount());
141141

142142
self::assertSniffError($report, 7, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
143143
self::assertSniffError($report, 11, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -155,8 +155,6 @@ public function testWithUnionErrors(): void
155155
self::assertSniffError($report, 55, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
156156
self::assertSniffError($report, 59, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
157157
self::assertSniffError($report, 63, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
158-
self::assertSniffError($report, 67, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
159-
self::assertSniffError($report, 71, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
160158

161159
self::assertAllFixedInFile($report);
162160
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ private function numericNullable(): int|float|string|null
5959
private function scalarAndnumericNullable(): string|int|float|bool|null
6060
{}
6161

62-
/** */
63-
private function objectAndVoid(): object|null
64-
{}
65-
66-
/** */
67-
private function mixedAndVoid(): mixed
68-
{}
69-
7062
/** @return non-empty-array|null */
7163
public function returnNonEmptyArray(): ?array
7264
{}

tests/Sniffs/TypeHints/data/returnTypeHintWithUnionErrors.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ private function numericNullable()
5959
private function scalarAndnumericNullable()
6060
{}
6161

62-
/** @return object|void */
63-
private function objectAndVoid()
64-
{}
65-
66-
/** @return mixed|void */
67-
private function mixedAndVoid()
68-
{}
69-
7062
/** @return non-empty-array|null */
7163
public function returnNonEmptyArray()
7264
{}

tests/Sniffs/TypeHints/data/returnTypeHintWithUnionNoErrors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ public function returnCallableArray(): ?callable
4343
{
4444
}
4545

46+
/** @return bool|never */
47+
public function neverUnion() {
48+
}
49+
50+
/** @return bool|void */
51+
public function voidUnion() {
52+
}
53+
4654
}

0 commit comments

Comments
 (0)