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.
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.
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...
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'];
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
}
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();
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 {
// ...
}
}
This functional is not realized yet in PhpStorm. Vote for support array access feature request.
Also you can try silex idea plugin.
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:
$obj = (object)[]; // Cast empty array to object
add properties:
$obj->x = 'some'
$obj->y = 'hints'
Now, PHPStorm, when typing $obj-> ..... hints x and y
new stdClass().