Magento 2: While running “vendor/bin/phpcbf” Uncaught Error: Class 'PHP_CodeSniffer\Runner' not found

magento.stackexchange https://magento.stackexchange.com/questions/331920

  •  15-04-2021
  •  | 
  •  

Question

I have faced issue in "vendor/bin/phpcbf" command for resolving custom extension coding standard.

I am using Magento ver.2.3.1

Running command in project root:

vendor/bin/phpcbf "app/code/Namespace/MyModule"

But while running command through below error:

PHP Fatal error: Uncaught Error: Class 'PHP_CodeSniffer\Runner' not found in /var/www/html/magento/vendor/bin/phpcbf:17 Stack trace:

0 {main} thrown in /var/www/html/magento/vendor/bin/phpcbf on line 17

Any help would be appreciated. Thanks.

Was it helpful?

Solution

Inside project

Install

composer require squizlabs/php_codesniffer:^3.4

// magento ruleset
composer require magento/magento-coding-standard

// auto ruleset installer – automatically pickup 'phpcodesniffer-standard' packages
composer require dealerdirect/phpcodesniffer-composer-installer

Usage

// should see Magento2 standard listed
vendor/bin/phpcs -i
vendor/bin/phpcbf -i

vendor/bin/phpcs --standard=Magento2 app/code/Xigen/Vehicle
vendor/bin/phpcbf --standard=Magento2 app/code/Xigen/Vehicle

vendor/bin/phpcs --standard=Magento2 app/code/Xigen/Data --report-file="Data.txt"
vendor/bin/phpcbf --standard=Magento2 app/code/Xigen/Data --report-file="Data.txt"

OTHER TIPS

Have the same issue, and fixed with this command:

composer global require squizlabs/php_codesniffer:^3.4

composer global require magento/magento-coding-standard

As result, my global phpcs -i working good do not need to call it from vendor/bin/phpcs

This error happens, if phpcs/phpcbf are copyed into the vendor/bin directory.

The reason is, the Package itself is not registering an autoloader, and the code inside the executables is

if (is_file(__DIR__.'/../autoload.php') === true) {
    include_once __DIR__.'/../autoload.php';
} else {
    include_once 'PHP/CodeSniffer/autoload.php';
}

The first is for it locally, including a custom autoload.php from inside. The second case I think is going to the include Path, which is only working if globally installed.

To solve you have multiple options.

1.

make sure its symlinked, not copied into vendor/bin/

2.

add the autoload.php file in your project composer.json like this:

    "files": [
        "vendor/squizlabs/php_codesniffer/autoload.php"
    ],

3.

execute the file directly as vendor/squizlabs/php_codesniffer/bin/phpcs

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top