1

I have got phpcs installed in Visual Studio Code, and I have got the following lines in file settings.json:

{
    // PHPCS
    "phpcs.enable":   true,
    "phpcs.standard": "WordPress",
}

This seems to have installed correctly and if I have the following code written in a file.

        foreach($array as $a ) {
            //Some Code here
        }

The following errors are returned in the Problem Tab of the Terminal.

Space after opening control structure is required <br/>
No space before opening parenthesis is prohibited <br/>
No space after opening parenthesis is prohibited

I am able to correct these errors manually and the issues goes away from the Problem Tab. Here is how my code looks after fixing manually.

        foreach ( $array as $a ) {
            // Some Code here.
        }

However, if I select the above code block and click on Format Selection, the IDE formats the code as below and again all the errors come back in the Problem Tab.

        foreach ($array as $a) {
            // Some Code here.
        }

Is there a way to use the Format Selection and have it follow the WordPress standards?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ascsoftw
  • 3,466
  • 2
  • 15
  • 23
  • Not sure why you are getting those errors, all three versions of your loop are correct, the spacing makes no difference other than neatness and ease-of-reading. It's just standard PHP... – Frits Jul 17 '19 at 09:32
  • 1
    Wordpress Standard says there should be a space before the opening and closing bracket after foreach. I just want Visual Studio to format my code as per WP Standards https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#space-usage – ascsoftw Jul 17 '19 at 09:37
  • I get it, but it makes absolutely 0 difference to the end result of your code. It's the same argument regarding `{` - on the same line as the function declaration or on the next line? Same result, best practice overall is just to standardize it across your team and your code, but as far as your code functionality is concerned, it makes absolutely no difference... – Frits Jul 17 '19 at 09:40
  • Yes, I understand it doesn't make any difference to the execution of the code. However to submit a plugin to WP Repo these standards must be followed. If IDE can not format the code this way, I will have to do it manually. – ascsoftw Jul 17 '19 at 09:43
  • Fair enough - I hadn't considered that you were working to create something for the repo. – Frits Jul 17 '19 at 09:45
  • The WordPress coding standards are strange. It is like the [vengeance of the outdated 1935 Wren & Martin textbook](https://english.stackexchange.com/questions/4645/is-it-ever-correct-to-have-a-space-before-a-question-or-exclamation-mark#comment206109_4645). Now it is even harder to get rid of. – Peter Mortensen Dec 23 '19 at 16:03

0 Answers0