mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Fix binary operations when necessary
This commit is contained in:
parent
4b7d41b4ce
commit
3969a4e845
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user