How to setup PHP CodeSniffer for project with personal rules that extend WordPress coding standards + autofix errors in VSCode on save?
I have installed CodeSniffer globally
composer global require "squizlabs/php_codesniffer=*"
WordPress coding standards are installed inside theme folder (so inside project they are at /wp-content/themes/bideja/wpcs
)
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
Inside theme folder I have created phpcs.xml
which should inherit WordPress standards so I can customize it further
<?xml version="1.0"?>
<ruleset name="Bideja">
<description>Sniffs</description>
<config name="installed_paths" value="wpcs" />
<arg value="s"/>
<exclude-pattern>assets/*</exclude-pattern>
<exclude-pattern>node_modules/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<rule ref="WordPress-VIP">
<exclude name="Generic.Files.EndFileNewline.NotFound" />
<exclude name="WordPress.PHP.YodaConditions.NotYoda" />
</rule>
</ruleset>
In Bash terminal inside theme, I can scan pages for errors and fix them
phpcs page.php
phpbcf page.php
But how can VSCode fix them on save? I get popup error:
phpcs: Referenced sniff "WordPress-VIP" does not exist
What should I place in User settings.json?
"phpcs.enable": true,
"phpcs.standard": "WordPress", // ?
I have tried setting
phpcs --config-set installed_paths wpcs
When I check installed standards with phpcs -i
I can see that WordPress standards are listed
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP
Also is CodeSniffer able to auto indent PHP and HTML or is that beyond of its capabilites? I am struggling with good indentation in VSCode
phpcs ./path/to/code --standard=./path/to/ruleset.xml
in acomposer.json
script, instead of changing settings – Flynt