Fix binary operations when necessary

This commit is contained in:
Gabriel Caruso 2018-05-13 15:19:51 -03:00
parent 4b7d41b4ce
commit 3969a4e845
No known key found for this signature in database
GPG Key ID: D93D6E258EFC438A
3 changed files with 86 additions and 11 deletions

View File

@ -13,6 +13,16 @@ use Rector\RectorDefinition\RectorDefinition;
final class UnnecessaryTernaryExpressionRector extends AbstractRector
{
/**
* @var string
*/
private $ifValue;
/**
* @var string
*/
private $elseValue;
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
@ -48,10 +58,10 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector
return false;
}
$ifConstFetch = $ifExpression->name->toLowerString();
$elseConstFetch = $elseExpression->name->toLowerString();
$this->ifValue = $ifExpression->name->toLowerString();
$this->elseValue = $elseExpression->name->toLowerString();
return ! in_array('null', [$ifConstFetch, $elseConstFetch], true);
return ! in_array('null', [$this->ifValue, $this->elseValue], true);
}
/**
@ -59,8 +69,35 @@ final class UnnecessaryTernaryExpressionRector extends AbstractRector
*/
public function refactor(Node $ternaryExpression): ?Node
{
$ternaryExpression = $ternaryExpression->cond;
/** @var BinaryOp */
$binaryOpOperation = $ternaryExpression->cond;
if ($this->ifValue === 'true' && $this->elseValue === 'false') {
$ternaryExpression = $binaryOpOperation;
} else {
$ternaryExpression = $this->fixBinaryOperation($binaryOpOperation);
}
return $ternaryExpression;
}
private function fixBinaryOperation(BinaryOp $operation): BinaryOp
{
$operandsMap = [
'===' => 'NotIdentical',
'!==' => 'Identical',
'==' => 'NotEqual',
'!=' => 'Equal',
'<>' => 'Equal',
'>' => 'Smaller',
'<' => 'Greater',
'>=' => 'SmallerOrEqual',
'<=' => 'GreaterOrEqual',
];
$operand = $operation->getOperatorSigil();
$binaryOpClassName = 'PhpParser\\Node\\Expr\\BinaryOp\\' . $operandsMap[$operand];
return new $binaryOpClassName($operation->left, $operation->right);
}
}

View File

@ -2,15 +2,34 @@
final class MyClass
{
public function unecessaryTernary(): bool
public function unnecessaryTernary(): bool
{
return $foo === $bar;
}
public function unecessaryTernaryExpression(): bool
public function unnecessaryTernaryExpression(): bool
{
$return = $foo === $bar;
return $return;
}
public function unnecessaryTernaryExpressionInverted(): bool
{
return $foo <= $bar;
}
public function ternaryExpressionInsideClosure(): bool
{
$result = function (): bool {
return $foo === 2;
};
return $result;
}
public function necessaryTernaryExpression(): bool
{
return $foo ?: $bar;
}
}

View File

@ -2,17 +2,36 @@
final class MyClass
{
public function unecessaryTernary(): bool
public function unnecessaryTernary(): bool
{
return $foo === $bar ? true : false;
}
public function unecessaryTernaryExpression(): bool
public function unnecessaryTernaryExpression(): bool
{
$return = $foo === $bar
? true
: false;
$return = $foo !== $bar
? false
: true;
return $return;
}
public function unnecessaryTernaryExpressionInverted(): bool
{
return $foo >= $bar ? false : true;
}
public function ternaryExpressionInsideClosure(): bool
{
$result = function (): bool {
return $foo !== 2 ? false : true;
};
return $result;
}
public function necessaryTernaryExpression(): bool
{
return $foo ?: $bar;
}
}