Skip to content

PSR12.Operators.OperatorSpacing interprets pipe character in catch declaration as binary operator #3663

Open
@pschultz

Description

@pschultz

Describe the bug

PSR12.Operators.OperatorSpacing produces false positives on type alternatives in catch clause.

Code sample

<?php

try {
} catch (A|B $exception) {
}

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs -s --standard=PSR12 test.php
  3. See error message displayed
FILE: /tmp/tmp.0QHXuAapol/foo.php
---------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------------------
 4 | ERROR | [x] Expected at least 1 space before "|"; 0 found
   |       |     (PSR12.Operators.OperatorSpacing.NoSpaceBefore)
 4 | ERROR | [x] Expected at least 1 space after "|"; 0 found
   |       |     (PSR12.Operators.OperatorSpacing.NoSpaceAfter)
---------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------

Expected behavior

No error, since the pipe character in the code is not an operator. This already works as expected for function declarations, i.e. this is fine:

<?php

function f(A|B $x)
{
}

Versions (please complete the following information):

  • OS: Fedora 53
  • PHP: 8.0
  • PHPCS: 3.7.1
  • 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