Skip to content

PHP 8.0 vsprintf #1967

@str

Description

@str

Bug Description

When testing with PHP 8 and wpcs 2.3.0 I get the following error message:

PHP Fatal error:  Uncaught TypeError: vsprintf(): Argument #2 ($values) must be of type array, string given in /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/File.php:1051
Stack trace:
#0 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/File.php(1051): vsprintf()
#1 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/File.php(670): PHP_CodeSniffer\Files\File->addMessage()
#2 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/File.php(778): PHP_CodeSniffer\Files\File->addError()
#3 /home/str/.config/composer/vendor/wp-coding-standards/wpcs/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php(395): PHP_CodeSniffer\Files\File->addFixableError()
#4 /home/str/.config/composer/vendor/wp-coding-standards/wpcs/WordPress/Sniff.php(910): WordPressCS\WordPress\Sniffs\WhiteSpace\ControlStructureSpacingSniff->process_token()
#5 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/File.php(496): WordPressCS\WordPress\Sniff->process()
#6 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(91): PHP_CodeSniffer\Files\File->process()
#7 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Runner.php(630): PHP_CodeSniffer\Files\LocalFile->process()
#8 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Runner.php(434): PHP_CodeSniffer\Runner->processFile()
#9 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
#10 /home/str/.config/composer/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
#11 {main}
  thrown in /home/str/.config/composer/vendor/squizlabs/php_codesniffer/src/Files/File.php on line 1051

I debugged and the 2 params sent to vsprintf where

  • string(97) "Expected exactly one space between closing parenthesis and opening control structure; "%s" found."
  • string(1) "\n"

Minimal Code Snippet

This is the code that generates the error

        // See if there is a custom error message format to use.
        // But don't do this if we are replaying errors because replayed
        // errors have already used the custom format and have had their
        // data replaced.
        if ($this->replayingErrors === false
            && isset($this->ruleset->ruleset[$sniffCode]['message']) === true
        ) {
            $message = $this->ruleset->ruleset[$sniffCode]['message'];
        }
        if (empty($data) === false) {
            $message = vsprintf($message, $data);   // <-- here is what trigers the error
        }

For bugs with fixers: How was the code fixed? How did you expect the code to be fixed?

Error Code

Environment

Question Answer
PHP version 8.0
PHP_CodeSniffer version 3.5.8
WPCS version 2.3.0
WPCS install type Composer global
IDE (if relevant) vscode / cli

Additional Context (optional)

I saw some changes in wpcs around php 8.0, from July. wpcs 2.3.0 was released on May, before those changes. How can I test the latest wpcs changes after 2.3.0?

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of WPCS.
    I don't know how

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions