How to set up phpcs with WordPress coding standard with PHP8?

Trying to set up PHP_CodeSniffer with the WordPress Codings Standards. I'm running PHP v8.0.3 and phpcs v3.5.8. (in March 2021).

WPCS is installed via

git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs

The installation worked, but running phpcs leads to this error:

phpcs: Uncaught TypeError: vsprintf(): Argument #2 ($values) must be of type array, 
string given in file.php

This is apparently an issue with WPCS and fixed in this commit. The commit seems to be not merged into master yet, so I checked out the develop branch. This appears to fix the above-mentioned issue but causes another error:

phpcs: Referenced sniff PHPCSUtils does not exist    

The proposed solution for fixing this issue is switching to the master branch, which brings me back to square one.

So both master and develop don't work for different reasons. I guess could change the source code in the master branch manually, but that doesn't seem to be a sustainable fix.

Is there a better way to make phpcs work with the current versions of PHP and WPCS?

Topic coding-standards php Wordpress

Category Web


Apparently PHPCS (link to issue) and WPCS (link to issue) do not work at this point with PHP8 and won't until their next releases.

It's therefore recommended to use PHP 7.4 instead.

"Hack" option

Before downgrading my system to PHP7, I tried this hack. It seems to work for me, at least for the brief testing I did. But it might cause any kinds of issues, so use it at you own risk.

Replacing the content of the local ControlStructureSpacingSniff.php file
(in /WordPress/Sniffs/WhiteSpace/ of the cloned repo or the vendor folder of the composer installation) with the one from the develop branch.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.