-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add handling for compound types. #45
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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( | ||||||||
\ReflectionObject | ||||||||
|\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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. Not sure here. Does this context also force multilining the return part? If so, that should be said explicitly somewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah doing this would split the argument list over multiple lines and would make "argument list is split across multiple lines" rules apply here. I get that this is different than actually splitting each argument onto their own lines but I don't think that distinction matters at all when it comes to readability and consistency. |
||||||||
{ | ||||||||
// ... | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
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, | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I personally agree that there should be no spaces around the "symbols", this appears to be inconsistent with
catch()
which has spaces around in the example:per-coding-style/spec.md
Line 967 in b49fd56
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be unified one way or another 👍