Problem/Motivation

When using version 8.3.14 from January 28, I get "ERROR: Referenced sniff "SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" does not exist":

When using version 8.3.13, it works as expected.

Steps to reproduce

composer global require \
  drupal/coder

phpcs --standard=Drupal example.module 
ERROR: Referenced sniff "SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" does not exist
composer global require \
  drupal/coder:8.3.13

phpcs --standard=Drupal example.module 
----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
  1 | ERROR | [x] Missing file doc comment
 14 | ERROR | [x] Expected 1 space after FUNCTION keyword; newline
    |       |     found
 17 | ERROR | [x] Multi-line function declarations must define one
    |       |     parameter per line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

I am running this within a Docker container [using this project](https://github.com/dcycle/docker-php-lint).

Proposed resolution

Coder 8.3.14 now depends on the SlevomatCodingStandard and will setup the phpcs installed_paths configuration automatically on composer install with the dealerdirect/phpcodesniffer-composer-installer plugin. During the composer install process you need to answer Do you trust "dealerdirect/phpcodesniffer-composer-installer" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?] with "y" (yes, we trust this plugin to setup PHPCS paths).

Please also remove all extra commands that set the installed_paths with phpcs from any Coder setup scripts you use.

If you have a composer vendor directory already then you might have an outdated vendor/squizlabs/php_codesniffer/CodeSniffer.conf file there. Reinitialize that:

rm -rf vendor/squizlabs/php_codesniffer/
composer install

Again make sure that you trust the "dealerdirect/phpcodesniffer-composer-installer" plugin. Your composer.json file should look similar to this:

{
    "require": {
        "drupal/coder": "^8.3"
    },
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true
        }
    }
}

The composer install should print PHP CodeSniffer Config installed_paths set to ../../drupal/coder/coder_sniffer,../../sirbrillig/phpcs-variable-analysis,../../slevomat/coding-standard, then you know the new Slevomat coding standard path was configured.

Remaining tasks

Fix Drupal.org testbot script which has a hard-coded --config-set installed_paths command.
#3283978: Remove --config-set installed_paths (Referenced sniff does not exist)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alberto56 created an issue. See original summary.

alberto56’s picture

Here are my automated test results using 8.3.13: https://app.circleci.com/pipelines/github/dcycle/docker-php-lint/14/work...

Here is my pull request on my Dockerized version of coder, which changes from 8.3.13 to the latest version: https://github.com/dcycle/docker-php-lint/pull/4/files

Here is the failing test for this pull request: https://app.circleci.com/pipelines/github/dcycle/docker-php-lint/15/work...

And here is the test being run: https://github.com/dcycle/docker-php-lint/blob/master/test.sh

This happens locally on a ARM macbook and on a VM with AMD as well.

IT-Cru’s picture

@alberto56: I run into similar issue today with my Gitlab CI PHPCS job.

Problem for me was that I run set in this CI job PHPCS config manually

non working PHPCS config: phpcs --config-set installed_paths /var/www/html/vendor/drupal/coder/coder_sniffer

working PHPCS config: phpcs --config-set installed_paths /var/www/html/vendor/drupal/coder/coder_sniffer,/var/www/html/vendor/sirbrillig/phpcs-variable-analysis,/var/www/html/vendor/slevomat/coding-standard

My manual config overrides the one from composer install. So maybe this manual config set could completely removed from my CI job script.

Grevil’s picture

I can confirm this issue. I am using dockerized Drupal via ddev and getting the same error as @alberto56. 8.3.13 is also working fine for me.

alberto56’s picture

Thanks @Grevil. Unfortunately in my Docker tests @IT-Cru's solution does not work.

alberto56’s picture

Title: ERROR: Referenced sniff "SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" does not exist » On 8.3.14 (but not 8.3.13): ERROR: Referenced sniff "SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" does not exist
Grevil’s picture

I also tried @IT-Cru solution, but unfortunately it doesn't work for me either.

