Vscode-intelephense: Support for "option arrays"

Created on 13 Dec 2017  路  11Comments  路  Source: bmewburn/vscode-intelephense

As shown here in the proposal for phpdoc fig standard: https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#examples-12

Basically looks like this

/**
 * Initializes this class with the given options.
 *
 * @param array $options {
 *     @var bool   $required Whether this element is required
 *     @var string $label    The display name for this element
 * }
 */
public function __construct(array $options = array())
{
    <...>
}

Would be really cool since PHP tends to use these option arrays all over the place in libraries!

enhancement

Most helpful comment

Alternate syntax that Psalm static checker uses: /** @return array<int, array{name: string, type: string, active: bool}> */ which is arguably nicer to look at and type.

All 11 comments

btw, I think it would be really helpful to initially just parse and show it properly when hovering or typing the function out with the little intellisense above for the params. Support for doing $options['required'] and having intelephense know that it should be a bool is great, but I think probably also harder and not as important as just seeing the definition initially.

Alternate syntax that Psalm static checker uses: /** @return array<int, array{name: string, type: string, active: bool}> */ which is arguably nicer to look at and type.

This would be useful. I agree the Psalm syntax is nicer but the fig standard might have more clout. Either way this will require a proper phpdoc parser. Currently it is very simple and just uses regex to pick out the text and tags.

Hi. @bmewburn, if you did not decide on format yet, would you mind taking a look on the format deep-assoc-completion plugin uses in phpstorm?
image

Do you accept pull requests? I could write the phpdoc parser and add support for such format in your extension if you would like that ;-).

Upd.: I somewhat ported deep-assoc-completion to vscode. It's shipped as a separate extension, should work nicely together with intelephense.

@klesun, sorry for the late reply and thank you for your offer. For now I'd rather wait until some common standard emerges.

oh, ok (though I guess it will take quite some time...)

@bmewburn, there is no common standard for PHP; however, there is a common standard for JSDoc that works well for majority of usecases - see @typedef tag (http://usejsdoc.org/tags-typedef.html).

An example of a definition using jsdoc syntax

/**

  • @typedef Object Person
  • @property string Person[name] Person's name
  • @property string Person[address] Person's address
  • @property string Person[address][city] Person's address
    */

/**

  • @var Person[] $person_array with SSN as key
    */

$person_array = array (
"493-11-1111"=> array("name"=>"John Doe", "address"=>array(city=>"Chicago"))},
"493-11-2222"=> array("name"=>"Jane Doe", "address"=>array(city=>"London"))},
);

The phpdoc proposed PSR no longer shows the format initially showed by @hworld.

Pslam has become quite popular now and it's format is pretty nice, so my vote goes to that :slightly_smiling_face:. I believe even phpstan is going to add / has added support for similar format.

Seems like psalm format is only sensible despite not being in PSR. JSDoc uses similar or the same format.

https://psalm.dev/docs/annotating_code/type_syntax/array_types/#object-like-arrays

Any updates on this? Thanks!

I would appreciate this feature as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zlianon picture zlianon  路  3Comments

nn-hh picture nn-hh  路  3Comments

steven7mwesigwa picture steven7mwesigwa  路  4Comments

ghost picture ghost  路  3Comments

aleksandervines picture aleksandervines  路  3Comments