make use of AssignAndBinaryMap

This commit is contained in:
Tomas Votruba 2018-11-07 18:22:27 +01:00
parent 71824ab33c
commit 9283ac30bf
6 changed files with 28 additions and 63 deletions

View File

@ -57,11 +57,7 @@ final class SimplifyConditionsRector extends AbstractRector
return $this->processBooleanNot($node);
}
if ($node instanceof Identical) {
return $this->processIdenticalAndNotIdentical($node);
}
return null;
return $this->processIdenticalAndNotIdentical($node);
}
private function processBooleanNot(BooleanNot $node): ?Node

View File

@ -5,37 +5,24 @@ namespace Rector\CodeQuality\Rector\Ternary;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Ternary;
use Rector\Exception\NotImplementedException;
use Rector\PhpParser\Node\AssignAndBinaryMap;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\sprintf;
final class UnnecessaryTernaryExpressionRector extends AbstractRector
{
/**
* @var string[]
* @var AssignAndBinaryMap
*/
private $inverseOperandMap = [
Identical::class => NotIdentical::class,
NotIdentical::class => Identical::class,
Equal::class => NotEqual::class,
NotEqual::class => Equal::class,
Greater::class => Smaller::class,
Smaller::class => Greater::class,
GreaterOrEqual::class => SmallerOrEqual::class,
SmallerOrEqual::class => GreaterOrEqual::class,
];
private $assignAndBinaryMap;
public function __construct(AssignAndBinaryMap $assignAndBinaryMap)
{
$this->assignAndBinaryMap = $assignAndBinaryMap;
}
public function getDefinition(): RectorDefinition
{
@ -86,27 +73,11 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector
return $binaryOperation;
}
return $this->inverseBinaryOperation($binaryOperation);
}
private function inverseBinaryOperation(BinaryOp $operation): BinaryOp
{
$this->ensureBinaryOperationIsSupported($operation);
$binaryOpClassName = $this->inverseOperandMap[get_class($operation)];
return new $binaryOpClassName($operation->left, $operation->right);
}
private function ensureBinaryOperationIsSupported(BinaryOp $operation): void
{
if (isset($this->inverseOperandMap[get_class($operation)])) {
return;
$inversedBinaryClass = $this->assignAndBinaryMap->getInversed($binaryOperation);
if ($inversedBinaryClass === null) {
return null;
}
throw new NotImplementedException(sprintf(
'"%s" type is not implemented yet. Add it in %s',
get_class($operation),
__METHOD__
));
return new $inversedBinaryClass($binaryOperation->left, $binaryOperation->right);
}
}

View File

@ -16,7 +16,7 @@ final class MyClass
public function unnecessaryTernaryExpressionInverted(): bool
{
return $foo <= $bar;
return $foo < $bar;
}
public function ternaryExpressionInsideClosure(): bool

View File

@ -46,9 +46,10 @@ final class DualCheckToAble
return null;
}
/** @var Instanceof_ $instanceOfNode */
/** @var FuncCall $funcCallNode */
[$instanceOfNode, $funcCallNode] = $matchedNodes;
/** @var Instanceof_ $instanceOfNode */
if ((string) $instanceOfNode->class !== $type) {
return null;
}

View File

@ -39,10 +39,7 @@ final class TernaryToNullCoalescingRector extends AbstractRector
public function refactor(Node $node): ?Node
{
if ($node->cond instanceof Isset_) {
$coalesceNode = $this->processTernaryWithIsset($node);
if ($coalesceNode) {
return $coalesceNode;
}
return $this->processTernaryWithIsset($node);
}
if ($node->cond instanceof Identical) {
@ -56,20 +53,21 @@ final class TernaryToNullCoalescingRector extends AbstractRector
/** @var Identical|NotIdentical $ternaryCompareNode */
$ternaryCompareNode = $node->cond;
if ($this->isNullMatch($ternaryCompareNode->left, $ternaryCompareNode->right, $checkedNode)) {
if ($this->isNullMatch($ternaryCompareNode->left, $ternaryCompareNode->right, $checkedNode) ||
$this->isNullMatch($ternaryCompareNode->right, $ternaryCompareNode->left, $checkedNode)
) {
return new Coalesce($checkedNode, $fallbackNode);
}
if ($this->isNullMatch($ternaryCompareNode->right, $ternaryCompareNode->left, $checkedNode)) {
return new Coalesce($checkedNode, $fallbackNode);
}
return $node;
return null;
}
private function processTernaryWithIsset(Ternary $ternaryNode): ?Coalesce
{
if ($ternaryNode->if === null) {
return null;
}
/** @var Isset_ $issetNode */
$issetNode = $ternaryNode->cond;
@ -78,10 +76,6 @@ final class TernaryToNullCoalescingRector extends AbstractRector
return null;
}
if ($ternaryNode->if === null) {
return null;
}
if ($this->areNodesEqual($ternaryNode->if, $issetNode->vars[0])) {
return new Coalesce($ternaryNode->if, $ternaryNode->else);
}

View File

@ -45,6 +45,7 @@ parameters:
- '#Method Rector\\Php\\Rector\\TryCatch\\MultiExceptionCatchRector\:\:collectCatchKeysByContent\(\) should return array<array<int\>\> but returns array<string, array<int, int\|string\>\>#'
- '#Method Rector\\NodeTypeResolver\\PhpDoc\\NodeAnalyzer\\DocBlockAnalyzer::getTagByName\(\) should return PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagNode but returns PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagNode\|null#'
- '#Parameter \#1 \$expr of class PhpParser\\Node\\Expr\\BooleanNot constructor expects PhpParser\\Node\\Expr, PhpParser\\Node given#'
- '#Parameter \#1 \$binaryOpNode of method Rector\\CodeQuality\\Rector\\Identical\\SimplifyConditionsRector::createInversedBooleanOp\(\) expects PhpParser\\Node\\Expr\\BinaryOp, PhpParser\\Node given#'
# irelevant
- '#Call to function in_array\(\) with arguments string, (.*?) and true will always evaluate to false#'
@ -65,6 +66,8 @@ parameters:
# - '#Access to an undefined property PhpParser\\Node::\$name#' # 11
- '#Access to an undefined property PhpParser\\Node\\Expr::\$expr#'
- '#Method Rector\\CodeQuality\\Rector\\Foreach_\\SimplifyForeachToCoalescingRector\:\:matchReturnOrAssignNode\(\) should return PhpParser\\Node\\Expr\\Assign\|PhpParser\\Node\\Stmt\\Return_\|null but returns PhpParser\\Node\|null#'
- '#Access to an undefined property PhpParser\\Node::\$(\w+)#'
- '#Parameter \#2 \$boolConstFetchNode of method Rector\\CodeQuality\\Rector\\Identical\\SimplifyArraySearchRector::resolveIsNot\(\) expects PhpParser\\Node\\Expr\\ConstFetch, PhpParser\\Node given#'
# false positive, resolved in previous method
- '#Parameter (.*?) of method Rector\\PhpParser\\Node\\Maintainer\\IdentifierMaintainer\:\:(.*?)\(\) expects PhpParser\\Node\\Expr\\ClassConstFetch\|PhpParser\\Node\\Expr\\MethodCall\|PhpParser\\Node\\Expr\\PropertyFetch\|PhpParser\\Node\\Expr\\StaticCall\|PhpParser\\Node\\Stmt\\ClassMethod, PhpParser\\Node given#'