Skip to content

Commit

Permalink
PHP 8 | ControlStructureSpacing: bug fix - $data should be an array
Browse files Browse the repository at this point in the history
The `$data` parameter of the `$phpcsFile->add[Fixable]Error|Warning()` methods is expected to be an array, not a string.

Without this fix, PHP 8 will throw and (uncaught) `TypeError: vsprintf(): Argument #2 ($args) must be of type array, string given`.
  • Loading branch information
jrfnl committed Jul 21, 2020
1 parent 08ef3d6 commit 7cd46be
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function process_token( $stackPtr ) {
$error,
$stackPtr,
'SpaceBeforeFunctionOpenParenthesis',
$this->tokens[ ( $function_name_ptr + 1 ) ]['content']
array( $this->tokens[ ( $function_name_ptr + 1 ) ]['content'] )
);

if ( true === $fix ) {
Expand Down Expand Up @@ -282,7 +282,7 @@ public function process_token( $stackPtr ) {
$error,
$stackPtr,
'ExtraSpaceBeforeOpenParenthesis',
$this->tokens[ ( $stackPtr + 1 ) ]['content']
array( $this->tokens[ ( $stackPtr + 1 ) ]['content'] )
);

if ( true === $fix ) {
Expand Down Expand Up @@ -310,7 +310,7 @@ public function process_token( $stackPtr ) {
$error,
$stackPtr,
'ExtraSpaceAfterOpenParenthesis',
$this->tokens[ ( $parenthesisOpener + 1 ) ]['content']
array( $this->tokens[ ( $parenthesisOpener + 1 ) ]['content'] )
);

if ( true === $fix ) {
Expand Down Expand Up @@ -341,7 +341,7 @@ public function process_token( $stackPtr ) {
$error,
$stackPtr,
'ExtraSpaceBeforeCloseParenthesis',
$this->tokens[ ( $parenthesisCloser - 1 ) ]['content']
array( $this->tokens[ ( $parenthesisCloser - 1 ) ]['content'] )
);

if ( true === $fix ) {
Expand Down Expand Up @@ -399,7 +399,7 @@ public function process_token( $stackPtr ) {
$error,
$stackPtr,
'ExtraSpaceAfterCloseParenthesis',
$this->tokens[ ( $parenthesisCloser + 1 ) ]['content']
array( $this->tokens[ ( $parenthesisCloser + 1 ) ]['content'] )
);

if ( true === $fix ) {
Expand Down

11 comments on commit 7cd46be

@jdoubleu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this be released? Right now, it's not possible to run wpcs with PHP 8.

See also squizlabs/PHP_CodeSniffer#3196 (comment)

@sephentos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl

PHP 8 Support would be great. Hope to see something to getting wpcs work with PHP 8.

@odil-io
Copy link

@odil-io odil-io commented on 7cd46be Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting as the latest release does not include this fix: https://github.com/WordPress/WordPress-Coding-Standards/releases/tag/2.3.0
I was looking into why php-fpm was taking enormous amounts of CPU usage. A side effect was that my WP install loaded images very slowly (8s for a 400x400 image).
Turned out that Fatal error: Uncaught TypeError: vsprintf(): Argument #2 ($values) must be of type array, string given in /Users/path_to_theme/vendor/squizlabs/php_codesniffer/src/Files/File.php:1050 filled my memory.

My advise: use [email protected].

Edit: Issues of slow response and high CPU are still present. Take advice with a bucket of salt.

@cklosowski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manually added these to the specified file and it seems to fix the issue for me:
Windows 11
VSCode

@WebGuyJeff
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
Manually applied patch as above and can confirm fix.

@mqschwanda
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bigupjeff I can also confirm making the changes above fixed issues for me.

To avoid making manual changes in my individual projects, I use composer to download the development repository and build all vendor files. This is currently a "quick fix" as I just encountered this issue today. Here is an example.

I plan to make a fork from the WordPress-Coding-Standards stable release version and make the changes as a patch. After the fork is made I will update the gist accordingly. Hopefully, I can finish this all over the weekend.

@jrfnl
Copy link
Member Author

@jrfnl jrfnl commented on 7cd46be May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mqschwanda Just had a look at the gist, but I honestly don't understand what you are doing there.

The below should be sufficient. Everything else is unnecessary.

{
  "require-dev": {
    "wp-coding-standards/wpcs": "dev-develop"
  },
  "minimum-stability": "dev",
  "config": {
    "allow-plugins": {
      "dealerdirect/phpcodesniffer-composer-installer": true
    }
  }
}

@Mte90
Copy link

@Mte90 Mte90 commented on 7cd46be Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For who is looking for a fix there is a tag that should fix the issue also if there isn't a new official release https://github.com/WordPress/WordPress-Coding-Standards/releases/tag/2.3.0-fix-vsprintf

@jrfnl
Copy link
Member Author

@jrfnl jrfnl commented on 7cd46be Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mte90 Please be aware that the tag you mention is NOT recommended as it already contains breaking changes which will be part of the 3.0.0 release.

@Mte90
Copy link

@Mte90 Mte90 commented on 7cd46be Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but in the meantime is a solution to avoid crashes on phpcs and let to use WPCS :-)

@jrfnl
Copy link
Member Author

@jrfnl jrfnl commented on 7cd46be Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding crashes is simple. Run WPCS on PHP 7.4 until v 3.0 has been released.

Please sign in to comment.