2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2024-01-02 02:40:38 +00:00
|
|
|
namespace Rector\NodeManipulator;
|
2018-11-06 23:33:10 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Exception\ShouldNotHappenException;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Php71\ValueObject\TwoNodeMatch;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\AssignAndBinaryMap;
|
2019-02-27 21:54:39 +00:00
|
|
|
final class BinaryOpManipulator
|
2018-11-06 23:33:10 +00:00
|
|
|
{
|
2020-01-05 12:10:01 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\AssignAndBinaryMap
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
|
|
|
private $assignAndBinaryMap;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(AssignAndBinaryMap $assignAndBinaryMap)
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
|
|
|
}
|
2018-11-06 23:33:10 +00:00
|
|
|
/**
|
|
|
|
* Tries to match left or right parts (xor),
|
|
|
|
* returns null or match on first condition and then second condition. No matter what the origin order is.
|
|
|
|
*
|
2022-11-07 09:33:09 +00:00
|
|
|
* @param callable(Node $firstNode, Node $secondNode): bool|class-string<Node> $firstCondition
|
|
|
|
* @param callable(Node $firstNode, Node $secondNode): bool|class-string<Node> $secondCondition
|
2018-11-06 23:33:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function matchFirstAndSecondConditionNode(BinaryOp $binaryOp, $firstCondition, $secondCondition) : ?TwoNodeMatch
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2018-11-09 22:24:57 +00:00
|
|
|
$this->validateCondition($firstCondition);
|
|
|
|
$this->validateCondition($secondCondition);
|
2018-11-09 22:22:42 +00:00
|
|
|
$firstCondition = $this->normalizeCondition($firstCondition);
|
|
|
|
$secondCondition = $this->normalizeCondition($secondCondition);
|
2018-11-07 01:29:05 +00:00
|
|
|
if ($firstCondition($binaryOp->left, $binaryOp->right) && $secondCondition($binaryOp->right, $binaryOp->left)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return new TwoNodeMatch($binaryOp->left, $binaryOp->right);
|
2018-11-06 23:33:10 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$firstCondition($binaryOp->right, $binaryOp->left)) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
2018-11-06 23:33:10 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$secondCondition($binaryOp->left, $binaryOp->right)) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new TwoNodeMatch($binaryOp->right, $binaryOp->left);
|
2018-11-06 23:33:10 +00:00
|
|
|
}
|
2022-07-14 22:29:28 +00:00
|
|
|
public function inverseBooleanOr(BooleanOr $booleanOr) : ?BinaryOp
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
|
|
|
// no nesting
|
2022-07-14 22:29:28 +00:00
|
|
|
if ($booleanOr->left instanceof BooleanOr) {
|
2020-01-05 12:10:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-14 22:29:28 +00:00
|
|
|
if ($booleanOr->right instanceof BooleanOr) {
|
2020-01-05 12:10:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-14 22:29:28 +00:00
|
|
|
$inversedNodeClass = $this->resolveInversedNodeClass($booleanOr);
|
2020-01-05 12:10:01 +00:00
|
|
|
if ($inversedNodeClass === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 14:00:49 +00:00
|
|
|
$firstInversedExpr = $this->inverseNode($booleanOr->left);
|
|
|
|
$secondInversedExpr = $this->inverseNode($booleanOr->right);
|
|
|
|
return new $inversedNodeClass($firstInversedExpr, $secondInversedExpr);
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function invertCondition(BinaryOp $binaryOp) : ?BinaryOp
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
|
|
|
// no nesting
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp->left instanceof BooleanOr) {
|
2020-01-05 12:10:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp->right instanceof BooleanOr) {
|
2020-01-05 12:10:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$inversedNodeClass = $this->resolveInversedNodeClass($binaryOp);
|
|
|
|
if ($inversedNodeClass === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new $inversedNodeClass($binaryOp->left, $binaryOp->right);
|
|
|
|
}
|
2021-10-25 14:24:14 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @return \PhpParser\Node\Expr\BinaryOp|\PhpParser\Node\Expr|\PhpParser\Node\Expr\BooleanNot
|
2021-10-25 14:24:14 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function inverseNode(Expr $expr)
|
2021-02-17 10:51:32 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof BinaryOp) {
|
2021-02-17 10:51:32 +00:00
|
|
|
$inversedBinaryOp = $this->assignAndBinaryMap->getInversed($expr);
|
2021-10-25 14:24:14 +00:00
|
|
|
if ($inversedBinaryOp !== null) {
|
2021-02-17 10:51:32 +00:00
|
|
|
return new $inversedBinaryOp($expr->left, $expr->right);
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof BooleanNot) {
|
2021-02-17 10:51:32 +00:00
|
|
|
return $expr->expr;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanNot($expr);
|
2021-02-17 10:51:32 +00:00
|
|
|
}
|
2020-07-27 06:56:25 +00:00
|
|
|
/**
|
2022-11-07 09:33:09 +00:00
|
|
|
* @param callable(Node $firstNode, Node $secondNode): bool|class-string<Node> $firstCondition
|
2020-07-27 06:56:25 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function validateCondition($firstCondition) : void
|
2018-11-09 22:24:57 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\is_callable($firstCondition)) {
|
2018-11-09 22:24:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (\is_a($firstCondition, Node::class, \true)) {
|
2018-11-09 22:24:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new ShouldNotHappenException();
|
2018-11-09 22:24:57 +00:00
|
|
|
}
|
2018-12-16 13:24:31 +00:00
|
|
|
/**
|
2022-11-07 09:33:09 +00:00
|
|
|
* @param callable(Node $firstNode, Node $secondNode): bool|class-string<Node> $condition
|
|
|
|
* @return callable(Node $firstNode, Node $secondNode): bool
|
2018-12-16 13:24:31 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function normalizeCondition($condition) : callable
|
2018-12-16 13:24:31 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\is_callable($condition)) {
|
2018-12-16 13:24:31 +00:00
|
|
|
return $condition;
|
|
|
|
}
|
2022-06-16 14:14:21 +00:00
|
|
|
return static function (Node $node) use($condition) : bool {
|
2023-01-02 11:58:30 +00:00
|
|
|
return $node instanceof $condition;
|
2018-12-16 13:24:31 +00:00
|
|
|
};
|
|
|
|
}
|
2021-10-25 14:24:14 +00:00
|
|
|
/**
|
|
|
|
* @return class-string<BinaryOp>|null
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveInversedNodeClass(BinaryOp $binaryOp) : ?string
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
|
|
|
$inversedNodeClass = $this->assignAndBinaryMap->getInversed($binaryOp);
|
|
|
|
if ($inversedNodeClass !== null) {
|
|
|
|
return $inversedNodeClass;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp instanceof BooleanOr) {
|
|
|
|
return BooleanAnd::class;
|
2020-07-03 14:56:56 +00:00
|
|
|
}
|
2020-02-01 16:04:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-11-06 23:33:10 +00:00
|
|
|
}
|