With vscode-intelephense extension (1.10.0 - 2023-11-05).
/** @param array{foo:string, bar:int} $arrayShape */
or /** @return object{foo:string, bar:int} */
For new $className
expressions where $className
is of type class-string
/** @disregard [OPTIONAL CODE] [OPTIONAL DESCRIPTION] */
. For example /** @disregard P1013 method exists on runtime type */
Must be encapsulated in parentheses.
For example - /** @return ($param is string ? string : object) */
, /** @return (T is string ? string : object) */
<?php
/** @var array{foo:string, bar:int} $arrayShape */
$arrayShape = ['foo' => 'foo', 'bar' => 1];
$foo = $arrayShape['foo']; // autocomplete works, type is string
$bar = $arrayShape['bar']; // autocomplete works, type is int
/** @var object{foo:string, bar:int} $objectShape */
$objectShape = (object)['foo' => 'foo', 'bar' => 1];
$foo = $objectShape->foo; // autocomplete works, type is string
$bar = $objectShape->bar; // autocomplete works, type is int
/** @var array{foo:string, bar:int}[] $arrayOfArrayShapes */
$arrayOfArrayShapes = [
['foo' => 'foo', 'bar' => 1],
['foo' => 'foo', 'bar' => 1],
];
$foo = $arrayOfArrayShapes[0]['foo']; // autocomplete works, type is string
$bar = $arrayOfArrayShapes[0]['bar']; // autocomplete works, type is int
/** @var object{foo:string, bar:int}[] $arrayOfObjectShapes */
$arrayOfObjectShapes = [
(object)['foo' => 'foo', 'bar' => 1],
(object)['foo' => 'foo', 'bar' => 1],
];
$foo = $arrayOfObjectShapes[0]->foo; // autocomplete works, type is string
$bar = $arrayOfObjectShapes[0]->bar; // autocomplete works, type is int