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:
- No space after opening and before the closing parenthesis, (phpcs)
- 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();
?>
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.
then you should select the one that says
valeryanm.vscode-phpsabOr if the popup doesn't appear, just add the following to your
settings.json:Now anytime you try to format it phpcbf will run on the whole document.