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

PSR12.Operators.OperatorSpacing and PHP 8.0 union types #3151

Closed
rask opened this issue Oct 29, 2020 · 5 comments
Closed

PSR12.Operators.OperatorSpacing and PHP 8.0 union types #3151

rask opened this issue Oct 29, 2020 · 5 comments

Comments

@rask
Copy link

rask commented Oct 29, 2020

Describe the bug
When using the PSR-12 implementation that ships with PHP_CodeSniffer, I cannot use union types as described in RFCs and other official or semi-official documentation.

Code sample

function foo(int|float $val) : void
{
     //
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
    <rule ref="PSR12" />
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Lint the file while running PHP 8.0 so you can use union types
  2. Get greeted with PSR12.Operators.OperatorSpacing errors (either NoSpaceBefore or NoSpaceAfter):
ERROR | [x] Expected at least 1 space after "|"; 0 found

Expected behavior
I would expect to be able to use PSR-12 operator spacing lints without it complaining about union type delimiters, as those to my understanding are not actually operators.

Versions (please complete the following information):

  • OS: Linux Ubuntu
  • PHP: 8.0 RC 1
  • PHPCS: Latest 3.5 stable
  • Standard: PSR12

Additional context
N/A

@jrfnl
Copy link
Contributor

jrfnl commented Oct 29, 2020

Union types aren't supported yet in PHP_CodeSniffer.

A PR to fix this is open and expected to be merged in PHPCS 3.6.0: #3032

Also see #2968

@jrfnl
Copy link
Contributor

jrfnl commented Nov 2, 2020

As PR #3032 has been merged, this issue can be closed as fixed.

@gsherwood
Copy link
Member

Can confirm fixed

@fr0N73ND3r
Copy link

fr0N73ND3r commented Feb 6, 2023

Hello. This still throws an error in the "catch" section

    try {
    } catch (\Throwable|\Exception $e) {
    }
vendor/bin/phpcs --standard=PSR12 src/

ERROR | [x] Expected at least 1 space before "|"; 0 found
ERROR | [x] Expected at least 1 space after "|"; 0 found

60c83ab06712:/var/www/app# vendor/bin/phpcs --version
PHP_CodeSniffer version 3.7.1 (stable) by Squiz (http://www.squiz.net)

@jrfnl
Copy link
Contributor

jrfnl commented Feb 6, 2023

@fr0N73ND3r That's because those are not considered union types, but a multi-catch condition. May look the same, but it is technically something different.

I'd suggest bringing this up with PSR PER to add explicit rules about this.

Related to #3663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants