WordPress / WordPress-Coding-Standards

PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions
MIT License
2.54k stars 474 forks source link

phpcs: ERROR: Referenced sniff "PHPCSUtils" does not exist #2396

Closed theMikeD closed 11 months ago

theMikeD commented 11 months ago

Bug Description

I have installed the standards per the instructions using composer. I have then set phpStorm to use the project-specific phpcs et al, and the standards installed via composer all appear fine.

However my phpStorm log is filled with phpcs: ERROR: Referenced sniff "PHPCSUtils" does not exist

Minimal Code Snippet

    "require-dev": {
        "wp-coding-standards/wpcs": "^3",
        "sirbrillig/phpcs-variable-analysis": "^2.8",
        "brain/monkey": "2.*",
        "phpunit/php-code-coverage": "^9.2",
        "phpunit/phpunit": "^9",
        "wp-phpunit/wp-phpunit": "^5",
        "yoast/phpunit-polyfills": "^1.0",
        "dealerdirect/phpcodesniffer-composer-installer": "^1.0"
    }

The issue happens when phpStorm attempts to scan a php file.

Environment

Question Answer
PHP version 8.1.22
PHP_CodeSniffer version 3.7.2
WordPressCS version 3.0.1
PHPCSUtils version 1.0.8
PHPCSExtra version 1.1.2
WordPressCS install type Composer project local
IDE (if relevant) PhpStorm 2023.2.2

I see the phpcsstandards folder in vendor
The vendor folder is in a non-standard location, set by

    "config": {
        "vendor-dir": "client-mu-plugins/vendor",
        "allow-plugins": {
            "composer/installers": true,
            "dealerdirect/phpcodesniffer-composer-installer": true,
            "php-http/discovery": true
        }
    },

Additional Context (optional)

I do not get the same warning running on the command line using

 phpcs -ps functions.php --standard=WordPress-Extra

Tested Against develop Branch?

Don't know how to do this.

jrfnl commented 11 months ago

If everything works fine on the command line, this is not a WPCS issue, but a PHPStorm issue. I suggest you ask in a PHPStorm forum for support, though based on your description it sounds like PHPStorm is using a different PHPCS version than the one you installed in the project, which would explain the problem.

Suggest: close as asked in the wrong repo.

P.S.: you should be able to remove the "dealerdirect/phpcodesniffer-composer-installer": "^1.0" requirement as that is now a dependency of WPCS, so to prevent conflicts in the future, you could just let WPCS (or rather PHPCSUtils) handle the supported versions.

theMikeD commented 11 months ago

As is usual in my life, the act of describing the issue has revealed the solution.

In my case, i had specified a folder in Preferences --> PHP --> Quality Tools, Installed Standards path. I had that set to vendor/wp-coding-standard/wpcs and as soon as I cleared it and restarted, everything started working again. The lightbulb went off after reading the first FAq answer on this page and realizing that the standard would not have to be set manually any more.

At least there is a record for this in case someone else has the same issue.

jrfnl commented 11 months ago

@theMikeD Ah, yes, that very much explains it. Happy to hear that the PHPCSUtils FAQ helped you solve this.

diegovogel commented 11 months ago

At least there is a record for this in case someone else has the same issue.

THANK YOU @theMikeD. This was driving me mad.