Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Symfony\Tests\Symfony44\Rector\ClassMethod\ConsoleExecuteReturnIntRector\Fixture;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ReturnNullWithIntReturnTypeCommand extends Command
{
public function execute(InputInterface $input, OutputInterface $output): int
{
return null;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony44\Rector\ClassMethod\ConsoleExecuteReturnIntRector\Fixture;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ReturnNullWithIntReturnTypeCommand extends Command
{
public function execute(InputInterface $input, OutputInterface $output): int
{
return 0;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ private function addReturn0ToExecuteClassMethod(ClassMethod $classMethod): void
return null;
}

$this->setReturnTo0InsteadOfNull($node);
if ($this->isSuccessfulRefactorReturn($node)) {
$this->hasChanged = true;
}

return null;
});
Expand Down Expand Up @@ -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
Expand Down
Loading