diff --git a/rules-tests/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector/Fixture/return_null_with_int_return_type.php.inc b/rules-tests/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector/Fixture/return_null_with_int_return_type.php.inc new file mode 100644 index 00000000..5d9e9319 --- /dev/null +++ b/rules-tests/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector/Fixture/return_null_with_int_return_type.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/rules/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector.php b/rules/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector.php index e684d1b3..2003d4ee 100644 --- a/rules/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector.php +++ b/rules/Symfony44/Rector/ClassMethod/ConsoleExecuteReturnIntRector.php @@ -145,7 +145,9 @@ private function addReturn0ToExecuteClassMethod(ClassMethod $classMethod): void return null; } - $this->setReturnTo0InsteadOfNull($node); + if ($this->isSuccessfulRefactorReturn($node)) { + $this->hasChanged = true; + } return null; }); @@ -204,42 +206,43 @@ private function processReturn0ToMethod(ClassMethod $classMethod): void } $classMethod->stmts[] = $return; + $this->hasChanged = true; } - private function setReturnTo0InsteadOfNull(Return_ $return): void + private function isSuccessfulRefactorReturn(Return_ $return): bool { if (! $return->expr instanceof Expr) { $return->expr = new \PhpParser\Node\Scalar\Int_(0); - return; + return true; } if ($this->valueResolver->isNull($return->expr)) { $return->expr = new \PhpParser\Node\Scalar\Int_(0); - return; + return true; } // false means the command failed, that is the 1 exit code if ($this->valueResolver->isFalse($return->expr)) { $return->expr = new \PhpParser\Node\Scalar\Int_(1); - return; + return true; } if ($return->expr instanceof Coalesce && $this->valueResolver->isNull($return->expr->right)) { $return->expr->right = new \PhpParser\Node\Scalar\Int_(0); - return; + return true; } - if ($return->expr instanceof Ternary) { - $hasChanged = $this->isSuccessfulRefactorTernaryReturn($return->expr); - if ($hasChanged) { - return; - } + if ($return->expr instanceof Ternary && $this->isSuccessfulRefactorTernaryReturn($return->expr)) { + return true; } $staticType = $this->getType($return->expr); if (! $staticType->isInteger()->yes()) { $return->expr = new Int_($return->expr); + return true; } + + return false; } private function isSuccessfulRefactorTernaryReturn(Ternary $ternary): bool