Skip to content

The phpdoc array keys format supported by the plugin. #63

Closed
@klesun

Description

@klesun

The README briefly mentions an ability to specify array keys in a doc. I'll describe it a bit more detailed here...

So, basically, in the @param, @var, @property, @method and @return phpdoc tags, after the variable name, you can type an = followed by any valid php expression - it will be parsed by the plugin and used in completion on further references to this variable.

  • Example: (specify that var contains the value returned by a function)

    /**
     * @param $sale = self::createSale() // a sale confirmed by bookeeping
     */
    public function processSale($sale) {}

    image

  • Another example: (specify associative array keys)

    /** @var array $clientData = [
     *     'id' => 1234, // unique id in DB
     *     'activityDt' => new DateTime,
     *     'sales' => [SaleApp::createSale()], // array of one or more sales client had with us over the years
     *     'agentReviews' => [
     *          ['agent' => 'Steeve', 'rating' => 9.5, 'comment' => 'Does his job well'],
     *          ['agent' => 'Tiffany', 'rating' => 6.3, 'comment' => 'Could try harder'],
     *     ],
     *     'soapData' => (object)[ // the data we got from external system
     *         'LoanPast' => 'GOOD',
     *         'Job' => 'WEB DESIGNER',
     *     ],
     * ] */
    $clientData = json_decode($text, true);

    image

  • Note that these type annotation will also work for argument key completion when you are calling the function:

    image

  • Another example: (specify return type keys)

    /** @return array [
     *     'address' => 'Novokuzneckaya Oblastj, Dom 427, Moscow, Russia',
     *     'family' => 'single',
     *     'name' => 'Vasya Pupkin',
     * ] */
    private function getExternalData() {}

    image
    (an = between @return and the expression is allowed, but not required)

  • As discussed in Enumeration #50, you can specify "enum" string value options in phpdoc:

    /** @param $parameters = ['keyword1', 'keyword2', 'keyword3'][$any] */
    function my_test($parameters) {}

    image

  • As mentioned below, you can specify magic method return type like that:

     * @method array getData() = ['name' => 'John', 'age' => 27]
    
  • As described in centralize configure as similar as .phpstorm.meta.php of phpstorm #75, you can now specify function return type and it's argument types in .phpstorm.meta.php

    image

  • As described in Support PSALM/phan phpdoc notation #77, you can now use psalm/phan array{key1: int, key2: string} notation to specify key types (with @template generics, yay)

  • As described in Implement WordPress-like documentations // Magic::argType() #152, you can now use WordPress format notation to specify key types

    image

  • As described in Twig type hints don't seem to work with completion for me #165 and Allow to specify php data reference function FQN in twig comment #119, deep-assoc also allows you to reference functions by fully qualified name in Twig templates to use them as type information for vars completion:

    Considering we have such php class somewhere in the project:

     namespace SomeNamespace;
     class SomeClass {
     	private function makeTemplateData() {
     		return [
     			'userName' => 'Vasya', 
     			'paymentMethods' => [
     				['name' => 'Paypal', 'countries' => ['US', 'LV']],
     				['name' => 'Swedbank', 'countries' => ['ES', 'LV']],
     			],
     		];
     	}
     }

    You can reference it in a twig file with {# data-source = ... #}:

     {# data-source = \SomeNamespace\SomeClass::makeTemplateData() #}
    
     <div>
     	<span>Hello, {{  }}</span> <!-- should suggest: "userName", "paymentMethods" -->
     </div>
  • There is a set of magic function names you can use in doc comments to reference plugin-specific resolution tools:

    • As described in use table name instead of get_object_var(static) #123 you can use @param $row = Magic::dbRow('my_table_name') to tell IDE that $row is an associative array with keys matching columns in the my_table_name. For that to work, you need Data Source with your database credentials configured. Note, you can use any expression instead of 'my_table_name' string, like $this->tableName or static::getTableName()

If there are any questions concerning the format, you are welcome to file an issue - I'll answer there, and if it involves adding more formats, will reference your issue here.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions