WordPress / WordPress-Coding-Standards

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

PHP Fatal Error in `ControlStructureSpacingSniff` #2182

Closed marekdedic closed 1 year ago

marekdedic commented 1 year ago

Bug Description

Minimal Code Snippet

The issue happens when running phpcs with this config:

<?xml version="1.0"?>
<ruleset>
    <file>error.php</file>
    <config name="installed_paths" value="vendor/wp-coding-standards/wpcs"/>
    <rule ref="WordPress.WhiteSpace.ControlStructureSpacing" />
</ruleset>

... over a file containing this code:

<?php

function fn () {}

Note the space between fn and () - removing it fixes the error.

The resulting error:

> vendor/bin/phpcs

PHP Fatal error:  Uncaught TypeError: vsprintf(): Argument #2 ($values) must be of type array, string given in /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/File.php:1056
Stack trace:
#0 /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/File.php(1056): vsprintf()
#1 /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/File.php(672): PHP_CodeSniffer\Files\File->addMessage()
#2 /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/File.php(780): PHP_CodeSniffer\Files\File->addError()
#3 /home/user/project/vendor/wp-coding-standards/wpcs/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php(205): PHP_CodeSniffer\Files\File->addFixableError()
#4 /home/user/project/vendor/wp-coding-standards/wpcs/WordPress/Sniff.php(910): WordPressCS\WordPress\Sniffs\WhiteSpace\ControlStructureSpacingSniff->process_token()
#5 /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/File.php(498): WordPressCS\WordPress\Sniff->process()
#6 /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()
#7 /home/user/project/vendor/squizlabs/php_codesniffer/src/Runner.php(628): PHP_CodeSniffer\Files\LocalFile->process()
#8 /home/user/project/vendor/squizlabs/php_codesniffer/src/Runner.php(500): PHP_CodeSniffer\Runner->processFile()
#9 /home/user/project/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
#10 /home/user/project/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
#11 {main}
  thrown in /home/user/project/vendor/squizlabs/php_codesniffer/src/Files/File.php on line 1056

Environment

Question Answer
PHP version 8.1.12
PHP_CodeSniffer version 3.7.1
WPCS version 2.3.0
WPCS install type composer project local
IDE (if relevant) N/A

Tested Against develop branch?

jrfnl commented 1 year ago

Closing as duplicate of numerous previous issues. This has been fixed a long time ago in develop and we're working hard on getting WPCS 3.0.0 released.