Skip to content

Commit

Permalink
ReturnTypeHintSniff: It knows unofficial "void" type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jul 9, 2021
1 parent 751e737 commit 4607562
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion SlevomatCodingStandard/Helpers/TypeHintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public static function isUnofficialUnionTypeHint(string $typeHint): bool
return in_array($typeHint, ['scalar', 'numeric'], true);
}

public static function isVoidTypeHint(string $typeHint): bool
{
return in_array($typeHint, ['void', 'never', 'never-return', 'never-returns', 'no-return'], true);
}

/**
* @param string $typeHint
* @return string[]
Expand Down Expand Up @@ -358,7 +363,7 @@ private static function normalize(string $typeHint): string

if (count($convertedParts) > 1) {
$convertedParts = array_map(static function (string $part): string {
return $part === 'void' ? 'null' : $part;
return TypeHintHelper::isVoidTypeHint($part) ? 'null' : $part;
}, $convertedParts);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ private function checkFunctionTypeHint(

$hasReturnAnnotation = $this->hasReturnAnnotation($returnAnnotation);
$returnTypeNode = $this->getReturnTypeNode($returnAnnotation);
$isAnnotationReturnTypeVoid = $returnTypeNode instanceof IdentifierTypeNode && strtolower($returnTypeNode->name) === 'void';
$isAnnotationReturnTypeVoid = $returnTypeNode instanceof IdentifierTypeNode && TypeHintHelper::isVoidTypeHint(
strtolower($returnTypeNode->name)
);
$isAbstract = FunctionHelper::isAbstract($phpcsFile, $functionPointer);
$returnsValue = $isAbstract
? ($hasReturnAnnotation && !$isAnnotationReturnTypeVoid)
Expand Down Expand Up @@ -353,7 +355,7 @@ private function checkFunctionTypeHint(
return;
}

$typeHintsWithConvertedUnion[$typeHintNo] = $typeHint === 'void'
$typeHintsWithConvertedUnion[$typeHintNo] = TypeHintHelper::isVoidTypeHint($typeHint)
? 'null'
: TypeHintHelper::convertLongSimpleTypeHintToShort($typeHint);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testErrors(): void
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
]);

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

self::assertSniffError($report, 6, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
self::assertSniffError($report, 14, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand Down Expand Up @@ -82,6 +82,7 @@ public function testErrors(): void
self::assertSniffError($report, 298, ReturnTypeHintSniff::CODE_USELESS_SUPPRESS);

self::assertSniffError($report, 305, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
self::assertSniffError($report, 313, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);

self::assertAllFixedInFile($report);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Sniffs/TypeHints/data/returnTypeHintErrors.fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,11 @@ public function voidButReturnsValue()
return true;
}

/**
* @return no-return
*/
public function noReturnTypeHint(): void
{
}

}
7 changes: 7 additions & 0 deletions tests/Sniffs/TypeHints/data/returnTypeHintErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,11 @@ public function voidButReturnsValue()
return true;
}

/**
* @return no-return
*/
public function noReturnTypeHint()
{
}

}

0 comments on commit 4607562

Please sign in to comment.