phpstorm phpcs and wpcs Follow
Answered
I have installed phpcs, php cs fixer and wpcs via composer and globally. I have set them under Settings/Preferences | Languages & Frameworks | PHP | Quality Tools | Code Sniffer. When I try to my own custom coding standard I get a error.
phpcs: PHP Fatal error: Uncaught Error: Class 'WordPress\Sniffs\Security\NonceVerificationSniff' not found in C:\Users\Robert\AppData\Roaming\Composer\vendor\wpcs\WordPress\Sniffs\CSRF\NonceVerificationSniff.php:27
I have checked the path and NonceVerificationSniff.php is indeed there and it extending the class so I don't know what the problem is?
Please sign in to leave a comment.
Please describe your exact actions - what do you click, where the coding standard reside. Thank you!
This is when I am trying to enable. -- https://d.pr/i/sKmoGp
This is when I am trying to select the coding standard. -- https://d.pr/i/JeQMCD
When I try to select "custom" https://d.pr/i/0rSDMR is when the error occurs.
I am trying to use this as a example to build from --
<?xml version="1.0"?>
<ruleset name="Robert Parker">
<description>The PHP CodeSniffer rules.</description>
<!-- Exclude the Composer Vendor directory. -->
<exclude-pattern>/vendor/*</exclude-pattern>
<!-- Exclude the Node Modules directory. -->
<exclude-pattern>/node_modules/*</exclude-pattern>
<!-- Exclude all Javascript files. -->
<exclude-pattern>*.js</exclude-pattern>
<!-- Exclude minified CSS file. -->
<exclude-pattern>*.css</exclude-pattern>
<rule ref="WordPress">
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed"/>
<exclude name="WordPress.Security.NonceVerification"/>
</rule>
</ruleset>
Basically, what PhpStorm is trying to run is
phpcs --standard="C:\Users\exact\Documents\codesniffer.ruleset.xml" file.php
.Does this command work from CMD for you?
I was able to run PHP_CodeSniffer with your standard in PhpStorm, but I had to add these installed paths first as otherwise it said that the referenced sniff WordPress is missing:
c:\Users\eugene\AppData\Roaming\Composer\vendor\wp-coding-standards\wpcs\WordPress\,C:\Users\eugene\AppData\Roaming\Composer\vendor\wp-coding-standards\wpcs\WordPress-Core,C:\Users\eugene\AppData\Roaming\Composer\vendor\wp-coding-standards\wpcs\WordPress-Docs,C:\Users\eugene\AppData\Roaming\Composer\vendor\wp-coding-standards\wpcs\WordPress-Extra
I never had the exception you see, so it must be something about your installation. Please check how it runs from CMD.
It will not run from command prompt.
phpcs --standard="C:\Users\exact\Documents\codesniffer.ruleset.xml" file.php
PHP Fatal error: Uncaught Error: Class 'WordPress\Sniffs\Security\NonceVerificationSniff' not found in C:\Users\exact\AppData\Roaming\Composer\vendor\wpcs\WordPress\Sniffs\CSRF\NonceVerificationSniff.php:27
Stack trace:
#0 C:\Users\exact\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\autoload.php(167): include()
#1 C:\Users\exact\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\src\Ruleset.php(1136): PHP_CodeSniffer\Autoload::loadFile('C:\\Users\\exact\\...')
#2 C:\Users\exact\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\src\Ruleset.php(217): PHP_CodeSniffer\Ruleset->registerSniffs(Array, Array, Array)
#3 C:\Users\exact\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\src\Runner.php(301): PHP_CodeSniffer\Ruleset->__construct(Object(PHP_CodeSniffer\Config))
#4 C:\Users\exact\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\src\Runner.php(70): PHP_CodeSniffer\Runner->init()
#5 C:\Users\exact\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\bin\phpcs(18): PHP_CodeSniffe in C:\Users\exact\AppData\Roaming\Composer\vendor\wpcs\WordPress\Sniffs\CSRF\NonceVerificationSniff.php on line 27
Running phpcs --standard="C:\Users\exact\Documents\codesniffer.ruleset.xml" file.php would run it using Global phpcs executable & it would search for WPCS in a global composer installation. I believe you didn't install WPCS globally that's why this fails.
According to their docs phpcs.xml (note that ruleset should be named .phpcs.xml, phpcs.xml, .phpcs.xml.dist or phpcs.xml.dist) should be placed in the directory from which you run the CodeSniffer.
Please try to setup the project & phpcs config this way & try launching it in console - it should be working this way.
As for PhpStorm - I believe there's indeed an issue with phpcs custom ruleset & relative paths. I've added this scenario as an example at https://youtrack.jetbrains.com/issue/WI-45521. I hope we' will be able to investigate this issue closely with a responsible developer.
I try to install it globally -- composer global require "wp-coding-standards/wpcs=*"
It made no difference. I can check the path and the document is there with the extended class so I don't understand.
What is strange is the default coding standards are working -- WordPress, WordPress-Core, WordPress-Docs,WordPress-Extra, WordPress-VIP
phpcs --standard=WordPress file.php
What Dmitry suggested is exactly opposite - he suggested that you install PHP_CodeSniffer and the coding standards into the project, not globally.
Also, since it doesn't work from CMD, apparently the issue is not on the PhpStorm side. Honestly, it's hard to say what's going wrong there - as I said, your ruleset worked for me like a charm.
@Dmitry Tronin I installed it globally and it still fails. If I use any of the defined code standards it is working. I renamed my coding standard and I get the same error.
It is with my installation fo phpcs & wpcs. I don't know what yet but I got the same error trying to use it in Sublime Text 3. That is why I think it is with my installation and not phpstorm.
Try to start from the scratch: delete the wpcs & phpcs (if it's an option) & install and set it up anew using their documentation.
I found out my problem. I had two dependencies of wpcs and one was not installed. It was reading from that and why I got the error. Thanks for your help!
You're welcome!
I'm getting the same issue when using my custom ruleset: https://github.com/infinum/coding-standards-wp, because I'm referencing PHPCompatibilityWP ruleset (external). When I use it in terminal all is ok, because I'm installing the dealerdirect lib that handles the linking of the rulesets for me (https://github.com/Dealerdirect/phpcodesniffer-composer-installer), so it resolves everything for me. But for some reason, PHPStorm has issues with it.
Also, my ruleset and composer are in the plugin, and the project where I opened my PHPStorm is in the root of wp installation. Not sure if this has anything to do with it (even though I've set it up so that the composer is loaded from the plugin).
I think I solved it: https://youtrack.jetbrains.com/issue/WI-45521#focus=streamItem-27-3750486.0-0