How to determine if ConstFetch
is used as default value of property/param
#8208
-
I have custom rector rule with such logic: private function shouldSkip(ConstFetch $node): bool
{
if (!in_array($node->name->toString(), $this->constNames, true)) {
return true;
}
// Ignore if constant is used as default value for class properties' or method param's default value
// because expressions are not allowed there as of now.
$parent = $node->getAttribute('parent');
return !is_object($parent) || in_array($parent::class, [Param::class, PropertyProperty::class]);
} It works on 0.18.0, but fails when I upgrade to 0.18.3 - I believe it's related to the process of removing parent node concept. But in this case it was the best approach to verify the context where the const is fetched... Should I change the rector's |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, you can add public function getNodeTypes(): array
{
return [Param::class, PropertyProperty::class, ConstFetch::class];
} Then check on public function refactor(Node $node) {
if ($node instanceof Param || $node instanceof PropertyProperty) {
return \PhpParser\NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
// else process the ConstFetch that not in param or propertyproperty Above is pseudocode, but you got the idea :) |
Beta Was this translation helpful? Give feedback.
Hi, you can add
Param
andPropertyProperty
togetNodeTypes()
sorted beforeConstFetch
definiton:Then check on
refactor()
method early:Above is pseudocode, but you got the idea :)