search

Home  >  Q&A  >  body text

Rector PHP: cannot add array index to ector rule

Using Rector, I'm trying to convert the following.

$integer = some_made_up_function_call($parm1, $parm2);

Enter this:

$integer = $parm1->made_up_method_call($parm2)['hard_coded_index'];

My problem is with the last ['hard_coded_index'] . I don't know how to do this. What I have so far is.

public function refactor(Node $node): ?Node
{
    if (! $this->isName($node->name, 'some_made_up_function_call')) {
        return null;
    }

    $firstParameter = new Node\Expr\Variable($node->args[0]->value->name);

    return new Node\Expr\MethodCall(
        $firstParameter, 
        'made_up_method_call', 
        [$node->args[1]]
    );
}

P粉218361972P粉218361972281 days ago395

reply all(1)I'll reply

  • P粉161939752

    P粉1619397522024-02-26 10:14:26

    好吧,我明白了...

    public function refactor(Node $node): ?Node
    {
        if (! $this->isName($node->name, 'some_made_up_function_call')) {
            return null;
        }
    
        $conn = new Node\Expr\Variable($node->args[0]->value->name);
    
        $methodCall = new Node\Expr\MethodCall($conn, 'made_up_method_call', [$node->args[1]]);
        $arrayKey   = new Node\Scalar\String_('hard_coded_index');
    
        return new Node\Expr\ArrayDimFetch($methodCall, $arrayKey);
    }
    

    reply
    0
  • Cancelreply