Skip to content

Commit

Permalink
[Core] Refactor CreatedByRuleDecorator to make consistent check for c…
Browse files Browse the repository at this point in the history
…onsecutive same Rector run (#3078)
  • Loading branch information
samsonasik committed Nov 19, 2022
1 parent 31bf9e4 commit 24b9eb7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
22 changes: 19 additions & 3 deletions src/NodeDecorator/CreatedByRuleDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,25 @@ public function decorate(array | Node $node, Node $originalNode, string $rectorC

private function createByRule(Node $node, string $rectorClass): void
{
$mergeCreatedByRule = array_merge($node->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [], [$rectorClass]);
$mergeCreatedByRule = array_unique($mergeCreatedByRule);
$createdByRule = $node->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];
$lastRectorRuleKey = array_key_last($createdByRule);

$node->setAttribute(AttributeKey::CREATED_BY_RULE, $mergeCreatedByRule);
// empty array, insert
if ($lastRectorRuleKey === null) {
$node->setAttribute(AttributeKey::CREATED_BY_RULE, [$rectorClass]);
return;
}

// consecutive, no need to refill
if ($createdByRule[$lastRectorRuleKey] === $rectorClass) {
return;
}

// filter out when exists, then append
$createdByRule = array_filter(
$createdByRule,
static fn (string $rectorRule): bool => $rectorRule !== $rectorClass
);
$node->setAttribute(AttributeKey::CREATED_BY_RULE, [...$createdByRule, $rectorClass]);
}
}
20 changes: 8 additions & 12 deletions src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function verify(string $rectorClass, Node $node, string $filePath): ?Rect
{
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);

if ($this->hasCreatedByRule($rectorClass, $node, $originalNode)) {
if ($this->hasConsecutiveCreatedByRule($rectorClass, $node, $originalNode)) {
return new RectifiedNode($rectorClass, $node);
}

Expand All @@ -62,21 +62,17 @@ public function verify(string $rectorClass, Node $node, string $filePath): ?Rect
/**
* @param class-string<RectorInterface> $rectorClass
*/
private function hasCreatedByRule(string $rectorClass, Node $node, ?Node $originalNode): bool
private function hasConsecutiveCreatedByRule(string $rectorClass, Node $node, ?Node $originalNode): bool
{
if (! $originalNode instanceof Node) {
$createdByRule = $node->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];
$lastRectorRuleKey = array_key_last($createdByRule);
$createdByRuleNode = $originalNode ?? $node;
$createdByRule = $createdByRuleNode->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];

if ($lastRectorRuleKey === null) {
return false;
}

return $createdByRule[$lastRectorRuleKey] === $rectorClass;
$lastRectorRuleKey = array_key_last($createdByRule);
if ($lastRectorRuleKey === null) {
return false;
}

$createdByRule = $originalNode->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];
return in_array($rectorClass, $createdByRule, true);
return $createdByRule[$lastRectorRuleKey] === $rectorClass;
}

/**
Expand Down

0 comments on commit 24b9eb7

Please sign in to comment.