JetBrains / phpstorm-attributes

PhpStorm specific attributes
https://packagist.org/packages/jetbrains/phpstorm-attributes
Apache License 2.0
391 stars 13 forks source link

[ArrayShape] for maps/associative arrays as dynamic array indexes #27

Open Justinas-Jurciukonis opened 8 months ago

Justinas-Jurciukonis commented 8 months ago

Right now if I have following attribute

class Shema {
   #[ArrayShape(['string' => Property::class])]
    public array $properties;
}

$schema->properties['prop1'] = new Property();
$shcema->properties['prop2'] = new Property();

PHPStorm (PhpStorm 2023.3.4) will report on key Value should be one of: 'string' (why value?).

Is there any way to show possible index values? Like "string" / "int" / "SomeEnum->getValues()"

Screenshot 2024-02-28 at 10 59 34
SerafimArts commented 5 months ago

I think it's worth using native PHP syntax:

/**
 * @var array<string, Property>
 */
public array $properties;

// or
/**
 * @var array{prop1: Property, prop2: Property}
 */
public array $properties;

// or
/**
 * @var array{
 *     prop1: Property, 
 *     prop2: Property,
 *     ...<string, Property>
 * }
 */