问题
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
回答1:
I have git cloned WordPress coding standards into C:/wamp64/www/tools/phpcs
and I have created custom ruleset.xml
in C:/wamp64/www/tools/phpcs/bideja
<?xml version="1.0"?>
<ruleset name="Bideja">
<arg value="s"/>
<exclude-pattern>assets/*</exclude-pattern>
<exclude-pattern>node_modules/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<rule ref="WordPress">
<exclude name="WordPress.PHP.YodaConditions.NotYoda" />
<!-- exclude other stuff as you wish -->
</rule>
</ruleset>
Install new standards
phpcs --config-set installed_paths C:/wamp64/www/tools/phpcs/wp-coding-standards/wpcs,C:/wamp64/www/tools/phpcs/bideja
Check if standards are installed
phpcs -i
Set default standard
phpcs --config-set default_standard bideja
Install phpcbf
extension, add Environment variable for Path in Windows depending on your folder path
C:\Users\User\AppData\Roaming\Composer\vendor\bin
and modify User settings.json
with correct path and name of your standard
"phpcbf.executablePath": "C:\\Users\\User\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
"phpcbf.executablePathWindows": "C:\\Users\\User\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
"phpcs.standard": "bideja",
"phpcbf.standard": "bideja",
"phpcs.enable": true,
"phpcbf.onsave": true,
I hope I didn't forget something as I messed around this for hours but finally now after saving file VSCode fixes what it can.
来源:https://stackoverflow.com/questions/53635328/how-to-setup-php-codesniffer-that-extend-wordpress-coding-standards-autofix-er