Hey #PHP. When you use union and intersection types, do you include spaces?

(RT for reach, etc. This is survey data for @phpfig. If you know of any official policies by major projects already, please note/link in the replies.)

#codingstyle #phpfig

Foo|Bar, Foo&Bar, (Foo&Bar)|Baz
59.4%
Foo | Bar, Foo & Bar, (Foo & Bar) | Baz
39.6%
Other (specify)
1%
Poll ended at .

@Crell @phpfig I use the CodeSniffer rule SlevomatCodingStandard.TypeHints.UnionTypeHintFormat with `withSpaces` set to “yes.” I think this also works on intersection types, despite the name.

https://github.com/slevomat/coding-standard/blob/master/doc/type-hints.md#slevomatcodingstandardtypehintsuniontypehintformat-

coding-standard/doc/type-hints.md at master · slevomat/coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs - slevomat/coding-standard

GitHub
@Crell @phpfig I believe #Symfony uses no spaces, and I think this is what php-cs-fixer does.
@ramsey @phpfig Have a link for Symfony?
@Crell @phpfig They don’t call it out here, but the code example shows using it without spaces. https://symfony.com/doc/current/contributing/code/standards.html
Coding Standards (Symfony Docs)

Symfony code is contributed by thousands of developers around the world. To make every piece of code look and feel familiar, Symfony defines some coding standards that all contributions must follow. T…

Rule types_spaces - PHP Coding Standards Fixer

PHP Coding Standards Fixer

@Crell @phpfig for everything that has more than a simple & or | chain having spaces is such an accessible thing for me, allowing me to parse the blob of symbols.

If my editor would highlight the braces and symbols differently from the text I would have more of a chance without spaces, but as it is, it's very annoying. Especially because they need to restate the compound types everywhere, not just define them once like in TS.

@edorian @phpfig So it depends on how complex it is?

Foo|Bar|Baz is OK, but not if mixing and &?

@Crell @phpfig For me, somehow yes.

If there are no braces I know at the first symbol that it's a uniform list and that I can just go and skip-scan through the text.

But (ClassNameWithSomeLength|(Foo|Bar))&Baz type constructions are quite hard for me to parse visually.

Might be the |( and &( constructions. Unclear t.b.h., just noticed it's more of a strain.

Given how rarely such complex types happen I'm going to be fine either way :)

@edorian @Crell @phpfig

> But (ClassNameWithSomeLength|(Foo|Bar))&Baz type constructions are quite hard for me to parse visually.

That is incorrect syntax, because that's not in DNF 😃 (only & may appear within the parentheses). Parentheses may also not be nested.

Thus the only legal variants are:

- A|B|C
- A&B&C
- (A&B)|C|D

@edorian @Crell @phpfig

> having spaces is such an accessible thing for me, allowing me to parse the blob of symbols

For me it's the other way around: No spaces, please.

Without spaces the type visually appears as a single token and when seeing the space I know the variable name comes next. For me the variable name is the most important piece of information.

@Crell @phpfig We follow PSR-12 which according to section 5.6 (implicitly) requires spaces around types in an exception catch block. This is seen by tooling like PHPCS as a union type, so by extension ‘normal’ union types also require spaces to consistently adhere to this rule
@NanoSector @phpfig We should unify those, definitely. I can't recall seeing multi type catches in the wild very often, though.

@Crell @phpfig We use them a lot in our code base, first example that comes to mind is our CQRS queries which can either throw a “QueryException” aka something went catastrophically wrong with the query, or ItemNotFoundException; sometimes we want to catch and act on both when we *require* a result

Can be mitigated by making them extend a common exception type, and it’s probably not remotely representative; just my 2 cents :)

@Crell @phpfig a little of both depending on available white space. Haven't fully settled into one or the other.

@Crell it depends. If the class names are long, or if there are many of them, then I put the spaces in between in order to visually scan them faster. There are no fixes rules.

Or... maybe to rephrase: no spaces until my eyes hurt and it looks like a mess.

@Crell @phpfig no spaces, just like in phpdoc. I say this as a white space fanboy.
@Crell @phpfig Not that my opinion is worth much because I don't do accessibility, but I don't use spaces and seeing spaces in a type is just really weird to me.
@Girgias @Crell @phpfig Interesting POV: You don't have two or more types combined but a totally new type.
@Crell No spaces but if it’s a complicated expression, I’d look to redesign as it gets too hard to work out at a glance.