php-fig/per-coding-style

Operator spacing union types / multi-catch expressions

jrfnl opened this issue · 3 comments

jrfnl commented

I believe some clarification is needed about the spacing requirements around the | operator when used in the following two situations:

  • Union types
  • Multi-catch statements

At this moment, most coding standards I'm aware of use "no spaces around the | operator" as a rule for union types.

However, multi-catch statements predate union types and the example in section 5.6 had spaces around the | operator.
PHPCS has taken the literal interpretation based on the example and expects one space on either side of the | operator in multi-catch statements.
CS-Fixer treats the | in multi-catch as if it were a union type, expecting no spaces.

End-users are confused ;-/

Ref: squizlabs/PHP_CodeSniffer#3663

See #45

We have some issues around consistency that won't be resolved before v2.0.0 in catch (Foo|Bar $e) and what syntax looks like in multiline function declarations.
In the meantime I'd just pick the paradigm that makes the most sense to you and stick to it, if the spec disagrees with your choice in the future this is a relatively safe bit of syntax to update with a static analysis tool.

jrfnl commented

Thanks for that link, I'll read up on the discussion.

In the meantime I'd just pick the paradigm that makes the most sense to you and stick to it, if the spec disagrees with your choice in the future this is a relatively safe bit of syntax to update with a static analysis tool.

For context: I'm not asking for me/my own code. I'm asking as a maintainer of/contributor to various PHPCS standards (i.e. as someone having to field support questions about this).

I believe some clarification is needed about the spacing requirements around the | operator when used in the following two situations:

  • Union types
  • Multi-catch statements

At this moment, most coding standards I'm aware of use "no spaces around the | operator" as a rule for union types.

However, multi-catch statements predate union types and the example in section 5.6 had spaces around the | operator.
PHPCS has taken the literal interpretation based on the example and expects one space on either side of the | operator in multi-catch statements.
CS-Fixer treats the | in multi-catch as if it were a union type, expecting no spaces.

End-users are confused ;-/

Ref: squizlabs/PHP_CodeSniffer#3663

nonver2. 0_1.0