Rector PHP:无法在校长规则中添加数组索引

发布于 2025-01-24 13:47:24 字数 682 浏览 1 评论 0原文

使用Rector,我正在尝试转换以下内容。

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

其中:

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

我的问题是['hard_coded_index']最后。我不知道如何做到这一点。到目前为止,我所拥有的。

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]]
    );
}

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

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

Into this:

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

My issue is with the ['hard_coded_index'] at the end. I can’t figure out how to accomplish that. 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]]
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2025-01-31 13:47:24

好吧,我想出了...

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);
}

All right, I figured it out...

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文