Sweetchuck’s picture

I ran into this issue, because I had a custom Composer script on the "post-update-cmd" event, with something like this: "phpcs --config-set installed_paths ...".
I removed this custom script.

dealerdirect/phpcodesniffer-composer-installer does the job very well.

Sweetchuck’s picture

I think the documentation should be updated, because the path "vendor/drupal/coder/coder_sniffer" simply not enough.

https://www.drupal.org/docs/contributed-modules/code-review-module/insta...

phpcs --config-show installed_paths
Using config file: /path/to/project/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

installed_paths: ../../drupal/coder/coder_sniffer,../../sirbrillig/phpcs-variable-analysis,../../slevomat/coding-standard,/path/to/project/vendor/drupal/coder/coder_sniffer
IT-Cru’s picture

I've tested remove of phpcs --config-set installed_paths... command yesterday in another project and for me this is working fine, because during composer install it is set with correct parameters.

This also happens in @alberto56's CircleCI job as far as I see at https://app.circleci.com/pipelines/github/dcycle/docker-php-lint/15/work...

But in https://app.circleci.com/pipelines/github/dcycle/docker-php-lint/15/work... this is/was overridden again. And then the error occurs.

alexpott’s picture

hitesh.koli’s picture

#3 setting the right installed_paths worked for me.

joachim’s picture

I'm getting the same error running this programatically in Drupal Code Builder's tests: https://github.com/drupal-code-builder/drupal-code-builder/blob/380ca748...

klausi’s picture

Issue summary: View changes

Sorry, not sure if we can do anything in Coder directly - please remove all extra commands that set the installed_paths with phpcs. This is now done automatically when you install Coder 8.3.14. By setting them manually you are missing the new SlevomatCodingStandard that Coder depends on and then the installed_paths configuration for phpcs is wrong.

Added this to the release notes: "Coder 8.3.14 now depends on the SlevomatCodingStandard and will setup the phpcs installed_paths configuration automatically on composer install. Please remove all extra commands that set the installed_paths with phpcs from any Coder setup scripts you use." https://www.drupal.org/project/coder/releases/8.3.14

Please don't use Homebrew to install Coder as that is not really supported by us maintainers. We had an argument on the docs discussion page https://www.drupal.org/node/1419988/discuss where BramDriesen wanted to keep the Homebrew instructions, but I think we should delete them again.

jweowu’s picture

please remove all extra commands that set the installed_paths with phpcs

If you have a phpcs.xml file then you would remove any installed_paths line like this:

  <config name="installed_paths" value="vendor/drupal/coder/coder_sniffer/" />

Deleting that will prevent the specified value from clobbering the default (and correct) value, which is now established automatically.

Chi’s picture

For me the only way to work around this issue was installing the slevomat manually.
<config name="installed_paths" value="vendor/slevomat/coding-standard"/>

jweowu’s picture

Chi: That's interesting. I'd initially tried adding that exact same element, but quickly found I actually needed to use the following diff to get the right result:

-  <config name="installed_paths" value="vendor/drupal/coder/coder_sniffer/" />
+  <config name="installed_paths" value="vendor/drupal/coder/coder_sniffer/,vendor/slevomat/coding-standard/" />

Despite it being XML (and despite commas being valid in filenames!), the whole list apparently needed to be stuffed into a single value attribute, CSV style.

Subsequent reading had caused me to realise that deleting that line entirely was the better fix, and that worked equally well for me.

geoanders’s picture

Issue seems to be around slevomat coding standard/format.

- Downgrading drupal/coder (8.3.14 => 8.3.13)
- Removing slevomat/coding-standard (7.0.18)

After downgrading, all is well.

Useful site: https://gorannikolovski.com/blog/referenced-sniff-slevomat-coding-standa...

agileadam’s picture

Thank you, @geoanders. I downgraded coder (composer global require drupal/coder:8.3.13) and it fixed the issue right away.

