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();
?>
I am using a combination of
phpcs
+phpcbf
(they ship together) andintelephense
to achieve WPCS sniffing and formatting.The two issues you mention:
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
andphpcbf
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: