Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

How to make PhpStorm suggest / autocomplete array keys

<?php


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

$test = test();

echo $test[''];

I would like to put my cursor in the last line in between the single quotes, and have it suggest one or two. How can I do this?

The below works fine, so why doesn't it work inside of a function:

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



    echo $test[''];
// Suggests 'one' and 'two'

Upvotes: 5

Views: 1948

Answers (2)

ya.teck
ya.teck

Reputation: 2336

In newest PHPStorm versions you can achieve this through array shape. No external plugins are required.

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

$test = test();

echo $test['one']; // Suggests 'one' and 'two'.

Upvotes: 2

LazyOne
LazyOne

Reputation: 165088

The below works fine, so why doesn't it work inside of a function:

Because it's implemented only for variables/class properties.

How can I do this?

Just install deep-assoc-completion plugin. It can do even more than that (e.g. help with completion of array parameter -- what keys are possible (if you bother to describe those keys, of course) etc).

Upvotes: 13

Related Questions