From 4a77565302216f6fde71232600cd175422a00965 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 5 Apr 2024 20:02:11 +0200 Subject: [PATCH] ExplicitReturnNullRector should skip never return (#5802) * ExplicitReturnNullRector should skip never return * cs * cs --- .../Fixture/skip-never-return.php.inc | 21 +++++++++++++++++++ .../ClassMethod/ExplicitReturnNullRector.php | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip-never-return.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip-never-return.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip-never-return.php.inc new file mode 100644 index 00000000000..3038a73bf54 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector/Fixture/skip-never-return.php.inc @@ -0,0 +1,21 @@ +neverReturns(); + } + + private function neverReturns($value): never + { + exit(); + } +} +?> diff --git a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php index b8fdb1be397..53e16eaa272 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php @@ -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; @@ -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 ) { } @@ -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;