Skip to content

Commit 3cb58d0

Browse files
committed
Add handling for compound types.
1 parent 2441346 commit 3cb58d0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

spec.md

+49
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,55 @@ Any new types and keywords added to future PHP versions MUST be in lower case.
128128
Short form of type keywords MUST be used i.e. `bool` instead of `boolean`,
129129
`int` instead of `integer` etc.
130130

131+
Compound types includes intersection, union, and mixed intersection and union type declarations. PHP requires
132+
that all compound types be structured as an ORed (unioned) series of ANDs (intersections), and that each set of
133+
intersections be encased with parentheses.
134+
135+
The union symbol `|` and intersection symbol `&` MUST NOT have a leading or trailing space. The parentheses MUST NOT
136+
have a leading or trailing space.
137+
138+
If it is necessary to split a compound type into multiple lines:
139+
140+
* If the type contains only intersections or only unions, then each line MUST have a single type.
141+
* If the type contains both intersections and unions, then each line MUST have a single union segment. All intersections in a segment MUST be on the same line.
142+
* The symbol on which the compound type is split MUST be at the start of each line.
143+
144+
The following are correct ways to format compound types:
145+
146+
```php
147+
function foo(int|string $a): User|Product
148+
{
149+
// ...
150+
}
151+
152+
function somethingWithReflection(
153+
\RelfectionObject
154+
|\ReflectionClass
155+
|\ReflectionMethod
156+
|\ReflectionParameter
157+
|\ReflectionProperty $reflect
158+
): object|null {
159+
// ...
160+
}
161+
162+
function complex(array|(ArrayAccess&Traversable) $input): ArrayAccess&Traversable
163+
{
164+
// ...
165+
}
166+
167+
function veryComplex(
168+
array
169+
|(ArrayAccess&Traversable)
170+
|(Traversable&Countable) $input): ArrayAccess&Traversable
171+
{
172+
// ...
173+
}
174+
```
175+
176+
If one of the ORed conditions is `null`, it MUST be the last item in the list.
177+
178+
An intersection of a single simple type with `null` SHOULD be abbreviated using the `?` alternate syntax: `?T`.
179+
131180
### 2.6 Trailing commas
132181

133182
Numerous PHP constructs allow a sequence of values to be separated by a comma,

0 commit comments

Comments
 (0)