2018-08-01 05:59

有没有办法在netbeans 8.2(PHP)中为if-else单行块自动添加大括号?

  • code-formatting
  • formatting
  • php

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.

    echo 'yes';
    echo 'no';

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

    echo 'yes';
} else { 
    echo 'no';

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongpa9277 dongpa9277 3年前

    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.

    点赞 评论 复制链接分享
  • doujiu3768 doujiu3768 3年前

    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.

    点赞 评论 复制链接分享