Skip to content

Commit 7bb28fe

Browse files
committed
ReturnTypeHintSniff: Fixed fixer for "void" in union type hint
1 parent c6a0a22 commit 7bb28fe

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

SlevomatCodingStandard/Helpers/TypeHintHelper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SlevomatCodingStandard\Helpers\Annotation\TypeAliasAnnotation;
88
use SlevomatCodingStandard\Helpers\Annotation\TypeImportAnnotation;
99
use function array_key_exists;
10+
use function array_map;
1011
use function array_merge;
1112
use function array_unique;
1213
use function count;
@@ -355,6 +356,12 @@ private static function normalize(string $typeHint): string
355356

356357
$convertedParts = array_unique($convertedParts);
357358

359+
if (count($convertedParts) > 1) {
360+
$convertedParts = array_map(static function (string $part): string {
361+
return $part === 'void' ? 'null' : $part;
362+
}, $convertedParts);
363+
}
364+
358365
sort($convertedParts);
359366

360367
return implode('|', $convertedParts);

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ private function checkFunctionTypeHint(
341341
return;
342342
}
343343

344-
$typeHintsWithConvertedUnion[$typeHintNo] = TypeHintHelper::convertLongSimpleTypeHintToShort($typeHint);
344+
$typeHintsWithConvertedUnion[$typeHintNo] = $typeHint === 'void'
345+
? 'null'
346+
: TypeHintHelper::convertLongSimpleTypeHintToShort($typeHint);
345347
}
346348

347349
if ($originalReturnTypeNode instanceof NullableTypeNode) {

tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function testErrors(): void
8787
public function testWithUnionNoErrors(): void
8888
{
8989
$report = self::checkFile(__DIR__ . '/data/returnTypeHintWithUnionNoErrors.php', [
90+
'enableObjectTypeHint' => true,
9091
'enableMixedTypeHint' => true,
9192
'enableUnionTypeHint' => true,
9293
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
@@ -97,12 +98,13 @@ public function testWithUnionNoErrors(): void
9798
public function testWithUnionErrors(): void
9899
{
99100
$report = self::checkFile(__DIR__ . '/data/returnTypeHintWithUnionErrors.php', [
101+
'enableObjectTypeHint' => true,
100102
'enableMixedTypeHint' => true,
101103
'enableUnionTypeHint' => true,
102104
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
103105
]);
104106

105-
self::assertSame(14, $report->getErrorCount());
107+
self::assertSame(15, $report->getErrorCount());
106108

107109
self::assertSniffError($report, 7, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
108110
self::assertSniffError($report, 11, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -119,6 +121,7 @@ public function testWithUnionErrors(): void
119121
self::assertSniffError($report, 51, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
120122
self::assertSniffError($report, 55, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
121123
self::assertSniffError($report, 59, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
124+
self::assertSniffError($report, 63, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
122125

123126
self::assertAllFixedInFile($report);
124127
}

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

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

62+
/***/
63+
private function objectAndVoid(): object|null
64+
{}
65+
6266
}

tests/Sniffs/TypeHints/data/returnTypeHintWithUnionErrors.php

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

62+
/** @return object|void */
63+
private function objectAndVoid()
64+
{}
65+
6266
}

0 commit comments

Comments
 (0)