Skip to content

Commit

Permalink
Fix false positive for a null return on mixed function
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Nov 15, 2023
1 parent 23d06fc commit 76e446e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/return-type-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

use Psr\Container\ContainerInterface as PsrContainer;

function returnMixed(): mixed
{
return null;
}

add_filter('x', function () {
return '';
});
Expand Down Expand Up @@ -218,6 +223,12 @@ function genMultiReturn(): \Generator
return 2;
}

// @phpcsErrorCodeOnNextLine IncorrectVoidReturn
function returnMixed2(): mixed
{
return;
}

// @phpcsErrorCodeOnNextLine IncorrectVoidReturnType
add_filter('x', function (): void {
return '0';
Expand Down

0 comments on commit 76e446e

Please sign in to comment.