I am creating a Rector
(rector/rector
) rule that should alter properties based on some of their attributes.
PhpParser
is from nikic/php-parser
.
I have no problem fetching the Properties, I simply check if current PhpParserNode
is an instance of PhpParserNodeStmtProperty
(and I do get matches).
However, for my use case to match some change source data, I also need the class name of the class the property belongs to – I tried following naive implementation:
private function getClassNameFromPropertyNode(Property $propertyNode): ?string {
$currentNode = $propertyNode;
while ($currentNode !== null) {
if ($currentNode instanceof PhpParserNodeStmtClass_) {
$name = $currentNode->name;
return $name instanceof PhpParserNodeIdentifier ? $name->toString() : null;
}
$currentNode = $currentNode->getAttribute('parent');
}
return null;
}
But it always gets me null
… it seems that $currentNode->getAttribute('parent')
this always return null.
How do I get the parent node of current node and/or the class name of the current property? Thank you.