Using CodeSniffer in a PHP project. (phpcs: ERROR: Referenced sniff "PHPCSUtils" does not exist. )

I am trying to get PHPCodesniffer to work with PHPStorm. I have Codesniffer installed and operating on the command line. I am using the WordPress coding standards since I am working on a WordPress plugin. Codesniffer works on the command line and it will identify errors and fix them. It would be great to have it within PHPStorm. I went and tried to configure it.

1. I went into preferences and set up CodeSniffer like this:

As you can see, it validated correctly.

2. I then went to the PHP > Quality Tools > PHP_CodeSniffer and set it up like this:

I checked and the files in wp-coding-standards are in the epcs folder.

My phpcs.xml.dist has this configuration:
<?xml version="1.0"?>
<ruleset name="Project WordPress Rules">
  <description>WordPress Coding Standards for this project.</description>

  <rule ref="WordPress-Core"/>
  <rule ref="WordPress-Docs"/>
  <rule ref="WordPress-Extra"/>

  <file>./</file>

  <exclude-pattern>vendor/*</exclude-pattern>
  <exclude-pattern>node_modules/*</exclude-pattern>
  <exclude-pattern>build/*</exclude-pattern>
</ruleset>

Finally, I set up PHP > Quality Tools > PHP CS Fixer like this:

Any time I open I file I get these errors:

PHP CS Fixer: ERROR: The file "fix" does not exist. Run "phpcbf --help" for usage information

phpcs: ERROR: Referenced sniff "PHPCSUtils" does not exist. ERROR: Referenced sniff "Universal.NamingConventions.NoReservedKeywordParameterNames" does not exist. ERROR: Referenced sniff "Universal.WhiteSpace.CommaSpacing" does not exist. ERROR: Referenced sniff "Universal.WhiteSpace.CommaSpacing.TooMuchSpaceAfterCommaBeforeTrailingComment" does not exist. ERROR: Referenced sniff 

does not exist. ERROR: Referenced sniff "Universal.Files.SeparateFunctionsFromOO" does not exist. ERROR: Referenced sniff "Universal.CodeAnalysis.NoEchoSprintf" does not exist. ERROR: Referenced sniff "Universal.CodeAnalysis.NoDoubleNegative" does not exist. ERROR: Referenced sniff "Universal.Attributes.BracketSpacing" does not exist. ERROR: Referenced sniff "Universal.Attributes.DisallowAttributeParentheses" does not exist. Run "phpcs --help" for usage information

Any advice on how to fix this would be appreciated.

0

Hello,

Sorry for the late reaction.

If you still experience this issue, you may want to just untick the “Installed standarts path” option and everything should work.

The reason why the error appears is because when you set the option like that, CodeSniffer is switched to use it as the default directory for standards, thus built-in ones are no longer “seen”. 

This option is the same as -runtime-set installed_paths project\vendor\wp-coding-standards\wpcs

But as WordPress standarts rely on default ones, you got the “referenced sniff does not exist” error.

When you install WPCS as a Composer dependency, its standarts are added to `installed_paths` automatically, vendor/squizlabs/php_codesniffer/CodeSniffer.conf:

<?php
 $phpCodeSnifferConfig = array (
  'installed_paths' => '../../phpcsstandards/phpcsextra,../../phpcsstandards/phpcsutils,../../wp-coding-standards/wpcs',
);
?>
0

Thanks Vasilly. Any comment is appreciated no matter when. I was able to get it to work on the command line and get through bringing the code up to standard. I will try out your suggestion next time I am making changes to my plugins.

0

请先登录再写评论。