mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-16 08:02:23 +00:00
[CodeQuality] Avoid double negation on UnnecessaryTernaryExpressionRector (#6115)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
parent
69e9da22d1
commit
620309c0d7
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class MultipleNegationArray
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return !! $data ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class MultipleNegationArray
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return ! $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class MultipleNegationArray2
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return !! empty($data) ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class MultipleNegationArray2
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return ! empty($data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class MultipleNegationBool
|
||||
{
|
||||
public function run(bool $data)
|
||||
{
|
||||
return !! $data ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class MultipleNegationBool
|
||||
{
|
||||
public function run(bool $data)
|
||||
{
|
||||
return ! $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class NegationArray
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return ! $data ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class NegationArray
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return (bool) $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class NegationArray2
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return ! empty($data) ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class NegationArray2
|
||||
{
|
||||
public function run(array $data)
|
||||
{
|
||||
return empty($data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class NegationBool
|
||||
{
|
||||
public function run(bool $data)
|
||||
{
|
||||
return ! $data ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\Fixture;
|
||||
|
||||
final class NegationBool
|
||||
{
|
||||
public function run(bool $data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -97,21 +97,40 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector
|
|||
private function processNonBinaryCondition(Expr $ifExpression, Expr $elseExpression, Expr $condition): ?Node
|
||||
{
|
||||
if ($this->valueResolver->isTrue($ifExpression) && $this->valueResolver->isFalse($elseExpression)) {
|
||||
if ($this->nodeTypeResolver->isStaticType($condition, BooleanType::class)) {
|
||||
return $condition;
|
||||
}
|
||||
return $this->processTrueIfExpressionWithFalseElseExpression($condition);
|
||||
}
|
||||
if (! $this->valueResolver->isFalse($ifExpression)) {
|
||||
return null;
|
||||
}
|
||||
if (! $this->valueResolver->isTrue($elseExpression)) {
|
||||
return null;
|
||||
}
|
||||
return $this->processFalseIfExpressionWithTrueElseExpression($condition);
|
||||
}
|
||||
|
||||
return new Bool_($condition);
|
||||
private function processTrueIfExpressionWithFalseElseExpression(Expr $expr): Expr
|
||||
{
|
||||
if ($this->nodeTypeResolver->isStaticType($expr, BooleanType::class)) {
|
||||
return $expr;
|
||||
}
|
||||
|
||||
if ($this->valueResolver->isFalse($ifExpression) && $this->valueResolver->isTrue($elseExpression)) {
|
||||
if ($this->nodeTypeResolver->isStaticType($condition, BooleanType::class)) {
|
||||
return new BooleanNot($condition);
|
||||
return new Bool_($expr);
|
||||
}
|
||||
|
||||
private function processFalseIfExpressionWithTrueElseExpression(Expr $expr): Expr
|
||||
{
|
||||
if ($expr instanceof BooleanNot) {
|
||||
if ($this->nodeTypeResolver->isStaticType($expr->expr, BooleanType::class)) {
|
||||
return $expr->expr;
|
||||
}
|
||||
|
||||
return new BooleanNot(new Bool_($condition));
|
||||
return new Bool_($expr->expr);
|
||||
}
|
||||
|
||||
return null;
|
||||
if ($this->nodeTypeResolver->isStaticType($expr, BooleanType::class)) {
|
||||
return new BooleanNot($expr);
|
||||
}
|
||||
|
||||
return new BooleanNot(new Bool_($expr));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user