uditrawat’s picture

Thank you @geoanders, @agileadam,
Downgrading the coder fixed the issue.

uditrawat’s picture

jonathan1055’s picture

I see this error on contrib testing on drupal.org at Core 9.4 (which uses Coder 8.3.15). Tests at Core 9.3 which use Coder 8.3.13 run ok.

The project's phpcs.xml.dist file does not have the installed_paths config, so this must be added by the test script being run on d.o.

Has anyone else seen this on d.o. contrib testing and if so, did you manage to fix it?

There is no reference to this on https://www.drupal.org/project/drupalci_testbot so I might raise it there.

jonathan1055’s picture

jonathan1055’s picture

Issue summary: View changes

Updated IS to include link to drupal.org testbot issue

jonathan1055’s picture

For anyone who's contrib project has a phpcs.xml.dist file and they want to gets PHPCS running for testing on drupal.org at Core 9.4+ and have it compatible with running locally, here is what you can add into the phpcs.xml.dist

<!-- Temporary fix until drupal.org testbot script is changed. This is also
  compatible with running phpcs locally, and on Travis, so can be committed.
  See https://www.drupal.org/project/drupalci_testbot/issues/3283978 -->
<config name="installed_paths" value="../../drupal/coder/coder_sniffer/,../../sirbrillig/phpcs-variable-analysis/,../../slevomat/coding-standard/"/>

This gets away from the hard-coded /var/www/html/vendor, is compatible with Travis testing, and is also OK all versions of coder.
Hope this helps.

klausi’s picture

Issue summary: View changes

Thank you jonathan1055 for finding and reporting that problem!

Added some more help to the issue summary to get a correct vendor/squizlabs/php_codesniffer/CodeSniffer.conf config file for old vendor directories.

I will leave this issue open to collect more help and tricks on upgrading.

klausi’s picture

Issue summary: View changes

Added more important info to the issue summary: During the composer install process you need to answer Do you trust "dealerdirect/phpcodesniffer-composer-installer" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?] with "y" (yes, we trust this plugin to setup PHPCS paths).

Your composer.json file should look similar to this:

{
    "require": {
        "drupal/coder": "^8.3"
    },
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true
        }
    }
}
afsch’s picture

In my case I opted to downgrade coder and php_codesniffer specifying the version which is still compatible with Drupal 9.4.1.

composer require drupal/coder:8.3.13
composer require --dev squizlabs/php_codesniffer:3.6.2

abdulaziz zaid’s picture

#3 worked for me.

CB’s picture

Thanks alexis_s, #28 worked for me.

AaronBauman’s picture

Version: 8.3.14 » 8.3.x-dev

issue persists to 8.3.16 and presumably dev

rpayanm’s picture

I fixed it after reading this article:
https://gorannikolovski.com/blog/referenced-sniff-slevomat-coding-standa...

Before (Coder 8.3.13 and earlier):

bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer && 
bin/phpcs -p --colors --standard=Drupal,DrupalPractice --extensions=php,module,install web/modules/custom/

After (Coder 8.3.14 and later):

bin/phpcs -p --colors --standard=Drupal,DrupalPractice --extensions=php,module,install web/modules/custom/
reflie’s picture

I also faced into this issue. I was receiving mentioned error in PhpStorm.

In my case the problem was in wrong installed path in Project_Default.xml file. I just removed that line and the error disappeared.

mandclu’s picture

I was having this same problem, and couldn't fix it with the suggestions in the comments. I realized that I had installed phpcs via homebrew, so I installed it there and reinstalled it via composer, and now it works.

enaznin’s picture

hello @mandclu, #34 could you please elaborate on the steps, please? I installed phpcs with homebrew as well and facing the same issue

AnokR’s picture

Hello @all, I was having same error But downgrading the global coder version to "8.3.13" run the command composer global require drupal/coder:8.3.13 fix my issue.
Hope it should helpfull.
Thanks

