Skip to content

Commit

Permalink
[DowngradePhp72/73/74] Refactor JsonConstCleaner: move defined check …
Browse files Browse the repository at this point in the history
…after in names check (#1788)
  • Loading branch information
samsonasik committed Feb 9, 2022
1 parent cd7140a commit 7c14a98
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions rules/DowngradePhp72/NodeManipulator/JsonConstCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ public function __construct(
*/
public function clean(ConstFetch|BitwiseOr $node, array $constants): ConstFetch|Expr|null
{
$defined = (bool) $this->betterNodeFinder->findFirstPreviousOfNode(
if ($node instanceof ConstFetch) {
return $this->cleanByConstFetch($node, $constants);
}

return $this->cleanByBitwiseOr($node, $constants);
}

/**
* @param string[] $constants
*/
private function hasDefinedCheck(ConstFetch|BitwiseOr $node, array $constants): bool
{
return (bool) $this->betterNodeFinder->findFirstPreviousOfNode(
$node,
function (Node $subNode) use ($constants): bool {
if (! $subNode instanceof FuncCall) {
Expand All @@ -51,16 +63,6 @@ function (Node $subNode) use ($constants): bool {
return in_array($args[0]->value->value, $constants, true);
}
);

if ($defined) {
return null;
}

if ($node instanceof ConstFetch) {
return $this->cleanByConstFetch($node, $constants);
}

return $this->cleanByBitwiseOr($node, $constants);
}

/**
Expand All @@ -73,11 +75,15 @@ private function cleanByConstFetch(ConstFetch $constFetch, array $constants): ?C
}

$parent = $constFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof BitwiseOr) {
return new ConstFetch(new Name('0'));
if ($parent instanceof BitwiseOr) {
return null;
}

return null;
if ($this->hasDefinedCheck($constFetch, $constants)) {
return null;
}

return new ConstFetch(new Name('0'));
}

/**
Expand All @@ -92,6 +98,10 @@ private function cleanByBitwiseOr(BitwiseOr $bitwiseOr, array $constants): ?Expr
return null;
}

if ($this->hasDefinedCheck($bitwiseOr, $constants)) {
return null;
}

if (! $isLeftTransformed) {
return $bitwiseOr->left;
}
Expand Down

0 comments on commit 7c14a98

Please sign in to comment.