Home » Php » php – VScode PHPCS Extension Error: Referenced Sniff "WordPress-Core" does not exist

php – VScode PHPCS Extension Error: Referenced Sniff "WordPress-Core" does not exist

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to add PHP CodeSniffer to VScode.

Within VScode I am getting the error 'phpcs: Referenced sniff "WordPress-Core" does not exist'

However when I run the following command in the terminal:

phpcs --standard="WordPress-Core" dropdowns.php

PHP CodeSniffer works as expected with the following terminal output:

--------------------------------------------------------------------------------
FOUND 34 ERRORS AND 17 WARNINGS AFFECTING 51 LINES
--------------------------------------------------------------------------------
  15 | WARNING | [x] Array double arrow not aligned correctly; expected 10
     |         |     space(s) between "'id'" and double arrow, but found 1.
  17 | WARNING | [x] Array double arrow not aligned correctly; expected 4
     |         |     space(s) between "'taxonomy'" and double arrow, but found
     |         |     1.
  18 | WARNING | [x] Array double arrow not aligned correctly; expected 5
     |         |     space(s) between "'orderby'" and double arrow, but found
     |         |     1.
  34 | WARNING | [x] Equals sign not aligned with surrounding assignments;
     |         |     expected 10 spaces but found 1 space
  35 | WARNING | [x] Equals sign not aligned with surrounding assignments;
     |         |     expected 7 spaces but found 1 space
  36 | ERROR   | [x] Short array syntax is not allowed
  44 | WARNING | [x] Equals sign not aligned with surrounding assignments;
     |         |     expected 2 spaces but found 1 space
  84 | WARNING | [x] Equals sign not aligned with surrounding assignments;
     |         |     expected 2 spaces but found 1 space

My settings.json file looks like this:

{
    "workbench.colorTheme": "Default Light+",
    "window.zoomLevel": 0,
    "phpcs.standard": "WordPress",
    "phpcs.executablePath": "/Users/michelle/.composer/vendor/bin/phpcs",
    "phpcs.enable": true,
    "phpcs.showWarnings": true,
    "phpcs.showSources": true,
}

My phpcs.ruleset.xml looks like this:

<?xml version="1.0"?>
<ruleset name="WordPress Theme Coding Standards">
    <description>My Project's Coding Standards</description>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-Docs"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-Extra"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-Core"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPressVIPMinimum"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-VIP-Go"/>
</ruleset>
How to&Answers:

I solved this error by updating the rule references in this file:
~/.composer/vendor/wp-coding-standards/wpcs/WordPressto also be absolute paths. Final ruleset.xml for WordPress coding standards now looks like this:

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="WordPress" namespace="WordPressCS\WordPress" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">

    <description>WordPress Coding Standards</description>

    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-Core"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-Docs"/>
    <rule ref="~/.composer/vendor/wp-coding-standards/wpcs/WordPress-Extra">
        <!-- Prevent duplicate messages + deprecation notice from deprecated sniff. -->
        <exclude name="WordPress.WP.TimezoneChange.timezone_change_date_default_timezone_set"/>
        <exclude name="WordPress.WP.TimezoneChange.DeprecatedSniff"/>
    </rule>

</ruleset>