WordPress-Coding-Standards: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string)

Bug Description

When i want run the phpcbf i get the following error: PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /somePath/vendor/wp-coding-standards/wpcs/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php on line 280

Minimal Code Snippet

The issue happens when running this command:

vendor/bin/phpcbf --standard=WordPress someFilePath.php

… over a file containing this code:

// All PHP Code Files

Error Code

PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /somePath/vendor/wp-coding-standards/wpcs/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php on line 280

Environment

Question Answer
PHP version PHP 8.1.4 (cli)
PHP_CodeSniffer version PHP_CodeSniffer version 3.6.2
WPCS version 2.3.0
WPCS install type Composer project local

Additional Context (optional)

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of WPCS.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (7 by maintainers)

Commits related to this issue

Most upvoted comments

As the values of the E_* constants can vary depending on the PHP version used, better to use phpcs -d error_reporting="E_ALL&~E_DEPRECATED"

To apply this flag in a phpcs.xml configuration file rather than on the command line, you’d want to add the following:

<!--
Prevent errors caused by WordPress Coding Standards not supporting PHP 8.0+.
See https://github.com/WordPress/WordPress-Coding-Standards/issues/2035
-->
<ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />

(Note that the &#38; is an escaped & (ampersand); the & character must be escaped in XML documents.)

Leaving this for others that have went searching for this error.

Looking at the error, this is due to a deprecation message getting bubbled up. While it is a workaround, if the error reporting level is set to ignore deprecated messages, it will no longer display throw this error. For instance phpcs -d error_reporting=24575 would execute PHPCS with error_reporting = E_ALL & ~E_DEPRECATED

As the values of the E_* constants can vary depending on the PHP version used, better to use phpcs -d error_reporting="E_ALL&~E_DEPRECATED"

To apply this flag in a phpcs.xml configuration file rather than on the command line, you’d want to add the following:

<!--
Prevent errors caused by WordPress Coding Standards not supporting PHP 8.0+.
See https://github.com/WordPress/WordPress-Coding-Standards/issues/2035
-->
<ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />

(Note that the &#38; is an escaped & (ampersand); the & character must be escaped in XML documents.)

Working solution on m2 pro and PHP 8.1

The year is 2023, I have installed the WordPress Coding Standards on a fresh install… and I am still getting this error. I find this issue every time I wipe my Macbook…

edit: mind you, I end up solving this by installing PHP 7.4 on my machine. probably not the correct answer, but at least it’s working.

Still getting the same error on my MacBook with php 8.2.

getting the same error, unable to install php < 8 because the older versions are no longer supported

@smileBeda The platform check is triggered by one of your other dependencies, not by WPCS, Nothing to do with the latest Composer either, Composer has contained a platform check for a long time.

You could run Composer with --ignore-platform-reqs=php to get round it, but again: you’d be getting round a requirement of one of your other dependencies. This is unrelated to WPCS.

This has already been fixed long ago in develop. The last WPCS release does not support PHP 8.0 or 8.1. Either run it on PHP < 8.0 or use develop for the time being until the next release.