diff --git a/Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php b/Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php index 48595e8..ae957f4 100644 --- a/Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php +++ b/Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php @@ -185,6 +185,12 @@ private function checkNonEmptyReturnTypes( array $returnInfo ): void { + if ($returnTypes === ['mixed']) { + $this->checkIsNotVoid($file, $position, $returnInfo); + + return; + } + if (($returnTypes === ['void']) || ($returnTypes === ['null'])) { $this->checkIsActualVoid($file, $position, $returnInfo, $returnTypes === ['null']); @@ -196,6 +202,30 @@ private function checkNonEmptyReturnTypes( || $this->checkIncorrectVoid($file, $position, $returnTypes, $returnInfo); } + /** + * @param File $file + * @param int $position + * @param array $returnInfo + * @param bool $checkNull + * @return void + */ + private function checkIsNotVoid( + File $file, + int $position, + array $returnInfo + ): void { + + if ($returnInfo['void'] === 0) { + return; + } + + $file->addError( + 'Return type is declared non-void but void return statement(s) found', + $position, + 'IncorrectVoidReturn' + ); + } + /** * @param File $file * @param int $position diff --git a/tests/fixtures/return-type-declaration.php b/tests/fixtures/return-type-declaration.php index a1d2c99..e70dde3 100644 --- a/tests/fixtures/return-type-declaration.php +++ b/tests/fixtures/return-type-declaration.php @@ -3,6 +3,11 @@ use Psr\Container\ContainerInterface as PsrContainer; +function returnMixed(): mixed +{ + return null; +} + add_filter('x', function () { return ''; }); @@ -218,6 +223,12 @@ function genMultiReturn(): \Generator return 2; } +// @phpcsErrorCodeOnNextLine IncorrectVoidReturn +function returnMixed2(): mixed +{ + return; +} + // @phpcsErrorCodeOnNextLine IncorrectVoidReturnType add_filter('x', function (): void { return '0';