Skip to content

Commit e9063d6

Browse files
committed
PHPCS 4.0: handle converting empty string array values to null
PHPCS 4.0 introduced a change where empty string values in array properties are converted to `null`. This is documented in the Version 4.0 Developer Upgrade Guide (https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Version-4.0-Developer-Upgrade-Guide#property-type-casting-has-been-made-more-consistent): > Array elements can now no longer contain an empty string value > as that value will be cast to `null`. This affected the sniffs PrefixAllGlobals and NoSilencedErrors. Both pass array values to `strtolower()`, which triggers a PHP 8.5 deprecation warning when `null` is passed. The fix is different for each sniff: - PrefixAllGlobals: Convert `null` back to empty string at the start of the validation loop. This preserves the "prefix too short" error that alerts users about invalid configuration. - NoSilencedErrors: Filter out `null` values before processing. Unlike PrefixAllGlobals, this sniff has no validation that provides user feedback for invalid entries, so filtering is cleaner. A test case was added to safeguard against the deprecation.
1 parent 21123c1 commit e9063d6

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ private function validate_prefixes() {
12291229
$prefixes = array();
12301230
$ns_prefixes = array();
12311231
foreach ( $this->prefixes as $key => $prefix ) {
1232+
// PHPCS >= 4.0 converts empty string values in array properties to null.
1233+
// Convert null back to empty string to preserve the "prefix too short" error.
1234+
$prefix = $prefix ?? '';
12321235
$prefixLC = strtolower( $prefix );
12331236

12341237
if ( isset( $this->prefix_blocklist[ $prefixLC ] ) ) {

WordPress/Sniffs/PHP/NoSilencedErrorsSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public function register() {
186186
public function process_token( $stackPtr ) {
187187
// Handle the user-defined custom function list.
188188
$this->customAllowedFunctionsList = RulesetPropertyHelper::merge_custom_array( $this->customAllowedFunctionsList, array(), false );
189+
// PHPCS >= 4.0 converts empty string values in array properties to null.
190+
// Filter out null values to avoid passing them to strtolower().
191+
$this->customAllowedFunctionsList = array_filter( $this->customAllowedFunctionsList );
189192
$this->customAllowedFunctionsList = array_map( 'strtolower', $this->customAllowedFunctionsList );
190193

191194
/*

WordPress/Tests/PHP/NoSilencedErrorsUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,11 @@ $file = @MyNS\MyClass::file_get_contents( $file ); // Bad.
9292
$file = @MyNS\MyClass\file_exists( $file ); // Bad.
9393
$file = @namespace\MyNS\MyClass::file( $file ); // Bad.
9494
$file = @namespace\is_dir( $dir ); // The sniff should stop flagging this once it can resolve relative namespaces.
95+
96+
/*
97+
* Safeguard handling of empty prefix values (PHPCS 4.x compatibility).
98+
* PHPCS >= 4.0 converts empty string values in array properties to null.
99+
*/
100+
// phpcs:set WordPress.PHP.NoSilencedErrors customAllowedFunctionsList[] ,
101+
$unserialized = @unserialize( $str );
102+
// phpcs:set WordPress.PHP.NoSilencedErrors customAllowedFunctionsList[]

0 commit comments

Comments
 (0)