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.
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:
- 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:
- 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.