An example is better than any explaining: Let's say I have the following code
function arrayToIdMap(array $models) : array
{
$result = [];
foreach ($models as $model) {
/** @var BaseClassWithGetIdMethod $model **/
$result[$model->getId()] = $model;
}
return $result;
}
/** @var Car[] **/
$carList = getCarList();
/** @var Truck[] **/
$truckList = getTruckList();
/** @var Car[] **/
$carMap = arrayToIdMap($carList);
/** @var Truck[] **/
$truckMap = arrayToIdMap($truckList);
What I want to do is, to inform PHP automatically the types of $carMap and $truckMap so that I have some code that acts like the following pseudocode:
/**
* @param array $$models
* @return The_Original_Type_Inferred_From_$models
*/
function arrayToIdMap(array $models) : array
{
$result = [];
foreach ($models as $model) {
/** @var BaseClassWithGetIdMethod $model **/
$result[$model->getId()] = $model;
}
return $result;
}
/** @var Car[] **/
$carList = getCarList();
/** @var Truck[] **/
$truckList = getTruckList();
$carMap = arrayToIdMap($carList);
$truckMap = arrayToIdMap($truckList);
// $carMap[$id]->someMethod() will not show error if Car class has the someMethod method and PhpStorm knows $carMap is of type Car[] at this point
Is this possible or not?


array_xxxPHP functions -- have a look here: github.com/JetBrains/phpstorm-stubs/blob/master/meta/… This does not use PHPDoc though -- it should be stored in a separate file for PhpStorm eyes only.