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

PHP 8 and PSR12: bitwise or vs union type #3201

Closed
dlakomski opened this issue Jan 25, 2021 · 7 comments
Closed

PHP 8 and PSR12: bitwise or vs union type #3201

dlakomski opened this issue Jan 25, 2021 · 7 comments

Comments

@dlakomski
Copy link

Describe the bug
CodeSniffer do not distinguishes between "bitwise or" operator and union type.

Code sample

<?php
namespace test;

final class Country
{
    private string $code;

    public function __construct(string $code)
    {
        $this->code = $code;
    }

    public function equals(Country|string $country): bool
    {
        if ($country instanceof self) {
            return $this->code === $country->code;
        }

        return $this->code === $country;
    }
}

To reproduce
Steps to reproduce the behavior:

  1. Create a file called Country.php with the code sample above.
  2. Run phpcs --standard=PSR12 Country.php
  3. See error message displayed
FILE: Country.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
 14 | ERROR | [x] Expected at least 1 space before "|"; 0 found
 14 | ERROR | [x] Expected at least 1 space after "|"; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Expected behavior
I expected that error not to be reported, because the union type is not a operator described in PSR12 that needs spaces around it.

Versions (please complete the following information):

  • OS: Ubuntu 20.04
  • PHP: 8.0.1
  • PHPCS: 3.5.8
  • Standard: PSR12
@phanan
Copy link

phanan commented Jan 25, 2021

I don't believe this library is PHP 8 ready yet.

@jrfnl
Copy link
Contributor

jrfnl commented Jan 25, 2021

CodeSniffer do not distinguishes between "bitwise or" operator and union type.

@dlakomski AFAIK this has already been fixed via #3032 three months ago. Please test with the master branch to confirm.

I don't believe this library is PHP 8 ready yet.

@phanan This is not true. Nearly all PHP 8 syntaxes are by now supported, safe for match and attributes. Everything else has already been addressed, though not all fixes are in a released version of PHPCS yet. See #3182 for further detail.

@phanan
Copy link

phanan commented Jan 25, 2021

@jrfnl Unless I'm mistaken, PHP 8 support hasn't been released. The latest tag was last year October, which simply breaks a lot if run against a PHP 8 codebase.

Edit: On second look, 3.5.8 is the version @dlakomski uses as well, so it only makes sense that it breaks. I believe if he used constructor param promotion e.g., __construct(private string $code) the breakage would be even worse. Not sure if using master is the proper fix.

@jrfnl
Copy link
Contributor

jrfnl commented Jan 25, 2021

@phanan The 3.5.8 release - or rather the 3.5.7 - release already contained a number of PHP 8 related fixes, but yes, as I said before, while most of the rest of the fixes have been made, not all fixes have been released.

And no, I have no clue when 3.6.0 will be released.

@dlakomski
Copy link
Author

@jrfnl yes, on master everything is ok. Is there any chance to release patch version that will fix it? I'm trying to avoid using dev versions of packages in my application.

@jrfnl
Copy link
Contributor

jrfnl commented Jan 26, 2021

Is there any chance to release patch version that will fix it?

No, this will go into the next minor 3.6.0 as the fix applied introduces a new token which is something which can't go into a patch release. (semver)

Regarding the planning: as I said before - that's not my area and I have no influence on it.

@gsherwood
Copy link
Member

Closing as duplicate of #3032. Version 3.6.0 is close to release. The items remaining are currently here: https://github.com/squizlabs/PHP_CodeSniffer/milestone/25

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