VScode PHPCS Extension Error: Referenced Sniff "WordPress-Core" does not exist
Asked Answered
C

3

10

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>
Claar answered 18/2, 2020 at 16:2 Comment(0)
C
3

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>
Claar answered 19/2, 2020 at 14:44 Comment(2)
This was useful to me: jasonyingling.me/…Three
and of course the docs: github.com/squizlabs/PHP_CodeSniffer/wiki/…Three
D
1

There is a good chance that you're using multiple versions of phpcs.

Try, in a shell:

which phpcs

If you get something like /usr/bin/phpcs, you have the package installed with your system's package manager. Either manage it using the package manager -- install WordPress-Core with apt or brew or whatever you're using -- or remove it (then try again).

If you don't get a result, try to install PHPCS globally using Composer. Make sure ~/.config/composer/vendor/bin is in your PATH.

At this point, you know the path of the executable that you want to use, and you know where it is. Now:

phpcs -i

That will give you a list of installed standards. Mine says: MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, PHPCompatibility, PHPCompatibilityParagonieRandomCompat, PHPCompatibilityParagonieSodiumCompat, PHPCompatibilityWP, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress.

You know where your executable is and you know how to modify it (either Composer with global, or apt, or brew, or whatever). Keep installing until you get the right thing.

If you're using a PHPCS extension in VS Code and you haven't specified a full path to the executable, it may be checking in your project path. Given how easy it is to set up Composer with lots of stuff, it may be finding another copy of the executable.

Set the extension to point to the correct executable. Now it should be able to find all the standards you need.

Also make sure that you're not running multiple VS Code extensions. If you're anything like me, when you move to a new install, you go and install 50 extensions of all the things you think you may need one day. Some meta-extensions come with PHPCS, commonly bundled with a formatter, or, "everything you need to code in VS Code in PHP." Make sure that either:

  • only one extension is linting, or,
  • all linters are pointing to the correct phpcs executable.

This should fix this issue permanently.

Dominus answered 5/6, 2021 at 19:52 Comment(0)
I
1

you need to make all installed coding standards available to phpcs:

phpcs --config-set installed_paths full-wpcs-path\WordPress,full-wpcs-path\WordPress-Core,full-wpcs-path\WordPress-Docs,full-wpcs-path\WordPress-Extra

see phpcs.standard in: https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs

Inspection answered 14/6, 2022 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.