5
<?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'
6
  • You can recommend it as a future feature on the JetBrains website? Commented May 24, 2018 at 19:56
  • @Martin I thought others would have had this request already, and I must be missing something. Why wouldn't this functionality be built in if it works with standard declared variables? Commented May 24, 2018 at 20:01
  • I don't know, but the place to get it built in is not Stack Overflow but the JetBrains product support forums, on their website. Commented May 24, 2018 at 20:06
  • 1
    That'd be pretty hard for PHPStorm to do since it would involve having to evaluate all your functions. For this function it may seem easy, but what about functions that have arguments and dynamic arrays returned? Commented May 24, 2018 at 20:11
  • Please submit the request to JetBrains tracking system here: youtrack.jetbrains.com/issues/WI#newissue=yes . You will be able to receive the answer directly from our developers while other users will be able to write their opinion of your proposal. Commented May 24, 2018 at 20:12

2 Answers 2

13

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, just what I was looking for! Thanks a lot
2

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'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.