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

[BinaryOperatorSpacesFixer] Fix align of = inside calls of methods #6112

Merged
merged 1 commit into from Feb 14, 2022

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Nov 16, 2021

Hi. I found a bug when using align/align_with_space_minimal for =.

Before, any time a ( was found, the check was skipped until the ).

Now, I do this only for ( for function definition and for/foreach/if/while/...

So

$this->foo(function () {
$a = 1;
$aa = 2;
})

aligns the =.

It fix my personal issue (I added a test), and I think it will fix the issue #5667

@coveralls
Copy link

coveralls commented Nov 16, 2021

Coverage Status

Coverage increased (+0.002%) to 93.153% when pulling f7750fd on VincentLanglet:fixEqual into 37e2cbd on FriendsOfPHP:master.

@VincentLanglet
Copy link
Contributor Author

@SpacePossum @keradus Hi, would you have time to take a look to this fix ? Does something need to be improved ?
Thanks :)

@VincentLanglet
Copy link
Contributor Author

Friendly ping @keradus @SpacePossum. Is something wrong/missing with my PR ?

@VincentLanglet
Copy link
Contributor Author

Friendly ping @keradus @SpacePossum ; would you mind taking a look at this bugfix ? :)

@SpacePossum
Copy link
Contributor

SpacePossum commented Feb 8, 2022

Hi and thanks for the PR!

This rule is kind of notorious complex and a good candidate for complete rewrite some day. As such, reviewing is also complex.

The first change seems to aim to never align what is inside the ( ) of a function signature. This seems good to me, I think we can extend with also fn, for example $fn1 = fn($x = 1) => $x + 3;. However the need for skip is because of the second change;

The second change I'm not 100% I follow, why only skip alignment in those new cases and not always in any ( ?

Can you update the sample to the more simple one:

            [
                '<?php
m(
    function ()
    {
        $d["a"]   = 1;
        $d["abc"] = 2;
    }
);
',
                '<?php
m(
    function ()
    {
        $d["a"] = 1;
        $d["abc"] = 2;
    }
);
',
            ],

we also need a test with $fn1 = fn($x = 1) => $x + 3;

I need some more time to check some cases

@VincentLanglet
Copy link
Contributor Author

Can you update the sample to the more simple one:

Sure, done.

we also need a test with $fn1 = fn($x = 1) => $x + 3;

I don't understand what should be tested.

@SpacePossum
Copy link
Contributor

Thanks for updating/simplify the sample, much appreciated 👍

I'm thinking along these lines,
lets say we have

<?php

fn ($x = 1) => $x + 3;
$f = 123;

it will get fixed to:

--- test.php
+++ test.php
@@ -13,4 +13,4 @@
  fn ($x = 1) => $x + 3;
-$f = 123;
+$f     = 123;

which I think is not correct

The change to if ($token->isGivenKind(T_FUNCTION)) { should be extended not to just test for function signatures but also for these cases, i.e. if ($token->isGivenKind([T_FUNCTION, T_FN])) {

@VincentLanglet
Copy link
Contributor Author

I'm thinking along these lines, lets say we have

<?php

fn ($x = 1) => $x + 3;
$f = 123;

it will get fixed to:

--- test.php
+++ test.php
@@ -13,4 +13,4 @@
  fn ($x = 1) => $x + 3;
-$f = 123;
+$f     = 123;

which I think is not correct

The change to if ($token->isGivenKind(T_FUNCTION)) { should be extended not to just test for function signatures but also for these cases, i.e. if ($token->isGivenKind([T_FUNCTION, T_FN])) {

You were right, it was fixing. I added the test and the fix.

@SpacePossum
Copy link
Contributor

SpacePossum commented Feb 14, 2022

Thank you @VincentLanglet , apologies for the long wait, PR is much appreciated 👍

@SpacePossum SpacePossum merged commit ae42466 into PHP-CS-Fixer:master Feb 14, 2022
@VincentLanglet
Copy link
Contributor Author

Thank you @VincentLanglet , apologies for the long wait, PR is much appreciated 👍

No problem, I know it's not always easy to find time to maintain such a big project.
I'll be happy to provide some others PR ;)

@stayallive
Copy link

This seems to "break" (in my projects) on 2 fronts:

         if ($tenant !== null) {
-            $tenantUrl = $tenant->url;
+            $tenantUrl                     = $tenant->url;
         } elseif (!empty($sessionTenantUrl = session('tenant.url'))) {
-            $tenantUrl = $sessionTenantUrl;
+            $tenantUrl                     = $sessionTenantUrl;
         }

The above seems to be fixed by #6333, the other case is:

-    $parser = new QueryParser(
+    $parser    = new QueryParser(
         $query = 'modifier:single'
     );

So I think this was a good change but missed some edge cases it looks like.

Happy to help where I can but thought to report it here first 👍

@VincentLanglet
Copy link
Contributor Author

@stayallive #6333 and #6334 are doing some fixes.

If the second PR is still not fixing your use case, please report it

keradus added a commit that referenced this pull request Mar 15, 2022
…paulbalandan, SpacePossum)

This PR was merged into the master branch.

Discussion
----------

BinaryOperatorSpacesFixer - Fix for alignment in `elseif`

Caused by #6112
Fixes #6329

Commits
-------

e01726a more test
85ee54e BinaryOperatorSpacesFixer - Fix for alignment in `elseif`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants