Skip to content

Incorrect error with arrow function and parameter passed as reference #3049

Closed
@Daaarkling

Description

@Daaarkling

Describe the bug
When arrow function is used together with argument passed as reference (&), phpcs display this error "Expected 1 space after "&" operator; 0 found", which is wrong. if space is incorectly (but as suggested) added then phpcs display this error "Expected 0 spaces after reference operator for argument "$stdClass"; 1 found". phpcbf will simply fail on this. If function is written in old fashion way, everything works correctly.

Code sample

// correct (not considering other sniffs)
var_dump(...array_map(function (?stdClass &$stdClass) {return 1;}, [])); 

// Expected 1 space after "&" operator; 0 found
var_dump(...array_map(fn (?stdClass &$stdClass) => 1, [])); 

// 0 spaces after reference operator for argument "$stdClass"; 1 found
var_dump(...array_map(fn (?stdClass & $stdClass) => 1, []));

Custom ruleset
None.

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
3 | ERROR | [x] Expected 1 space after "&" operator; 0 found

3 | ERROR | [x] Expected 0 spaces after reference operator for argument "$stdClass"; 1 found

Expected behavior
There should be no error displayed with arrow function and zero space between & and paramerer name

Versions (please complete the following information):

  • OS: Ubuntu 20.04 LTS
  • PHP: PHP 7.4.7
  • PHPCS: 3.5.5, master

Additional context
None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions