Php-cs-fixer: binary_operator_spaces should have a single_space_minimal option

Created on 29 Apr 2019  Â·  5Comments  Â·  Source: FriendsOfPHP/PHP-CS-Fixer

In rule: binary_operator_spaces,
There is an align_single_space and an align_single_space_minimal option.

I had some difficulties to understand the way they worked but if I'm not wrong

  • align_single_space: Align operator with at least one space before and after
  • align_single_space_minimal: Align operator with exactly one space before and after

So there should be

  • single_space: At least one space before and after
  • single_space_minimal: Exactly one space before and after (The actual single_space behaviour)
kinfeature request

Most helpful comment

I also came here looking for single_space_minimal – the use case is that I feel enforcing either alignment or 1 space only is too aggressive, and doesn't look great in a bunch of cases on the project I'm working on, I want is to enforce at least 1 space around all operators, and allow for the alignment of = and => but not enforce it

        'binary_operator_spaces' => [
            'default' => 'single_space', // exactly one
            'operators' => [
                '=' => 'single_space_minimal', // at least one
                '=>' => 'single_space_minimal', // at least one
            ]
        ],

I currently set = and => to null which gets me nice single spaces on everything else and looks great, but I am forced to pick between aligning everything or aligning nothing on those 2, and I don't want to pick 🙃

Edit: Now that I look over the definition example for align_single_space_minimal I think what I want is more single_space_miniMUM or at_least_one than what that describes

All 5 comments

Hi!

I think your observations are correct for the two align options; one will align to the operators and assures a single space, the other will do the same but assures minimal space on the right side of the operators.

For the not-aligning options, single_space is indeed your single_space_minimal description.

So if understand correctly you want a non-aligning option that assure a single space, but allows for multiple spaces?
So:

$a = $b+$c;

would be fixed to

$a = $b + $c;

however;

$a = $b              +      $c;

would not be fixed?

Yes @SpacePossum

I didn't look at the code, but I think it wouldn't be difficult to implement this option.
I think the more coherent should be to rename the single_space option as single_space_minimal and writing the real single_space option, but it implies breaking changes...

I think the options were added later and therefore the naming might not be as coherent as could be, however we took a practical way of adding these without BC breaking changes.

For the requested missing option; I'm not sure I see a use case for this one. Currently having multiple spacing left and/or right of the operator is only possible for alignment reasons. I think that is valid, but having such spacing for a single operator is not a code style I've seen around much. Do you maybe have an example project that uses this code style?

In our code base, we aligned = when we declared const,

    const GENDER_MISTER  = 'gender.mister';
    const GENDER_MADAM   = 'gender.mrs';
    const GENDER_MISS    = 'gender.miss';
    const GENDER_COMPANY = 'gender.company';
    const GENDER_OTHER   = 'gender.other';
    const GENDER_UNKNOWN = 'gender.unknown';

But we use one space for anywhere else.

I know it's not a classic usecase.

I also came here looking for single_space_minimal – the use case is that I feel enforcing either alignment or 1 space only is too aggressive, and doesn't look great in a bunch of cases on the project I'm working on, I want is to enforce at least 1 space around all operators, and allow for the alignment of = and => but not enforce it

        'binary_operator_spaces' => [
            'default' => 'single_space', // exactly one
            'operators' => [
                '=' => 'single_space_minimal', // at least one
                '=>' => 'single_space_minimal', // at least one
            ]
        ],

I currently set = and => to null which gets me nice single spaces on everything else and looks great, but I am forced to pick between aligning everything or aligning nothing on those 2, and I don't want to pick 🙃

Edit: Now that I look over the definition example for align_single_space_minimal I think what I want is more single_space_miniMUM or at_least_one than what that describes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ro0NL picture ro0NL  Â·  3Comments

kcloze picture kcloze  Â·  3Comments

aidantwoods picture aidantwoods  Â·  3Comments

Bilge picture Bilge  Â·  3Comments

datenmeister picture datenmeister  Â·  3Comments