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

feat: Introduce NumericLiteralSeparatorFixer #6761

Merged
merged 22 commits into from Jan 10, 2024

Conversation

muuvmuuv
Copy link
Contributor

@muuvmuuv muuvmuuv commented Jan 23, 2023

Closes: #6755

@muuvmuuv muuvmuuv changed the title Insert numeric literal separator feature: Insert numeric literal separator Jan 23, 2023
@coveralls
Copy link

coveralls commented Jan 23, 2023

Coverage Status

coverage: 94.805% (+0.02%) from 94.789%
when pulling 29433f7 on muuvmuuv:feature-proposal-6755
into 4842c3e on PHP-CS-Fixer:master.

Copy link
Contributor

@kubawerlos kubawerlos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have this fixer configured to add or remove the separator, according to the config?

Neither way is any standard that is good and the other is bad.

src/Fixer/Basic/NumericLiteralSeparatorFixer.php Outdated Show resolved Hide resolved
src/Fixer/Basic/NumericLiteralSeparatorFixer.php Outdated Show resolved Hide resolved
src/Fixer/Basic/NumericLiteralSeparatorFixer.php Outdated Show resolved Hide resolved
@muuvmuuv
Copy link
Contributor Author

Can we have this fixer configured to add or remove the separator, according to the config?

Neither way is any standard that is good and the other is bad.

Agree with it. Let's do:

  • true: add
  • false: remove
  • null: keep as is

I will try how easy it would be to do the other way around.

Is the position in the Fixer structure fine? Thought "basic" would match the most from what I saw.

@keradus
Copy link
Member

keradus commented Jan 23, 2023

not sure if agree, to be honest ;)

I would go YAGNI here. 123_345_678 is more readable than 123456789, I see way bigger value in fixer adding _ than in fixer removing _. To simplify the feature, I would suggest to have only adding(/fixing existing) _ and not removing them for first version of this Fixer rule, and consider to add option to remove existing _ only if more folks will actually need it.

@kubawerlos
Copy link
Contributor

not sure if agree, to be honest ;)

I would go YAGNI here. 123_345_678 is more readable than 123456789, I see way bigger value in fixer adding _ than in fixer removing _. To simplify the feature, I would suggest to have only adding(/fixing existing) _ and not removing them for first version of this Fixer rule, and consider to add option to remove existing _ only if more folks will actually need it.

@keradus I would prefer to remove it, also I can imagine ignoring it, but I cannot imagine adding numeric literals automatically, how would you fix these automatically:

const ONE_EURO_IN_CENTS = 1_00;
const THOUSAND_EUROS_IN_CENTS = 1_000_00;
const HUNDRED_THOUSAND = 100_000;
const ONE_MILLION = 1_000_000;

(source)

@muuvmuuv
Copy link
Contributor Author

@kubawerlos, in a first step, I remove the “maybe wrongly placed” underscores and then apply the formatting. So, that would become:

const ONE_EURO_IN_CENTS = 100;
const THOUSAND_EUROS_IN_CENTS = 100_000;

But I agree that some may prefer removing it or leaving as is. But as a first step, I want to make sure adding works as expected before removing.

@keradus
Copy link
Member

keradus commented Jan 29, 2023

@kubawerlos

A. why to remove _ from 12_345 to use 12345?

B. I see the value in having "add _ when not used" (convert 12345 into 12_345, but NOT touch 123_45 and option [false by default, can be added as later PR, if any] that would "rewrite _" (eg convert 123_45 into 12_345)

@kubawerlos
Copy link
Contributor

@keradus my reasoning is if we cannot automate adding _ then we should not add it at all. The option to not touch the number if already has some separators is fine only if we automate checking the added are in the correct place.

@muuvmuuv
Copy link
Contributor Author

Thats the reason I remove them first since you cannot be sure and one could misplaced them. I dont think someone wants them intentionally placed like that. I suggest to add it and wait for the community to ask for "remove", "keep as is" or possibilities to adjust the position etc.

@keradus
Copy link
Member

keradus commented Jan 29, 2023

I believe #6761 (comment) case shows that we must have "don't touch _ if already present" by default.

my reasoning is if we cannot automate adding _ then we should not add it at all.

Perfectly valid option, then just one not interested in it will not use the rule and that's it.

The option to not touch the number if already has some separators is fine only if we automate checking the added are in the correct place.

Can you elaborate on condition here? I believe this option is fine even if we don't automate checking - and because we cannot know what is the context of number, we will not be able to automate it.

@muuvmuuv muuvmuuv marked this pull request as ready for review January 30, 2023 08:01
@keradus
Copy link
Member

keradus commented Jan 30, 2023

I believe we can follow this path to make sure we are ready with state of fixer.
let's enable the fixer for this repo, and see if we will be happy with default config of it. If not, we need to groom it.

.vscode/settings.json Outdated Show resolved Hide resolved
src/Fixer/Basic/NumericLiteralSeparatorFixer.php Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

Since this pull request has not had any activity within the last 90 days, I have marked it as stale.

I will close it if no further activity occurs within the next 14 days.

Please keep your branch up-to-date by rebasing it when main branch is ahead of it, thanks in advance!

@Wirone Wirone changed the title feature: Insert numeric literal separator feat: Introduce NumericLiteralSeparatorFixer Jan 10, 2024
Copy link
Member

@Wirone Wirone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let myself approve and merge this, as several things were already discussed, I've verified all open discussions, implemented missing pieces and fixed requested stuff. I believe it's good to go 🙂.

@Wirone Wirone enabled auto-merge (squash) January 10, 2024 18:35
@Wirone Wirone merged commit 28e40bc into PHP-CS-Fixer:master Jan 10, 2024
26 checks passed
@muuvmuuv muuvmuuv deleted the feature-proposal-6755 branch January 10, 2024 18:45
@Wirone
Copy link
Member

Wirone commented Jan 10, 2024

Thank you very much @muuvmuuv 🍻!


Allowed values: ``'no_separator'`` and ``'use_separator'``

Default value: ``'no_separator'``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure of the default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was suggested by me here and agreed by @kubawerlos so I implemented it like this. All the years I've been programming I have never worked with codebase that used literal separators, that's why I believe it's sane default.

Copy link
Member

@keradus keradus Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, it was not possible for long in PHP, no doubts there is not adoption.

I aim to use everywhere personally, basically a must have for me for TS or python.
if both of you think this set of defaults is good, let's keep the defaults as-is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must personally agree with @keradus, but I am OK with it as long as I have the option to set it for myself.

danog pushed a commit to zoonru/PHP-CS-Fixer that referenced this pull request Feb 2, 2024
Co-authored-by: Greg Korba <greg@codito.dev>
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.

Force Numeric Literal Separator
7 participants