From 9c389a9635cfc75913cef8118515e3c1cee7addd Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Mon, 12 Sep 2022 15:29:56 -0500 Subject: [PATCH 1/2] Add handling for compound types. --- spec.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/spec.md b/spec.md index a26ea89..09a2aa2 100644 --- a/spec.md +++ b/spec.md @@ -128,6 +128,55 @@ Any new types and keywords added to future PHP versions MUST be in lower case. Short form of type keywords MUST be used i.e. `bool` instead of `boolean`, `int` instead of `integer` etc. +Compound types includes intersection, union, and mixed intersection and union type declarations. PHP requires +that all compound types be structured as an ORed (unioned) series of ANDs (intersections), and that each set of +intersections be encased with parentheses. + +The union symbol `|` and intersection symbol `&` MUST NOT have a leading or trailing space. The parentheses MUST NOT +have a leading or trailing space. + +If it is necessary to split a compound type into multiple lines: + +* If the type contains only intersections or only unions, then each line MUST have a single type. +* 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. +* The symbol on which the compound type is split MUST be at the start of each line. + +The following are correct ways to format compound types: + +```php +function foo(int|string $a): User|Product +{ + // ... +} + +function somethingWithReflection( + \RelfectionObject + |\ReflectionClass + |\ReflectionMethod + |\ReflectionParameter + |\ReflectionProperty $reflect +): object|null { + // ... +} + +function complex(array|(ArrayAccess&Traversable) $input): ArrayAccess&Traversable +{ + // ... +} + +function veryComplex( + array + |(ArrayAccess&Traversable) + |(Traversable&Countable) $input): ArrayAccess&Traversable +{ + // ... +} +``` + +If one of the ORed conditions is `null`, it MUST be the last item in the list. + +An intersection of a single simple type with `null` SHOULD be abbreviated using the `?` alternate syntax: `?T`. + ### 2.6 Trailing commas Numerous PHP constructs allow a sequence of values to be separated by a comma, From aae49f727e1a0b3a678eba7e81a9243dd94b46f7 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Tue, 20 Sep 2022 18:52:48 -0500 Subject: [PATCH 2/2] Typo fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Düsterhus --- spec.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec.md b/spec.md index 09a2aa2..5d41715 100644 --- a/spec.md +++ b/spec.md @@ -150,7 +150,7 @@ function foo(int|string $a): User|Product } function somethingWithReflection( - \RelfectionObject + \ReflectionObject |\ReflectionClass |\ReflectionMethod |\ReflectionParameter