PHP-CS-Fixer icon indicating copy to clipboard operation
PHP-CS-Fixer copied to clipboard

BinaryOperatorSpacesFixer - Single space minimum option

Open stevelacey opened this issue 6 years ago • 8 comments

Assuming this is of interest I'll tidy it up as per the contribution guide.

This allows a config like this:

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

Which allows a less drastic change than either forcing single spaces or forcing alignment for all = and => but still enforcing at least one space – which are currently the only available options afaics.

The naming of this is tricky, I think the other names are a bit confusing, I also thought about calling this single_space_or_more to distinguish it from align_single_space_minimal. Is that better?

An alternative (or extra) implementation I'd probably prefer would be single_space_or_align... but that'd be a lot harder to throw together than this was.

For comparison, on my current project, using single_space or align_single_space_minimal resulted in 300+ file changes, given there's a whole mix of legitimately aligned and single-spaced things, whilst single_space_minimum caused 5 file changes, sniping out the specific problem I wanted to address: zero spaces.

Thoughts? Shall I tidy this up and add the docs and tests?

Related: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/4396

stevelacey avatar Aug 09 '19 14:08 stevelacey

@SpacePossum @keradus ?

stevelacey avatar Aug 15 '19 13:08 stevelacey

@julienfalque @nicolas-grekas?

stevelacey avatar Aug 26 '19 19:08 stevelacey

No opinion on my side as I don't think I'd use this rule. Single space is my all time favorite :)

nicolas-grekas avatar Aug 26 '19 19:08 nicolas-grekas

Hi and apologies for getting back so late.

Am I correct to understand you want something like this:

$a =$b&&$c; // fix
$d =$a   &&    $e; // don't fix

being fixed to this:

$a =$b && $c; // fix
$d =$a   &&    $e; //don't fix

? It might help having unit tests as an example :)

Im +0 on adding the option, but leave the remark that Im very much in favor to rename the options in 3.0.0 because there is a lot of confusion we could get rid of :+1:

SpacePossum avatar Oct 08 '19 15:10 SpacePossum

@SpacePossum yes, whilst those examples aren't really what I'm after, that's what the effect would be, in short, I want to enforce a minimum of 1 space, without enforcing or disallowing alignment.

If it were trivial to write "single space or alignment" as a rule I'd do that, but that's pretty complex.

I am happy to spend more time on this and submit update this PR with tests... if it'd ever merge.

stevelacey avatar Oct 09 '19 04:10 stevelacey

if it'd ever merge.

I cant promise that as we would have to see what others think as well, but I would vote for it :) There are a few details to sort out though, not sure if you want to work on this or if you want to wait for more people expressing support for your PR in concept?

SpacePossum avatar Oct 09 '19 06:10 SpacePossum

I would like to voice my support for this, having one space with optional alignment is ideal imo.

AJenbo avatar Nov 04 '19 14:11 AJenbo

@stevelacey This option would be great !

I'm just scared about the confusion SINGLE_SPACE -> ALIGN_SINGLE_SPACE_MINIMAL SINGLE_SPACE_MINIMUM -> ALIGN_SINGLE_SPACE

VincentLanglet avatar Nov 30 '19 14:11 VincentLanglet

@stevelacey if you still want this to be merged, please provide tests for this feature, and - obviously - rebase on current master.

Wirone avatar Jun 12 '23 15:06 Wirone