How to add autocomple to a method with array arguments.

Hi, i want to add autocomplete to a library, which would be the best way to implement it.

array $b(['option1' => string, 'option2' => int)]);

public function a(array $b = [], array $c = [])

I would like to be able that when the user uses it: 

$class = new class();
$class->a(['suggestion from phpstorm option1 or option2

 

Thks,

 

 

 

 

13 comments
Comment actions Permalink

You might be able to achieve this using metadata, please take a look: https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#map

0
Comment actions Permalink

If it is correct i can make it work, but not when the argument is an array, i apologize for the trouble,  could provide the example that i propose when it is an array,

thks,

0
Comment actions Permalink

Did you solved that?

I search the same code problem solved.

0
Comment actions Permalink

unfortunately i have not been able to find any solution.

0
Comment actions Permalink

can u share it :D?

0
Comment actions Permalink

Apparently it seems that this issue has no solution :(

0
Comment actions Permalink

Sorry, if you could provide a code example of what you have and you're trying to achieve - I would be able to look into it & advise.

0
Comment actions Permalink

Using PHP 8 attributes:

https://github.com/JetBrains/phpstorm-attributes#arrayshape

<?php
declare(strict_types=1);

use JetBrains\PhpStorm\ArrayShape;

class someClass
{

    public function someFunc(
        #[ArrayShape(['option1' => 'string', 'option2' => 'int'])]
        array $b = [],
        #[ArrayShape(['name' => 'Exception', 'age' => 'int'])]
        array $c = []
    )
    {
    }
}

$cl = new someClass();
$cl->someFunc([''], ['']);

2
Comment actions Permalink

Andriy Bazanov THANK YOU :D

0
Comment actions Permalink

Andriy Bazanov Indeed ArrayShape works but only within the same class, and the idea is to use it as a help for an API, if you use it outside the class it will not work unfortunately,

Thks.

0
Comment actions Permalink

@Terabytefrelance

What do you mean by "the same class"? An example will help clarifying the things/your requirements.

0
Comment actions Permalink

Andriy Bazanov 

I notice the same thing, autcomplete for ArrayShape (at least for function arguments) only works for me when calling the annotated method from the same class/file. If I instantiate the class in another class, in another file, autocomplete does not work.

0
Comment actions Permalink

@T

I notice the same thing, autcomplete for ArrayShape (at least for function arguments) only works for me when calling the annotated method from the same class/file...

Looks like https://youtrack.jetbrains.com/issue/WI-66616 to me (a ticket that was extracted from this comment in WI-56021 ). Consider voting for it so it may get fixed for next 2022.2 version.

1

Please sign in to leave a comment.