Skip to content

Commit

Permalink
ExplicitReturnNullRector should skip never return (#5802)
Browse files Browse the repository at this point in the history
* ExplicitReturnNullRector should skip never return

* cs

* cs
  • Loading branch information
staabm committed Apr 5, 2024
1 parent a16a04d commit 4a77565
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\Fixture;

final class SkipNeverReturn
{
public function run(bool $param)
{
if (rand(0,1)) {
return 5;
}

$this->neverReturns();
}

private function neverReturns($value): never
{
exit();
}
}
?>
Expand Up @@ -21,6 +21,7 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\NodeAnalyzer\NeverFuncCallAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\ValueObject\MethodName;
Expand All @@ -37,6 +38,7 @@ public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly TypeFactory $typeFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly NeverFuncCallAnalyzer $neverFuncCallAnalyzer,
private readonly ReturnTypeInferer $returnTypeInferer
) {
}
Expand Down Expand Up @@ -129,7 +131,9 @@ public function refactor(Node $node): ?Node
return null;
});

if (! $this->silentVoidResolver->hasSilentVoid($node)) {
if (! $this->silentVoidResolver->hasSilentVoid($node)
|| $this->neverFuncCallAnalyzer->hasNeverFuncCall($node)
) {
if ($hasChanged) {
$this->transformDocUnionVoidToUnionNull($node);
return $node;
Expand Down

0 comments on commit 4a77565

Please sign in to comment.