Syntax
Shape keys
A field of a shape has an implicit key or an explicit one. Both kinds may be used together in one shape, the way PHPStan and Psalm allow it: which keys the implicit fields denote is left to whoever reads the shape.
An implicit and an explicit key in one shape.
array{First, name: Second}
resultTypeLang\Type\NamedTypeNode
Implicit keys
Section titled “Implicit keys”An implicit field is a type alone, its position in the shape gives it a numeric index.
array{First, Second, Third}
resultTypeLang\Type\NamedTypeNode
Explicit keys
Section titled “Explicit keys”An explicit field is a key, a colon (:) and the type: key: Type. The
simplest key is an identifier, the same as a type
name: it may contain dashes and a namespace, and the words true, false and
null are ordinary names here, not literals.
array{a: First, b: Second}
resultTypeLang\Type\NamedTypeNode
A dash, a namespace and a reserved word as keys.
array{some-key: First, Some\Name: Second, null: Third}
resultTypeLang\Type\NamedTypeNode
Numeric keys
Section titled “Numeric keys”A key may be an integer literal in any notation, signed ones included. A float cannot be a key.
array{42: A, -1: B, 0x1F: C}
resultTypeLang\Type\NamedTypeNode
A float is not a key.
array{1.5: A}
errorMalformedSyntaxException Invalid shape, a field, "," or "}" is expected
String keys
Section titled “String keys”A key may be a string literal, which allows characters an identifier does not, escape sequences included.
array{"name some": First, "escape\nchars": Second}
resultTypeLang\Type\NamedTypeNode
Constant keys
Section titled “Constant keys”A key may be a class constant.
array{Path\To\ClassName::CONSTANT_NAME: string}
resultTypeLang\Type\NamedTypeNode
A constant mask is not a key.
array{Path\To\ClassName::PREFIX_*: string}
errorMalformedSyntaxException Invalid shape, a field, "," or "}" is expected