Can we set up Intelephense in VS Code as per WordPress coding Standard?

2.5k views Asked by At

I am using PHPCS and PHPCBF for WordPress development in VS Code.
( as per instructions mentioned in https://github.com/tommcfarlin/phpcs-wpcs-vscode )

While PHPCBF is formatting the codes as per WPCS, I still feel that the code looks really ugly, especially when Html tags are present in the PHP document.

When I use Intelephense to format the Php codes, the code looks visually pleasant including properly indented Html tags. The tradeoff of using Intelephense is that the code is not WPCS compliant anymore. There is an option to include WordPress as stubs in Intelephense but that is only for including WordPress specific functions and not related to formatting.

Some issues faced while formatting with Intelephense are:

  1. No space after opening and before the closing parenthesis, (phpcs)
  2. There must be no blank line before the file comment (phpcs)
<?php
// this is a blank line inserted by Intelephense which is causing error no.2 provided in summary above
/**
 * This is a sample file
 *
 * @package something.
 * something something
 * something something
 */

get_header();
if (have_posts()) {
    while (have_posts()) {
// No space is inserted by Intelephense After opening and before Closing parenthesis which is causing error no.1 provided in summary above
        the_post(); ?>
        <h2>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a>
        </h2>
        <?php the_content(); ?>
        <hr>
<?php
    }
}
get_footer();
?>
2

There are 2 answers

0
xyeres On

I am using a combination of phpcs + phpcbf (they ship together) and intelephense to achieve WPCS sniffing and formatting.

The two issues you mention:

  1. No space after opening and before the closing parenthesis, (phpcs)
  2. There must be no blank line before the file comment (phpcs)

I fixed these by configuring phpcbf to use the "WordPress-Core" standard. (While phpcs uses "WordPress" standard)

This has been the only way I've been able to enforce WPCS whitespace requirements without simply excluding those rules in a phpcs.xml file

In VS Code we have the phpcs and phpcbf extensions which allow you to run the two when a file is opened and saved. They let you pass config settings from your VS Code settings.json, the settings would look like this for phpcbf:

"phpcbf.standard": "WordPress-Core"
0
zoltankundi On

This issue was closed with the conclusion "This extension doesn't use phpcbf though", so I'm not sure how you could do it specifically with Intelephense for VS Code.

But if you're looking for a solution to use phpcbf as your formatter for php files in VS Code, and your phpcbf is set up and you have all your coding standards installed, you can use phpcbf from Per Soderlind.

Then, if you reload the editor and try to format a php file with Ctrl+Shift+I (or your preferred method) a popup will appear where you have to select your preferred formatter.

enter image description here

then you should select the one that says valeryanm.vscode-phpsab

enter image description here

Or if the popup doesn't appear, just add the following to your settings.json:

"[php]": {
        "editor.defaultFormatter": "valeryanm.vscode-phpsab"
    },

Now anytime you try to format it phpcbf will run on the whole document.