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

PSR2.Methods.FunctionCallSignature false positive when arrow function has array return type #2773

Closed
jasonmm opened this issue Dec 18, 2019 · 4 comments
Milestone

Comments

@jasonmm
Copy link

jasonmm commented Dec 18, 2019

This might be related to #2523.

Using the file:

<?php

declare(strict_types=1);

print_r(array_map(
    fn(string $n): array => [1, bcadd($n, '3')],
    ['1', '2', '3']
));

Produces the error:

$ phpcs --standard=PSR12 -s ./test2.php

FILE: test2.php
-------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------
 6 | ERROR | [x] Only one argument is allowed per line in a multi-line function call
   |       |     (PSR2.Methods.FunctionCallSignature.MultipleArguments)
-------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------

Time: 54ms; Memory: 6MB

This seems specific to an arrow function that has an array return type with a multiple argument function call in the body of the arrow function. Using fn(string $n): string => bcadd($n, '3') or fn(string $n): array => [$n, 3] as the arrow function produces no error from phpcs.

@gsherwood gsherwood added this to the 3.5.4 milestone Jan 6, 2020
@gsherwood gsherwood changed the title PHP 7.4 arrow function returning array with multiple argument function call PSR2.Methods.FunctionCallSignature false positive when arrow function has array return type Jan 6, 2020
gsherwood added a commit that referenced this issue Jan 6, 2020
…hen arrow function has array return type
@gsherwood
Copy link
Member

Thanks for the report. The fix will be in 3.5.4.

@cl1ck
Copy link

cl1ck commented Feb 21, 2020

When an arrow function with an array return value is the last function argument, this still reports an incorrect error. I have the following in my code:

return $this->mapResults(
    $dql,
    fn (array $row) => [$row[0], $row[3]]
 );

@jrfnl
Copy link
Contributor

jrfnl commented Feb 21, 2020

@cl1ck You may want to open a new issue as 1) this is closed and 2) your issue is actually a different one.

Your issue can be reproduced even without the array type declaration and without the first argument.
As far as I can see, it has to do with the , within the short array confusing the sniff into thinking there are multiple arguments.

Oh and the actual problem is in the PEAR.Functions.FunctionCallSignature sniff in the processMultiLineCall() method.

@gsherwood
Copy link
Member

I opened issue #2895 for the issue above. Someone else also commented on another closed issue with the same general problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants