WordPress / WordPress-Coding-Standards

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

Unable to install #2068

Closed navigatrum closed 1 year ago

navigatrum commented 2 years ago

Bug Description

Installing fails with composer create-project wp-coding-standards/wpcs. And would seem to work, but throws Fatal Error with standalone installatation.

System: Ubuntu 20.04.4 LTS php: 7.4 composer: 2.3.7

First attempt: install locally

command:

composer create-project wp-coding-standards/wpcs --no-dev

Result:

Creating a "wp-coding-standards/wpcs" project at "./wpcs"
Info from https://repo.packagist.org: #StandWithUkraine
Installing wp-coding-standards/wpcs (2.3.0)
  - Downloading wp-coding-standards/wpcs (2.3.0)
  - Installing wp-coding-standards/wpcs (2.3.0): Extracting archive
Created project in /home/me/test/wpcs
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - dealerdirect/phpcodesniffer-composer-installer[v0.5.0, ..., v0.6.2] require composer-plugin-api ^1.0 -> found composer-plugin-api[2.3.0] but it does not match the constraint.
    - Root composer.json requires dealerdirect/phpcodesniffer-composer-installer ^0.5 || ^0.6 -> satisfiable by dealerdirect/phpcodesniffer-composer-installer[v0.5.0, v0.6.0, v0.6.1, v0.6.2].

Second attempt: install standalone

Do stuff to do. PHP_CodeSniffer version 2.2.0 (stable) Command:

 /home/me/.config/composer/vendor/bin/phpcs --report=json --encoding=UTF-8 --standard=/home/me/test/wpcs/WordPress/ruleset.xml --error-severity=5 --warning-severity=5

Result: some very nice:

PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1923
PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1928
PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1928
PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1974
PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1974

and cute:

PHP Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer/File.php on line 1763
PHP Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer/File.php on line 2723

and finally:

PHP Fatal error:  Uncaught PHP_CodeSniffer_Exception: Referenced sniff "Generic.Arrays.DisallowShortArraySyntax" does not exist in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php:1086
Stack trace:
#0 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php(733): PHP_CodeSniffer->_expandRulesetReference()
#1 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php(1099): PHP_CodeSniffer->processRuleset()
#2 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php(733): PHP_CodeSniffer->_expandRulesetReference()
#3 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php(1099): PHP_CodeSniffer->processRuleset()
#4 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php(733): PHP_CodeSniffer->_expandRulesetReference()
#5 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php(551): PHP_CodeSniffer->processRuleset()
#6 /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer/CLI.php in /home/me/.config/composer/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1086
dingo-d commented 2 years ago

Hi!

I recommend that you use WPCS using Composer per project.

Installing it globally could cause conflicts if you are using Composer in your projects, and dependencies clash.

In your project just do composer require wp-coding-standards/wpcs --dev, and add the composer plugin so that WPCS standards are visible to PHPCS: composer require --dev dealerdirect/phpcodesniffer-composer-installer.

Then you can just run vendor/bin/phpcs . to check your code.

andfinally commented 2 years ago

I get the same problem on a global install in macOS Monterey.

  Problem 1
    - Root composer.json requires dealerdirect/phpcodesniffer-composer-installer ^0.5 || ^0.6 -> satisfiable by dealerdirect/phpcodesniffer-composer-installer[v0.5.0, v0.6.0, v0.6.1, v0.6.2].
    - dealerdirect/phpcodesniffer-composer-installer[v0.5.0, ..., v0.6.2] require composer-plugin-api ^1.0 -> found composer-plugin-api[2.2.0] but it does not match the constraint.
  Problem 2
    - phpcsstandards/phpcsdevtools[1.0.1, ..., 1.1.1] require dealerdirect/phpcodesniffer-composer-installer ^0.4.1 || ^0.5 || ^0.6.2 || ^0.7 -> satisfiable by dealerdirect/phpcodesniffer-composer-installer[v0.5.0, v0.6.2].
    - phpcsstandards/phpcsdevtools 1.0.0 requires dealerdirect/phpcodesniffer-composer-installer ^0.3 || ^0.4.1 || ^0.5 || ^0.6.2 -> satisfiable by dealerdirect/phpcodesniffer-composer-installer[v0.5.0, v0.6.2].
    - dealerdirect/phpcodesniffer-composer-installer[v0.5.0, ..., v0.6.2] require composer-plugin-api ^1.0 -> found composer-plugin-api[2.2.0] but it does not match the constraint.
    - Root composer.json requires phpcsstandards/phpcsdevtools ^1.0 -> satisfiable by phpcsstandards/phpcsdevtools[1.0.0, 1.0.1, 1.1.0, 1.1.1].

Based on this error, it looks like the installer is incompatible with Composer 2. I tried upgrading to the latest Composer with composer self-update --2, but still got the error.

I was able to downgrade Composer to 1 using composer self-update --1 as suggested here. Then the installation completes, although I get the warning

Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/

If you're installing globally, one way around this is to downgrade Composer, do the install, and then restore Composer to the latest version.

jrfnl commented 2 years ago

@andfinally See @dingo-d's answer just above your post. You can use the same instructions with global in them to install globally.

The installer is perfectly compatible with Composer 2.x, but not when using the old 0.6.x version which was released before Composer 2.x existed..

jrfnl commented 1 year ago

Closing as duplicate of #1901.