Annotate an Array of Task Objects with PHPDoc
Annotate Task objects in a PHP array, see how PHPStan uses the element type, and decide when list keys matter too.
To describe an array whose values are Task objects, write @param Task[] $tasks or @param array<Task> $tasks. Choose @param list<Task> $tasks only if callers must also supply sequential integer keys starting at zero. These are PHPDoc annotations that PHPStan can use for analysis, not native PHP declarations that validate every element when the function runs.
Let PHPStan follow a Task through the array
This example gives Task one method and passes two Task objects to a function that reads the array. The commented-out call shows a different value type; it is not part of the valid call.
<?php
class Task
{
public function __construct(private string $name)
{
}
public function label(): string
{
return $this->name;
}
}
/** @param Task[] $tasks */
function printTaskLabels(array $tasks): void
{
foreach ($tasks as $task) {
echo $task->label(), "\n";
}
}
printTaskLabels([
new Task('Write'),
new Task('Review'),
]);
// Invalid for the PHPDoc element type: 'Review' is a string.
// printTaskLabels([new Task('Write'), 'Review']);
The useful detail is what happens at the array read. PHPStan can use Task[] to treat each value assigned to $task by foreach as a Task. It can therefore analyze $task->label() as a method call on that class. The annotation also gives PHPStan a type to check against values supplied at a call. PHPStan documents both array-value notation and the flow of type information through its analysis.
The commented call supplies a string where the annotation expects a Task. That is a documented element-type mismatch for static analysis. It is not a TypeError caused by the array $tasks parameter: the argument is still an array. Its keys are not the problem either. Like the valid call, the commented call uses an array with sequential integer keys starting at zero. Separating those two questions helps when a caller has the right array structure but the wrong kind of value.
Choose the value type first, then decide whether keys matter
Task[] and array<Task> are alternative ways to annotate an array's value type. PHPStan lists both Type[] and array<Type> as array type forms. To use the second spelling in the example, replace its PHPDoc line with this one; do not put both lines on the function:
/** @param array<Task> $tasks */
Both forms address the same question here: what type should an element read from $tasks have? Neither spelling adds a requirement that the keys form a list. If the function only needs Task values, avoid making a stronger key promise than its callers need to keep.
When that promise does matter, use the list form instead:
/** @param list<Task> $tasks */
PHPStan defines lists as arrays with sequential integer keys starting at zero. The valid call in the worked example has that sequence, so it fits a list<Task> annotation as well. A caller with a different key sequence presents a separate issue. For instance, using the Task class from the example:
$tasksWithAnotherKey = [2 => new Task('Review')];
The value in this array is a Task, so its class is not the reason it fails a list<Task> requirement. Its only key is 2, rather than a sequence starting at 0. If such keys are acceptable to the function, Task[] or array<Task> describes its value requirement without also asking for list keys. The choice is about what callers must provide, not about which notation looks more precise.
The runtime boundary is narrow but important. array $tasks requires an array argument, while the PHPDoc element type does not check every value as the function runs. Task[] is not valid native parameter syntax. If the function must reject an array containing a non-Task value at runtime, that needs a separate check; the PHPDoc annotation alone cannot provide one.
