10up / phpcs-composer

Official 10up PHPCS rules.
MIT License
48 stars 8 forks source link

Support for PHP 8 with WPCS #37

Closed akshitsethi closed 9 months ago

akshitsethi commented 1 year ago

Describe the bug

There is an error when running phpcs with the current release of wpcs which is v2.3.0. The error which occurs for PHP 8.0 only is as under:

PHP Fatal error:  Uncaught TypeError: vsprintf(): Argument #2 ($values) must be of type array, string given in /Users/akshitsethi/wp-local-docker-sites/snowsoftware-test/wordpress/wp-content/vendor/squizlabs/php_codesniffer/src/Files/File.php:1056

Related issues:

Steps to Reproduce

  1. Upgrade PHP to v8.0
  2. In the terminal, try running composer lint for any project using phpcs-composer package
  3. Notice the error

Screenshots, screen recording, code snippet

Screenshot 2023-01-03 at 8 32 41 PM

Environment information

WordPress information

No response

Code of Conduct

akshitsethi commented 1 year ago

No release has been made yet but a tagged version in wpcs repo has this issue resolved.

https://github.com/WordPress/WordPress-Coding-Standards/releases/tag/2.3.0-fix-vsprintf

claytoncollie commented 1 year ago

@akshitsethi I am seeing the same thing. Do you have a temporary workaround in your project to get past this?

Long term solution might be to version control the wpcs package in the composer.json file but that would require a release by this repository. I'm hoping there is something more temporary we can do right now.

akshitsethi commented 1 year ago

@claytoncollie Unfortunately, I'm stuck as well atm. If you want something to be fixed locally for you, then I suggest you make the changes as done in this PR - https://github.com/WordPress/WordPress-Coding-Standards/pull/1935/files

johnwatkins0 commented 1 year ago

@akshitsethi @claytoncollie One solution would be to exclude the broken sniff from the 10up ruleset and add the updated version from that PR as a custom sniff in the 10up ruleset.

claytoncollie commented 1 year ago

I tried to create a branch in this repository with the tag created by Gary Jones. But since it is on the develop branch and not published to packagist, I am having a hard time sourcing it with the command composer require wp-coding-standards/wpcs:dev-develop#2.3.0-fix-vsprintf -W and that is after adding a repo reference to the composer.json file seen below.

"repositories": [
  {
    "type": "git",
    "url": "https://github.com/WordPress/WordPress-Coding-Standards"
  }
],

Running the require command produces an error related to the automatic vipcs package.

Problem 1
    - automattic/vipwpcs is locked to version 2.3.3 and an update of this package was not requested.
    - automattic/vipwpcs 2.3.3 requires wp-coding-standards/wpcs ^2.3 -> found wp-coding-standards/wpcs[2.3.0] but it conflicts with your root composer.json require (dev-develop#2.3.0-fix-vsprintf).

I think the easiest fix is what @johnwatkins0 suggested.

37Rb commented 1 year ago

@claytoncollie your idea worked great for me after a long struggle trying other solutions. Thanks! Here's my composer.json:

{
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/WordPress/WordPress-Coding-Standards"
        }
    ],
    "require-dev": {
        "dealerdirect/phpcodesniffer-composer-installer": "^1.0",
        "wp-coding-standards/wpcs": "dev-develop#2.3.0-fix-vsprintf"
    },
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true
        }
    }
}
joemcgill commented 1 year ago

The official roadmap for WPCS v3.0, which adds support for PHP 8.0 can be read here: https://github.com/WordPress/WordPress-Coding-Standards/issues/1877

PypWalters commented 1 year ago

The workaround I used for this was to upgrade to PHP 8.1 (8.0 is approaching EOL in a couple months anyway) and then add the following to your phpcs.xml file: <ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />. If you don't add that to your phpcs.xml you will see an error like this "An error occurred during processing; checking has been aborted. The error message was: trim(): Passing null to parameter #1 ($string) of type string is deprecated..."

claytoncollie commented 1 year ago

WPCS 3.0.0 was released on 8/21. You can either wait for this repository to be updated or you can use the composer.json example above to require the latest tag.

https://github.com/WordPress/WordPress-Coding-Standards/releases/tag/3.0.0

darylldoyle commented 1 year ago

I've just tagged a 3.0.0-RC1 release that updates the ruleset to use PHPCS 3.0.0.

You can try it out by using:

composer require 10up/phpcs-composer:~3@RC

If you have any issues, please log them in #49 or open an issue and reference that PR.