Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

@TimWolla TimWolla Sep 29, 2022

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:

} catch (OtherThrowableType | AnotherThrowableType $e) {

Copy link
Contributor

@samdark samdark Sep 30, 2022

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 👍

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|(Traversable&Countable) $input): ArrayAccess&Traversable
|(Traversable&Countable) $input
): ArrayAccess&Traversable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Expand Down