Rector PHP: cannot add array index to ector rule
P粉218361972
P粉218361972 2024-02-25 18:48:49
0
1
334

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粉218361972
P粉218361972

reply all(1)
P粉161939752

好吧,我明白了...

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);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!