24

I have:

$myarr['DB'] = new DB();
$myarr['config'] = new config();

Can I somehow make PhpStorm to know exactly what is inside those keys? For now I see hinting for variables and class properties only, but not array keys.

1

8 Answers 8

24

Late answer, but things have changed.

According to 2021.2 changelist it is possible now to define shape of a simple array with one line comment:

/**
 * @return array{id: int, name: string, object: \Of\Some\Class}
 */
function getArray(): array {...}

If there are object-like arrays in your code, you can now define their structure with this PHPDoc annotation: array{key: type, key: type, ...}.

PhpStorm provides code completion for such annotated arrays, reducing the time you spend on routine typing and protecting you from mistakes.

The support is limited to one-line array shape definitions. For larger structures, it is often better to use real objects and classes.

Unfortunatelly I have not found a way to define structure of multi dimensional array, and it would be great to annotate a list of such "shaped" arrays...

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

Comments

4

According to https://youtrack.jetbrains.com/issue/WI-59083/recognize-use-of-list-with-array-shapes it is possible now to describe even item shape in the list:

/**
 * @return array<int, array{id: int, name: string}>
 */
function getShapedArray(): array {...}

Works nice with for loops, but no completion when:

$array = getShapedArray();
$elementId = 2;
// No completion for name property.
$array[$elementId]['name'];

Comments

3

You can define the array keys in advance, then PHPStorm will suggest them (CTRL+space)

$my = array();
$my['qwe'] = '';
$my['asd'] = '';
$my['zxc'] = '';

$my['']// inside '' will be autosuggest

You can also use phpdoc (CTRL+Q):

/**
 * keys:
 * <pre>
 * some_array   (array)
 * some_bool    (boolean)
 * some_double  (double)
 * some_nice_integer    (integer)
 * </pre>
 * @return array
 */
public function toArray(){
    // return some array
}

Comments

1

This can be done inline. Array shapes were added in phpStorm as of 2021.

/* @var $myarr array{DB: DB, config: config}*/
$myarr['DB'] = new DB();
$myarr['config'] = new config();

Comments

1

This is done with attributes in modern PHPStorm versions.

use JetBrains\PhpStorm\ArrayShape;

#[ArrayShape(["DB" => \DB::class, "config" => \config::class])]
private array $myarr;

As with the PHPDoc comments in other answers, there does not appear to be any enforcement of these types (e.g. IDE notices when assigning an unexpected type of value would be nice) but it does allow auto-complete at least.

Typically I will define all my array shapes in a single constant and then refer to it later:

<?php

namespace App;

use JetBrains\PhpStorm\ArrayShape;

class MyClass {
    private const array ARRAY_SHAPE = [
        "myMethod" => ["foo" => "string", "bar" => "int[]"],
        "otherMethod" => ["prop1" => "self", "prop2" => \PDO::class],
    ];

    #[ArrayShape(self::ARRAY_SHAPE["myMethod"])]
    public function myMethod(): array {
        // ...
    }

    #[ArrayShape(self::ARRAY_SHAPE["otherMethod"])]
    public function otherMethod(): array {
        // ...
    }
}

Comments

0

This functional is not realized yet in PhpStorm. Vote for support array access feature request.

Also you can try silex idea plugin.

1 Comment

Think it more about when PHP knows structure at first place. I, for example, returning rows with JSON from DB witch decoded then to 4D array, and it good to have hinting for theme. But only way to do it is PHPdocking, and sadly there is no syntax for that=(
-1

For an arbitrary array, PHPStorm has no idea of the keys used in it and thus does not provide hints. It is even possible to prove that it is impossible to reliably implement such a feature, so I think you are out of luck here.

Collected From:

3 Comments

Don't think that DHPdocing class properties is somewhat more reliable. Why not to make feture to PHPdoc array keys? Especially useful when working with service containers.
Not Impossible, just difficult. plugins.jetbrains.com/plugin/9927-deep-assoc-completion works like a champ with no apparent performance reduction (phpStorm is a bit of a beast so another second or so generating an index would go unnoticed). Been using it for a few days in laravel and WP projects - its lovely.
Apart from if using PhpStorm deep-assoc-completion is the companion (and works like a charm), the (needlessly dv'ed) answer about casting of an array already demonstrated the same as the answer without such an object cast demonstrates that there already was support before the PHPDoc Array Shape syntax (which again deep-assoc-completion supported already earlier than PhpStorm itself — and with template types btw.). Mentioning the halting problem is interesting in that regard still.
-3
$obj = (object)[]; // Cast empty array to object

add properties:
$obj->x = 'some'
$obj->y = 'hints'

Now, PHPStorm, when typing $obj-> ..... hints x and y

1 Comment

Avoid type casting by using new stdClass().

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.