Skip to content

Find all New_ nodes that are not used as a default value for Param node #8742

Answered by samsonasik
ruudk asked this question in Q&A
Discussion options

You must be logged in to vote

You can define Param::class early before New_::class:

use PhpParser\Node\Param;
// ...

    public function getNodeTypes() : array
    {
        return [Param::class, New_::class];
    }

Then, in refactor method, you can early skip if below Param:

use PhpParser\NodeTraverser;

// ...
/**
 * @param Param|New_ $node
 */
public function refactor(Node $node): null|int|Node
{
    if ($node instanceof Param) {
        // below Param won't be processed on current rule
        return NodeTraverser::DONT_TRAVERSE_CHILDREN;
    }

    // process New_ here...
}

see https://getrector.com/blog/5-tricks-to-write-better-custom-rules

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ruudk
Comment options

Answer selected by samsonasik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants