All articles

Find and fix PHP problems

Specify Required and Optional Array Keys with PHPDoc Shapes

Document required and optional PHP array keys with a PHPDoc shape, and identify a wrong value type through static analysis.

Editorial illustration for Specify Required and Optional Array Keys with PHPDoc Shapes

To describe an associative array with a required integer id and string name, annotate it as array{id: int, name: string} in PHPDoc. Add nickname?: string when nickname may be absent but must be a string if present. This gives PHPStan or Psalm named keys and value types to analyze alongside a function’s native array declaration.

Mark required and optional entries

An array shape puts the expected keys inside array{...}, followed by each key’s value type. In array{id: int, name: string, nickname?: string}, id and name are required; nickname is optional.

The ? belongs after the key name, not after its value type. It means the key may be missing; it does not change the declared type of a present nickname. Both PHPStan and Psalm document optional shape keys. Simple string keys such as id and name do not need quotes in PHPStan’s shape syntax.

This is a known-key description: it names the fields and states the value type associated with each one. Use a shape when those field names are part of the agreement between functions. Psalm also calls shapes “object-like arrays” because their key offsets are known.

Put the shape on a function

Here is a single-file example. The native declarations say array; the PHPDoc supplies more specific parameter and return shapes for analysis. The return shape has a different set of keys from the parameter shape, so each annotation describes its own side of the function.

<?php

/**
 * @param array{id: int, name: string, nickname?: string} $person
 * @return array{id: int, displayName: string}
 */
function personSummary(array $person): array
{
    return [
        'id' => $person['id'],
        'displayName' => $person['name'],
    ];
}

$withoutNickname = personSummary(['id' => 12, 'name' => 'Ada']);
$withNickname = personSummary([
    'id' => 13,
    'name' => 'Lin',
    'nickname' => 'L',
]);

// Expected static-analysis finding: id is a string, but the shape requires int.
$wrongId = personSummary(['id' => '14', 'name' => 'Jo']);

The first two calls supply the required keys with their declared types. The second also supplies the optional nickname. This function does not use that field; it places the required id and name values into a return array whose documented keys are id and displayName. Reading the two annotations separately matters: name is an input key, while displayName is a return key.

The last call is deliberately incorrect for static analysis: '14' is a string where the parameter shape specifies int. The comment labels an expected finding, not an observed result from running the example. PHPStan’s documentation says it checks the value types of specified array keys; Psalm documents the known-key shape format and optional keys.

PHPDoc or a PhpStorm attribute?

For the PHPStan or Psalm analysis illustrated here, use the PHPDoc array{...} form. PhpStorm also parses array shapes in PHPDoc and supports #[ArrayShape] attributes for describing key-value structures to the IDE. Its documentation describes key completion and value-type inference from those declarations.

There is an editor-version distinction if you later make the shape more elaborate: PhpStorm 2021.2’s PHPDoc shape support was limited to single-line, single-level annotations. PhpStorm 2022.1 added multiline and nested shapes in both PHPDoc and attributes. The example here stays single-level. Whichever notation you choose for the tooling involved, keep the input and return shapes aligned with the keys the function actually reads and returns.

← All articles