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

One line if statement #159

Open
PrinceOfAbyss opened this issue Jan 6, 2025 · 4 comments
Open

One line if statement #159

PrinceOfAbyss opened this issue Jan 6, 2025 · 4 comments

Comments

@PrinceOfAbyss
Copy link

PrinceOfAbyss commented Jan 6, 2025

Hello. Is there an option to set phpfmt to exclude the one-liner if statements from formatting with braces?

I know it's a bad practice to do

if ( condition )
    echo 'test';

and I always use curly braces in my code in such cases, but there are specific cases, where for simplicity I'd like to be able to write one-liner if statements, ie.

if ( condition ) return; or if ( condition ) break;

It's so much easily readable (at least I'm used to it) and really saves space to be able to "evade" the whole braces formatting when putting the if condition and the code to execute in the same line.

@driade
Copy link

driade commented Jan 7, 2025

Hi @PrinceOfAbyss , good morning. The closests to what you need would be excluding the filter "AddMissingCurlyBraces".

Captura de pantalla 2025-01-07 a las 13 53 02

But it'll affect all of your code. There's no rule "AddMissingCurlyBracesIfConditionOnSameLine".

@PrinceOfAbyss
Copy link
Author

Would you consider adding a setting for that @driade ?

@driade
Copy link

driade commented Jan 8, 2025

Hello @PrinceOfAbyss

At this time there are no plans to add more rules to this extension, except for bug fixes and ensuring that the existing rules continue to work with new versions of PHP.

Best regards.

@PrinceOfAbyss
Copy link
Author

Fair enough @driade ! I tried your suggestion and indeed it works exactly as I wanted it to.

I mean, if I put the if() expression and a single statement in the same line, it formats it as it should.

Then, if I add curly braces around the statement, again it formats it as it should!

The only problem would be for someone who forgets to put curly braces around multiple statement if()'s, but that's not a problem for me, I always use them in such cases.

So all in all, it's doing my work exactly as I wanted it to. Thanks a lot for your support!

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

No branches or pull requests

2 participants