mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-01 23:23:30 +00:00
[Symfony4] Skip ConsoleExecuteReturnIntRector on ternary returns number (#5735)
This commit is contained in:
parent
896a87c631
commit
8430a8d73a
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Rector\Symfony4\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
||||
use PhpParser\Node\Expr\Cast\Int_;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
|
@ -110,10 +111,7 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof Return_ && $this->nodeComparator->areNodesEqual(
|
||||
$parentNode->expr,
|
||||
$node
|
||||
) && $node instanceof Int_) {
|
||||
if ($this->isAReturnWithExprIntEquals($parentNode, $node)) {
|
||||
$hasReturn = true;
|
||||
return null;
|
||||
}
|
||||
|
@ -126,8 +124,12 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($node->expr instanceof Ternary && $this->isIntegerTernaryIfElse($node->expr)) {
|
||||
$hasReturn = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
// is there return without nesting?
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($this->nodeComparator->areNodesEqual($parentNode, $classMethod)) {
|
||||
$hasReturn = true;
|
||||
}
|
||||
|
@ -137,6 +139,23 @@ CODE_SAMPLE
|
|||
return null;
|
||||
});
|
||||
|
||||
$this->processReturn0ToMethod($hasReturn, $classMethod);
|
||||
}
|
||||
|
||||
private function isIntegerTernaryIfElse(Ternary $ternary): bool
|
||||
{
|
||||
/** @var Expr $if */
|
||||
$if = $ternary->if;
|
||||
/** @var Expr $else */
|
||||
$else = $ternary->else;
|
||||
$ifType = $this->getStaticType($if);
|
||||
$elseType = $this->getStaticType($else);
|
||||
|
||||
return $ifType instanceof IntegerType && $elseType instanceof IntegerType;
|
||||
}
|
||||
|
||||
private function processReturn0ToMethod(bool $hasReturn, ClassMethod $classMethod): void
|
||||
{
|
||||
if ($hasReturn) {
|
||||
return;
|
||||
}
|
||||
|
@ -144,6 +163,19 @@ CODE_SAMPLE
|
|||
$classMethod->stmts[] = new Return_(new LNumber(0));
|
||||
}
|
||||
|
||||
private function isAReturnWithExprIntEquals(?Node $parentNode, Node $node): bool
|
||||
{
|
||||
if (! $parentNode instanceof Return_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->nodeComparator->areNodesEqual($parentNode->expr, $node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $node instanceof Int_;
|
||||
}
|
||||
|
||||
private function setReturnTo0InsteadOfNull(Return_ $return): void
|
||||
{
|
||||
if ($return->expr === null) {
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Symfony4\Tests\Rector\ClassMethod\ConsoleExecuteReturnIntRector\Fixture;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class SkipTernaryReturnInt extends Command
|
||||
{
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
return rand(0, 1) ? 0 : 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Symfony4\Tests\Rector\ClassMethod\ConsoleExecuteReturnIntRector\Fixture;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
function returns_bool()
|
||||
{
|
||||
return rand(0, 1) === 0;
|
||||
}
|
||||
|
||||
final class SkipTernaryReturnIntVariable extends Command
|
||||
{
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$boolVariable = returns_bool();
|
||||
return $boolVariable ? 0 : 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Symfony4\Tests\Rector\ClassMethod\ConsoleExecuteReturnIntRector\Fixture;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class SkipTernaryReturnIntVariableAssignBool extends Command
|
||||
{
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$boolVariable = true;
|
||||
return $boolVariable ? 0 : 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Symfony4\Tests\Rector\ClassMethod\ConsoleExecuteReturnIntRector\Fixture;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class SkipTernaryReturnIntVariableInIfElse extends Command
|
||||
{
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$yes = 0;
|
||||
$no = 1;
|
||||
return rand(0, 1) ? $yes : $no;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user