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

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

Closed
Daaarkling opened this issue Aug 11, 2020 · 2 comments
Closed

Comments

@Daaarkling
Copy link

Daaarkling commented Aug 11, 2020

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.

@jrfnl
Copy link
Contributor

jrfnl commented Sep 5, 2020

@Daaarkling Thanks for reporting this. I can confirm the bug.

This bug affects both the PSR12.Operators.OperatorSpacing and the Squiz.WhiteSpace.OperatorSpacing sniffs. PR #3103 should fix this for both.

@gsherwood
Copy link
Member

The linked PR does fix this issue. The fix will be release in 3.5.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging a pull request may close this issue.

3 participants