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

binary_operator_spaces doesn't work in function #5667

Closed
volovodenko opened this issue Apr 29, 2021 · 8 comments
Closed

binary_operator_spaces doesn't work in function #5667

volovodenko opened this issue Apr 29, 2021 · 8 comments
Labels

Comments

@volovodenko
Copy link

volovodenko commented Apr 29, 2021

Bug report

  • PHP version: 8.0.2
    • PHP CS Fixer version: 2.18.6
    • the command used to run PHP CS Fixer (run with -vvv)
    • the configuration (file) you used

'binary_operator_spaces' => [
'operators' => [
'+=' => 'align_single_space_minimal',
'=' => 'align_single_space_minimal',
'=>' => 'align_single_space_minimal'
],
],

Code snippet that reproduces the problem

  <?php
  \DB::transaction(function () use ($entity) {
            $goalFile = new GoalFile();
            $goalFile->goal_id = $entity->goalId()->value();
            $goalFile->author_id = $entity->authorId()->value();
            $goalFile->original_name = $entity->name();
            $goalFile->description = $entity->description();

            $file = new File();
            $file->storage = $entity->attributes()->storage();
            $file->directory = $entity->attributes()->directory();
            $file->file_name = $entity->attributes()->fileName();
            $file->mime_type = $entity->attributes()->mimeType();
            $file->size = $entity->attributes()->size();
            $file->save();

            $goalFile->file()->associate($file);
            $goalFile->save();
        });

Also another type of issue with arrow functions

  <?php
  private function keys(): array
    {
        return [
            'email'                                       => fn ($value)                                       => mb_strtolower($value),
            'fio'                                         => fn ($value)                                         => $this->userNameParser($value),
            'inn'                                         => fn ($value)                                         => mb_strtoupper($value),
            'rol'                                         => fn ($value)                                         => mb_strtolower($value),
            'tabelnyi_nomer'                              => fn ($value)                              => mb_strtoupper($value),
            'nazvanie_podrazdeleniya'                     => fn ($value)                     => $this->divisionNameParser($value),
            'kod_podrazdeleniya'                          => fn ($value)                          => mb_strtoupper($value),
            'yavlyaetsya_li_rukovoditelem_podrazdeleniya' => fn ($value) => mb_strtolower($value),
            'roditelskoe_podrazdelenie'                   => fn ($value)                   => $this->divisionNameParser($value),
            'kod_roditelskogo_podrazdeleniya'             => fn ($value)             => mb_strtoupper($value),
            'nazvanie_dolznosti'                          => fn ($value)                          => $this->positionNameParser($value),
            'kod_dolznosti'                               => fn ($value)                               => mb_strtoupper($value),
        ];
    }
@GrahamCampbell
Copy link
Contributor

GrahamCampbell commented May 19, 2021

I don't see how the second example is a bug? The => in the fn () => code is not a binary operator - it's special syntax for that kind of function, so is out of scope of this fixer.

@volovodenko
Copy link
Author

volovodenko commented May 19, 2021

But why fixer aligns it randomly? If its out of him scope.

@GrahamCampbell
Copy link
Contributor

Oh, you didn't post the before file? I assumed the report was that it wasn't changing the arrows?

@volovodenko
Copy link
Author

image

@johanvanhelden
Copy link

johanvanhelden commented Jul 17, 2021

I stumbled upon the same issue. Hoping it was fixed in V3, but unfortunately it is not.

Rule:

'binary_operator_spaces' => [
    'default'   => 'single_space',
    'operators' => [
        '=>' => 'align_single_space_minimal',
    ],
],

Before:

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'flashNotifications' => fn () => Session::get('flash_notification'),
        'currentUser' => fn () => $this->getCurrentUser($request->user()),
        'menuItems' => fn () => $this->getMenuItems($request->user()),
    ]);
}

After:

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'flashNotifications' => fn () => Session::get('flash_notification'),
        'currentUser'        => fn ()        => $this->getCurrentUser($request->user()),
        'menuItems'          => fn ()          => $this->getMenuItems($request->user()),
    ]);
}

Expectation:

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'flashNotifications' => fn () => Session::get('flash_notification'),
        'currentUser'        => fn () => $this->getCurrentUser($request->user()),
        'menuItems'          => fn () => $this->getMenuItems($request->user()),
    ]);
}

@VincentLanglet
Copy link
Contributor

I think it will be fixed by #6112

SpacePossum added a commit that referenced this issue Feb 14, 2022
…f methods (VincentLanglet)

This PR was squashed before being merged into the master branch (closes #6112).

Discussion
----------

[BinaryOperatorSpacesFixer] Fix align of `=` inside calls of methods

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

Commits
-------

f7750fd [BinaryOperatorSpacesFixer] Fix align of `=` inside calls of methods
@SpacePossum
Copy link
Contributor

thanks for reporting, needed additional fixing, please see #6334

@SamuelWei
Copy link

SamuelWei commented Mar 18, 2022

I have a similar issue, not sure if it fits here or should be a new issue.

During upgrading of PHP-CS-Fixer (see littleredbutton/bigbluebutton-api-php#130) I changed the config from:

'binary_operator_spaces' => [
    'align_double_arrow' => true,
    'align_equals'       => true,
]

to:

'binary_operator_spaces' => ['operators' => ['=' => 'align', '=>' => 'align']]

As a result the expression inside the function argument is changed.
Can this be prevented by setting another option, is this a bug or intended?

Before

$hooksCreateParameters->setMeetingId($meetingId = $this->faker->uuid);
$hooksCreateParameters->setGetRaw($getRaw = $this->faker->boolean);

After

$hooksCreateParameters->setMeetingId($meetingId = $this->faker->uuid);
$hooksCreateParameters->setGetRaw($getRaw       = $this->faker->boolean);

Expected

$hooksCreateParameters->setMeetingId($meetingId = $this->faker->uuid);
$hooksCreateParameters->setGetRaw($getRaw = $this->faker->boolean);

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