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
developbranch of WPCS.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (7 by maintainers)
Links to this issue
Commits related to this issue
- Ruleset Tests: Work around WPCS trim() bug PHP 8.1 increased the strictness of types used with native functions, and this meant WPCS 2.3.0 had a bug: https://github.com/WordPress/WordPress-Coding-Sta... — committed to Automattic/VIP-Coding-Standards by GaryJones a year ago
- CS: Work around WPCS trim() bug PHP 8.1 increased the strictness of types used with native functions, and this meant WPCS 2.3.0 had a bug: https://github.com/WordPress/WordPress-Coding-Standards/issu... — committed to Automattic/VIP-Coding-Standards by GaryJones a year ago
- Fix checks for PHP 8.1 and above (#746) * Ruleset tests: Add label before test runs Make it clear which ruleset being tested, even on a test failure. * Ruleset Tests: Work around WPCS trim() bu... — committed to Automattic/VIP-Coding-Standards by GaryJones a year ago
To apply this flag in a
phpcs.xmlconfiguration file rather than on the command line, you’d want to add the following:(Note that the
&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=24575would execute PHPCS witherror_reporting = E_ALL & ~E_DEPRECATEDWorking 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=phpto 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 usedevelopfor the time being until the next release.