per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

Operator spacing union types / multi-catch expressions

Open jrfnl opened this issue 2 years ago • 3 comments

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: https://github.com/squizlabs/PHP_CodeSniffer/issues/3663

jrfnl avatar Mar 02 '23 16:03 jrfnl

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.

KorvinSzanto avatar Mar 02 '23 17:03 KorvinSzanto

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

jrfnl avatar Mar 02 '23 17:03 jrfnl

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: https://github.com/squizlabs/PHP_CodeSniffer/issues/3663

nonver2. 0_1.0

ijesneo611 avatar Dec 23 '23 02:12 ijesneo611

This has been addressed.

Crell avatar Jun 11 '25 13:06 Crell