Skip to content

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

@dlakomski

Description

@dlakomski

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions