0

I am using Netbeans 8.2 for my PHP project. It was an already build one. When code, I saw so many areas where using if-else conditions without braces like below.

if(1<2) 
    echo 'yes';
else 
    echo 'no';

I don't like this syntax and want to change them to

if(1<2){
    echo 'yes';
} else { 
    echo 'no';
}

Is there any inbuilt option or custom plugin to do the same?

Arun
  • 3,316
  • 6
  • 33
  • 71
  • Are you looking for a language-specific solution, or are you looking for something more general? – skomisa Aug 04 '18 at 05:54
  • @skomisa, I am looking for PHP programming. – Arun Aug 07 '18 at 11:13
  • Possible duplicate of [Netbeans code format braces in PHP single line statements](https://stackoverflow.com/q/13801657/2985643), though no solution is offered unfortunately. – skomisa Aug 11 '18 at 03:17

2 Answers2

0

I don't think it is possible to automatically correct all if-else statements missing a brace in a single pass, but the functionality to automatically add missing braces on an individual basis for PHP source is built in to NetBeans:

First ensure that if-else statements without braces are reported:

  • Tools > Options > Editor > Hints > Language > PHP.
  • Ensure If-Else Statements must Use Braces is checked.
  • Select the appropriate severity level (Error, Warning, etc.) and click OK.

IfElseBracesOption

Then build your project containing if-else statements without braces:

  • I chose to report missing if-else braces as errors, so errors were reported on the line following if and also on the line following else for the PHP code in the screen shot below:
  • Placing the cursor on the offending line shows a tooltip with the message If-Else Statements Must Use Braces:

tooltipPrompt

  • Press Alt-Enter and you will be prompted Add Braces.
  • Press Enter and the braces will be added automatically, and the cursor positioned on the following line:

bracesAdded

  • Note that the braces were only added for the if statement, but the associated error is now gone.
  • You have to go through the same process again to correct the else statement.

This approach is not ideal, but it is reliable, and you only need to got through the exercise once.

skomisa
  • 12,583
  • 7
  • 47
  • 75
0

I fixed this issue. Added php-cs-fixer to Netbeans. And it fixed the issue. php-cs-fixer will fix coding standard issues. So the above mentioned is a coding standard issue and php-cs-fixer fixed it.

Arun
  • 3,316
  • 6
  • 33
  • 71