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

UseArrowFunctionsFixer - introduction #4778

Merged
merged 1 commit into from
Apr 10, 2020

Conversation

gharlan
Copy link
Contributor

@gharlan gharlan commented Feb 2, 2020

$array = array_map(function (Foo $foo) use ($x) {
    return $foo->bar($x);
}, $array);

is fixed to:

$array = array_map(fn (Foo $foo) => $foo->bar($x), $array);

But only real one-liner return statements are transformed. If there is a comment before, or if the return statement uses more lines, there is no point in using an arrow function.
So these cases are not transformed:

foo(function () {
    // explanation for the return statement
    return 1;
});

foo(function () {
    return bar(
       $a,
       $b
    );
});

@gharlan gharlan force-pushed the use-arrow-functions branch 2 times, most recently from 8279357 to 9438853 Compare February 2, 2020 23:34
@julienfalque
Copy link
Member

if the return statement uses more lines, there is no point in using an arrow function

Can you elaborate please? I think it would be nice to cover those as well but maybe I'm missing something.

@gharlan
Copy link
Contributor Author

gharlan commented Feb 3, 2020

Maybe it is a matter of taste, but in my opinion the arrow functions without the braces are less readable for multiline return statements.
So for my part, I would like the fixer more, if it does not fix multiline return statements. But if you disagree, I'm open to change the behavior and fix them, too.

But would it be the responsibility of this fixer to change indentation?
When enabling fixing for multiline return statement, at the moment the fixer would fix this input:

foo($x, function () {
    return bar(
       $a,
       $b
    );
}, $y);

to:

foo($x, fn () => bar(
       $a,
       $b
    ), $y);

And this input:

foo($x, function ($a, $b) {
    return $a
        ->aMethodCall()
        ->otherMethodCall($b)
        ->thirdMethod();
}, $y);

to:

foo($x, fn ($a, $b) => $a
        ->aMethodCall()
        ->otherMethodCall($b)
        ->thirdMethod(), $y);

I think we should avoid these problems and the fixer should not touch these cases with multiline return statements.

@julienfalque
Copy link
Member

I do prefer to fix multiline return statements as well, @SpacePossum @keradus @kubawerlos WDYT? Maybe this can be an option?

But would it be the responsibility of this fixer to change indentation?

No, that would be another fixer.

@gharlan
Copy link
Contributor Author

gharlan commented Feb 23, 2020

do prefer to fix multiline return statements as well, @SpacePossum @keradus @kubawerlos WDYT?

Ping :)

@SpacePossum
Copy link
Contributor

SpacePossum commented Feb 24, 2020

I don't think I would use the multi line version. Therefore I'm fine if this is not part of the first version of the fixer. I think it can be added as an option later.
Thanks for all the work so far!

@julienfalque
Copy link
Member

👍 for implementing multiline support later, but without option. It's part of the intended scope of the rule, but it's not fully implemented yet (like we do with rulesets).

@gharlan gharlan force-pushed the use-arrow-functions branch from 9438853 to 30ff1ed Compare February 25, 2020 20:57
@SpacePossum SpacePossum added this to the 2.17.0 milestone Feb 27, 2020
@julienfalque julienfalque added the RTM Ready To Merge label Apr 10, 2020
@julienfalque
Copy link
Member

@gharlan Could you please rebase to fix the conflict?

@gharlan gharlan force-pushed the use-arrow-functions branch from 30ff1ed to f8692e4 Compare April 10, 2020 09:47
@keradus keradus force-pushed the use-arrow-functions branch from fc29b6d to 9e7a558 Compare April 10, 2020 12:30
@keradus keradus removed the RTM Ready To Merge label Apr 10, 2020
@keradus
Copy link
Member

keradus commented Apr 10, 2020

Thank you @gharlan.

@keradus keradus merged commit bd9771e into PHP-CS-Fixer:master Apr 10, 2020
julienfalque added a commit that referenced this pull request Apr 15, 2020
…(keradus)

This PR was merged into the 2.17-dev branch.

Discussion
----------

Add use_arrow_functions rule to PHP74Migration:risky set

follow up after #4778

Commits
-------

ef31e5a Add use_arrow_functions rule to PHP74Migration:risky set
@gharlan gharlan deleted the use-arrow-functions branch January 3, 2022 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants