I'm facing the following issue with my Visual Code OSS, whenever I open my Drupal Workspace:
phpcs: Uncaught Error: Call to undefined method DrupalPractice\Project::register() in ~/.config/composer/vendor/squizlabs/php_codesniffer/src/Ruleset.php:1218
When I take a closer look, everything seems ok... is there something that I've missed?
$ pwd
~/.config/composer/vendor/squizlabs/php_codesniffer/src/Standards
$ ls -al
total 48
drwxr-xr-x 10 joe joe 4096 jan 13 17:11 .
drwxr-xr-x 11 joe joe 4096 apr 11 2019 ..
lrwxrwxrwx 1 joe joe 67 jan 13 17:11 Drupal -> ~/.config/composer/vendor/drupal/coder/coder_sniffer/Drupal
lrwxrwxrwx 1 joe joe 76 jan 13 17:11 DrupalPractice -> ~/.config/composer/vendor/drupal/coder/coder_sniffer/DrupalPractice/
drwxr-xr-x 5 joe joe 4096 apr 11 2019 Generic
drwxr-xr-x 4 joe joe 4096 apr 11 2019 MySource
drwxr-xr-x 5 joe joe 4096 apr 11 2019 PEAR
drwxr-xr-x 5 joe joe 4096 apr 11 2019 PSR1
drwxr-xr-x 5 joe joe 4096 apr 11 2019 PSR12
drwxr-xr-x 5 joe joe 4096 apr 11 2019 PSR2
drwxr-xr-x 5 joe joe 4096 apr 11 2019 Squiz
drwxr-xr-x 5 joe joe 4096 apr 11 2019 Zend
$ cat ~/.config/composer/vendor/drupal/coder/coder_sniffer/DrupalPractice/ruleset.xml
<?xml version="1.0"?>
<ruleset name="DrupalPractice">
<description>Drupal best practice checks</description>
<!-- All Drupal code files must be UTF-8 encoded and we treat them as such. -->
<arg name="encoding" value="utf-8"/>
<arg name="extensions" value="php,module,inc,install,test,profile,theme,yml"/>
<autoload>../Drupal/autoload.php</autoload>
<rule ref="Internal.NoCodeFound">
<!-- Empty files are fine, might be used for testing. -->
<exclude-pattern>*</exclude-pattern>
</rule>
<rule ref="DrupalPractice.CodeAnalysis.VariableAnalysis.UndefinedVariable">
<!-- Do not run this sniff on template files. -->
<exclude-pattern>*.tpl.php</exclude-pattern>
</rule>
<!-- Ignore various version control directories. -->
<exclude-pattern>*/\.git/*</exclude-pattern>
<exclude-pattern>*/\.svn/*</exclude-pattern>
<exclude-pattern>*/\.hg/*</exclude-pattern>
<exclude-pattern>*/\.bzr/*</exclude-pattern>
</ruleset>
@madjoe I'm not 100% sure, but this sounds like a configuration issue in your IDE which seems to register the Project class with PHPCS as if it were a sniff (which it isn't).
You may be better off asking in a support forum for Visual Code OSS or with the Drupal standard where they may have come across this before and know the solution already.
Other than that, it would be helpful to know which version of PHPCS is being used.
Another suggestion would be to try to not use the symlinks to the Drupal standards, but to add the DealerDirect Composer PHPCS plugin to your project and let that handle sorting out the installed_paths.
Great! I'll try with your suggestions. BTW, in my VS-OSS I'm running vscode-phpcs 1.0.5 (extension).
$ phpcs --version
PHP_CodeSniffer version 3.4.2 (stable) by Squiz (http://www.squiz.net)
Thanks for your swift reply.
I've located the class that causes it:
https://git.drupalcode.org/project/coder/blob/8.x-3.x/coder_sniffer/DrupalPractice/Project.php
And I redirected this issue over here:
https://www.drupal.org/project/coder/issues/3106119
add the DealerDirect Composer PHPCS plugin to your project and let that handle sorting out the
installed_paths
You were right, it was a configuration issue and the following line(s) helped me to resolve my original issue:
composer global require drupal/coder:^8.3.1
composer global require dealerdirect/phpcodesniffer-composer-installer
@madjoe Thanks for updating the issue with the solution. Hopefully it'll help someone else in the future too :+1:
Sorry, I am having the same issue while running phpcs in PHP 7.4 with --standard=Drupal.
I created a simple test.php file and if ran phpcs on it without the "Drupal" then it show the output but if i use the "Drupal" standard it never runs.
Here, I'd would to share that I recently upgraded my php from 7.2 to 7.4.
I am facing the same issue:
Fatal error: Uncaught Error: Call to undefined method DrupalPractice\Project::register() in /var/www/vendor/squizlabs/php_codesniffer/src/Ruleset.php on line 1231
Error: Call to undefined method DrupalPractice\Project::register() in /var/www/vendor/squizlabs/php_codesniffer/src/Ruleset.php on line 1231
Call Stack:
0.0003 441456 1. {main}() /var/www/vendor/squizlabs/php_codesniffer/bin/phpcs:0
0.0470 2294792 2. PHP_CodeSniffer\Runner->runPHPCS() /var/www/vendor/squizlabs/php_codesniffer/bin/phpcs:18
0.2593 2669352 3. PHP_CodeSniffer\Runner->init() /var/www/vendor/squizlabs/php_codesniffer/src/Runner.php:70
0.2735 2907368 4. PHP_CodeSniffer\Ruleset->__construct() /var/www/vendor/squizlabs/php_codesniffer/src/Runner.php:332
0.6165 6543784 5. PHP_CodeSniffer\Ruleset->populateTokenListeners() /var/www/vendor/squizlabs/php_codesniffer/src/Ruleset.php:218
Command was : phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme
Finally resolved by adding "squizlabs/php_codesniffer": "3.5.5" to my composer.json which instead should have been installed from https://github.com/Dealerdirect/phpcodesniffer-composer-installer/blob/master/composer.json#L28 (v3.5.8)