phpstorm phpcs and wpcs

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?

14 comments
Comment actions Permalink

When I try to my own custom coding standard I get a error.

Please describe your exact actions - what do you click, where the coding standard reside. Thank you!

0
Comment actions Permalink

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>

0
Comment actions Permalink

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?

0
Comment actions Permalink

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.

0
Comment actions Permalink

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

0
Comment actions Permalink

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.

0
Comment actions Permalink

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.

0
Comment actions Permalink

What is strange is the default coding standards are working -- WordPress, WordPress-Core, WordPress-Docs,WordPress-Extra, WordPress-VIP

phpcs --standard=WordPress file.php

0
Comment actions Permalink

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.

0
Comment actions Permalink

@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.

0
Comment actions Permalink

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.

0
Comment actions Permalink

Try to start from the scratch: delete the wpcs & phpcs (if it's an option) & install and set it up anew using their documentation.

1
Comment actions Permalink

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!

0
Comment actions Permalink

You're welcome!

0

Please sign in to leave a comment.