如何让 PhpStorm 建议/自动完成数组键

问题描述 投票:0回答:2
<?php


function test(){
    return array(
        'one' => 1,
        'two' => 2
    );
}

$test = test();

echo $test[''];

我想将光标放在单引号之间的最后一行,并让它建议

one
two
。我该怎么做?

下面的代码工作正常,那么为什么它不能在函数内部工作:

    $test = array(
        'one' => 1,
        'two' => 2
    );



    echo $test[''];
// Suggests 'one' and 'two'
php phpstorm
2个回答
13
投票

下面的代码工作正常,那么为什么它不能在函数内部工作:

因为它仅针对变量/类属性实现。

我该怎么做?

只需安装 deep-assoc-completion 插件。它甚至可以做更多的事情(例如帮助完成数组参数 - 哪些键是可能的(当然,如果您费心描述这些键)等)。


0
投票

在最新的 PHPStorm 版本中,您可以通过数组形状来实现这一点。无需外部插件。

/**
 * @return array{one: int, two: int}
 */
function test(): array {
  return [
    'one' => 1,
    'two' => 2
  ];
}

$test = test();

echo $test['one']; // Suggests 'one' and 'two'.
© www.soinside.com 2019 - 2024. All rights reserved.