Skip to content

Commit 26e8fc3

Browse files
authored
Drop JSON constants from bitwise-or structurally in JsonConstCleaner (#410)
1 parent 6f458fe commit 26e8fc3

1 file changed

Lines changed: 35 additions & 16 deletions

File tree

‎rules/DowngradePhp72/NodeManipulator/JsonConstCleaner.php‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
namespace Rector\DowngradePhp72\NodeManipulator;
66

7-
use PhpParser\Node;
87
use PhpParser\Node\Expr;
98
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
109
use PhpParser\Node\Expr\ConstFetch;
1110
use PhpParser\Node\Scalar\Int_;
1211
use Rector\Enum\JsonConstant;
1312
use Rector\NodeNameResolver\NodeNameResolver;
14-
use Rector\NodeTypeResolver\Node\AttributeKey;
1513

1614
final readonly class JsonConstCleaner
1715
{
@@ -47,35 +45,56 @@ private function cleanByConstFetch(ConstFetch $constFetch, array $constants): ?I
4745
/**
4846
* @param array<JsonConstant::*> $constants
4947
*/
50-
private function cleanByBitwiseOr(BitwiseOr $bitwiseOr, array $constants): null|Expr|Int_
48+
private function cleanByBitwiseOr(BitwiseOr $bitwiseOr, array $constants): ?Expr
5149
{
52-
$isLeftTransformed = $this->isTransformed($bitwiseOr->left, $constants);
53-
$isRightTransformed = $this->isTransformed($bitwiseOr->right, $constants);
50+
$keptOperands = [];
51+
$hasChanged = false;
5452

55-
if (! $isLeftTransformed && ! $isRightTransformed) {
53+
foreach ($this->flattenOperands($bitwiseOr) as $operand) {
54+
if ($operand instanceof ConstFetch && $this->nodeNameResolver->isNames($operand, $constants)) {
55+
$hasChanged = true;
56+
continue;
57+
}
58+
59+
$keptOperands[] = $operand;
60+
}
61+
62+
if (! $hasChanged) {
5663
return null;
5764
}
5865

59-
if (! $isLeftTransformed) {
60-
return $bitwiseOr->left;
66+
if ($keptOperands === []) {
67+
return new Int_(0);
6168
}
6269

63-
if (! $isRightTransformed) {
64-
return $bitwiseOr->right;
70+
$result = array_shift($keptOperands);
71+
foreach ($keptOperands as $keptOperand) {
72+
$result = new BitwiseOr($result, $keptOperand);
6573
}
6674

67-
return new Int_(0);
75+
return $result;
6876
}
6977

7078
/**
71-
* @param string[] $constants
79+
* Flatten a left-associative bitwise-or chain into its operands. Structural, so dropping a
80+
* target constant does not depend on the ORIGINAL_NODE attribute, which is not reliable on
81+
* re-decorated nodes.
82+
*
83+
* @return Expr[]
7284
*/
73-
private function isTransformed(Expr $expr, array $constants): bool
85+
private function flattenOperands(BitwiseOr $bitwiseOr): array
7486
{
75-
if ($expr instanceof ConstFetch && $this->nodeNameResolver->isNames($expr, $constants)) {
76-
return true;
87+
$operands = [];
88+
89+
foreach ([$bitwiseOr->left, $bitwiseOr->right] as $operand) {
90+
if ($operand instanceof BitwiseOr) {
91+
$operands = [...$operands, ...$this->flattenOperands($operand)];
92+
continue;
93+
}
94+
95+
$operands[] = $operand;
7796
}
7897

79-
return ! $expr->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node;
98+
return $operands;
8099
}
81100
}

0 commit comments

Comments
 (0)