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.MultipleArguments false positive with arrow function argument #2895

Closed
gsherwood opened this issue Mar 8, 2020 · 5 comments
Milestone

Comments

@gsherwood
Copy link
Member

I have a problem.
Using 3.5.4 with PHP 7.4.2.

Code example:

<?php

declare(strict_types=1);

namespace Examples;

class ExampleClass
{
    public function doSomething(): void
    {
        print_r($this->filter());
    }

    private function cleanString(string $string, string $except): string
    {
        return str_replace($except, '', $string);
    }

    /**
     * @return array|string[]
     */
    private function filter(): array
    {
        $stringValues = [
            'foo',
            'bar',
        ];

        return array_filter(
            $stringValues,
            fn (string $value): string => $this->cleanString($value, 'b')
        );
    }
}
# vagrant @ homestead in ~/projects/php-quality-tools [14:00:15] C:2
$ ./vendor/bin/phpcs --version
PHP_CodeSniffer version 3.5.4 (stable) by Squiz (http://www.squiz.net)

# vagrant @ homestead in ~/projects/php-quality-tools [14:00:17]
$ ./vendor/bin/phpcs --standard="PSR2" testo.php -s

FILE: /home/vagrant/projects/php-quality-tools/testo.php
---------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------------------------------------------------
 31 | 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: 209ms; Memory: 6MB

Originally posted by @zlodes in #2523 (comment)

@gsherwood gsherwood added this to the 3.5.5 milestone Mar 8, 2020
@gsherwood
Copy link
Member Author

gsherwood commented Mar 8, 2020

Code to replicate:

$foo = array_filter(
    fn (): string => $this->cleanString($value, 'b')
);

And this:

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

@gsherwood
Copy link
Member Author

This is actually a problem with File::findEndOfStatement(), which is not able to figure out which token ends the arrow function. In this case, the close parenthesis of cleanString() is probably the token it should pick.

@gsherwood
Copy link
Member Author

Actually, this looks like it's a problem with scope detection for arrow functions nested in this way. The scope_closer for the arrow function is being set to the closing parenthesis for array_filter() and not the one for cleanString()

gsherwood added a commit that referenced this issue Mar 12, 2020
…s false positive with arrow function argument
@gsherwood
Copy link
Member Author

Turns out it was a problem with both.

gsherwood added a commit that referenced this issue Mar 12, 2020
…s false positive with arrow function argument
@designcise
Copy link

This issue persists with something like this:

fn (): ResponseInterface => (new JsonpResponse($data, $callback))->withStatus($statusCode)

It complains: Only one argument is allowed per line in a multi-line function call

However, that specific error goes away by refactoring it like this:

// ...
fn (): ResponseInterface => (new JsonpResponse(
    $data,
    $callback
))->withStatus($statusCode)

But then it complains about the indentation: Multi-line function call not indented correctly; expected 12 spaces but found 16

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

2 participants