Skip to content

Commit

Permalink
Do not replace existing constant nodes if they are already the correc…
Browse files Browse the repository at this point in the history
…t ones
  • Loading branch information
SerethiX committed Dec 12, 2023
1 parent 3768001 commit 91f17e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,4 @@ parameters:

-
path: rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php
message: "#Method \"processToClassConstFetch\\(\\)\" returns bool type, so the name should start with is/has/was#"
message: "#Method \"process(ToClassConstFetch|Arg)\\(\\)\" returns bool type, so the name should start with is/has/was#"
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function processToClassConstFetch(
AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch
): bool {
$hasChanged = false;

foreach ($attributeGroup->attrs as $attribute) {
if (! $this->isName($attribute->name, $attributeKeyToClassConstFetch->getAttributeClass())) {
continue;
Expand All @@ -147,23 +148,38 @@ private function processToClassConstFetch(
continue;
}

$value = $this->valueResolver->getValue($arg->value);

$constName = $attributeKeyToClassConstFetch->getValuesToConstantsMap()[$value] ?? null;
if ($constName === null) {
continue;
if ($this->processArg($arg, $attributeKeyToClassConstFetch)) {
$hasChanged = true;
}

$arg->value = $this->nodeFactory->createClassConstFetch(
$attributeKeyToClassConstFetch->getConstantClass(),
$constName
);

$hasChanged = true;
continue 2;
}
}

return $hasChanged;
}

private function processArg(Node\Arg $arg, AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch): bool
{
$value = $this->valueResolver->getValue($arg->value);

$constName = $attributeKeyToClassConstFetch->getValuesToConstantsMap()[$value] ?? null;
if ($constName === null) {
return false;
}

$newValue = $this->nodeFactory->createClassConstFetch(
$attributeKeyToClassConstFetch->getConstantClass(),
$constName
);

if (
$arg->value instanceof Node\Expr\ClassConstFetch
&& $this->getName($arg->value) === $this->getName($newValue)
) {
return false;
}

$arg->value = $newValue;

return true;
}
}

0 comments on commit 91f17e1

Please sign in to comment.