Anwaikurn’s picture

#36 works for me.

Also after downgrading Coder to the 8.3.13 version you can upgrade it again to the latest one and Slevomat coding standards appear in phpcs -i. At least it did a "trick" for me.

AaronBauman’s picture

after R'ing TFA, I realized I needed to make a couple updates to my project's composer.json to get this working on latest coder stable.

Remove any config-set step from composer.json:
bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer

Then remove dependencies which are now integrated with this module:

composer remove squizlabs/php_codesniffer dealerdirect/phpcodesniffer-composer-installer
composer update drupal/coder -W

Good to go after that.

shivakumarshidkapla’s picture

Thank you, @geoanders. I downgraded coder (composer global require drupal/coder:8.3.13) and it fixed the issue right away.

#18 worked for me thank you @geoanders

xeM8VfDh’s picture

same issue for me on latest but not on 8.3.16

Nick Hope’s picture

The downgrade-then-upgrade trick in #37 worked for me.

Downgrade to 8.3.13:
composer global require drupal/coder:8.3.13
Upgrade to the latest version (currently 8.3.18):
composer global require drupal/coder
Verify installed coding standards:
phpcs -i outputs (for me):
The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, Drupal, DrupalPractice, VariableAnalysis and SlevomatCodingStandard

jonnyhocks’s picture

Just to expand on #33, I was also experiencing the issue with PHPStorm even though the issue had been resolved when running with CLI.

I removed the installed paths section from the .idea/php.xml file. I'm guessing that some config is saved in different places depending on which version of PHPStorm you are using.

dynamdilshan’s picture

#18 worked for me as I followed the instructions from this resource below:
https://gorannikolovski.com/blog/referenced-sniff-slevomat-coding-standa...

I'm on D9.5, PHP 8.1 and the latest version of coder module.

selwynpolit’s picture

Just to elaborate on #42. I found that by editing the .idea/php.xml file, even while PHPStorm is open, and removing the line:

    <option name="installedPaths" value="$PROJECT_DIR$/vendor/drupal/coder/coder_sniffer" />

from the section displayed below, the error immediately went away.

  <component name="PHPCodeSnifferOptionsConfiguration">
    <option name="codingStandard" value="Drupal" />
    <option name="highlightLevel" value="WARNING" />
    <option name="installedPaths" value="$PROJECT_DIR$/vendor/drupal/coder/coder_sniffer" />
    <option name="useInstalledPaths" value="true" />
    <option name="transferred" value="true" />
  </component>
Dimetry’s picture

#44 does work. Many thanks.

Sivaji_Ganesh_Jojodae’s picture

#36 works. Downgrading drupal/coder (8.3.21 => 8.3.13).

pfrenssen’s picture

Category: Bug report » Support request

Changing this to a support request since this is not a bug in coder but rather a configuration issue on the user side.

fgm’s picture

Seeing how it's so common since it happens as the default in the most popular IDE, I would say it's rather a bug, but that's your call.

TomTech’s picture

I just encountered this issue.

You shouldn't need to edit any PhpStorm XML files. There is a setting for this.

Seems that JetBrains was being helpful, as this is configuration specifically documented for this module(and similar implementations): PHP - Quality Tools - PHP Code Sniffer.

You can clear the field and uncheck the "Installed standards path" to remove this setting in PhpStorm.

PhpStorm - Settings - PHP - Quality Tools - phpcs

Seems some communication should be made with JetBrains that the latest version of coder requires multiple paths, not a single path. (Or could we just symlink the other directories into the coder directory as an alternative?)

emb03’s picture

#36 worked for me

j. ayen green’s picture

@TomTech #49 (using ddev and PhPStorm) ... I owe you a beer!

selwynpolit’s picture

Hopefully I captured the steps correctly here to save others some time: https://selwynpolit.github.io/d9book/setup_mac#code-sniffing