Skip to content

Commit 0fc21dd

Browse files
committed
Ruleset::processRule(): fix edge case bug - inconsistent handling of empty string array key
Unlikely scenario for real-life, but still a bug. When an array property is set via the old comma-separated string ruleset format or via an inline `phpcs:set` annotation, and the key for an array element would be explicitly set to an empty string, the resulting array item in the property on the sniff class would be added without a key, resulting in a numeric key. However, if the (not so) "new" ruleset format using `<element>` nodes is used and an array element key is an empty string, this would result in the array item being added with an empty string for the array key. This is inconsistent behaviour and is now fixed. As 2 out of 3 scenarios would result in a numeric key for PHPCS 3.x, the behaviour has now been standardized as such. Note: for PHPCS 4.x, an empty string array key will remain an empty string and will not become a numeric key. This is in-line with the type handling proposal in 708, which explicitly states: > I do NOT intend to change the handling of array keys. These will remain strings at all times.
1 parent e38b254 commit 0fc21dd

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Ruleset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ private function processRule($rule, $newSniffs, $depth=0)
12151215
}
12161216

12171217
$value = (string) $element['value'];
1218-
if (isset($element['key']) === true) {
1218+
if (isset($element['key']) === true && trim($element['key']) !== '') {
12191219
$key = (string) $element['key'];
12201220
$values[$key] = $value;
12211221
$printValue .= $key.'=>'.$value.',';

0 commit comments

Comments
 (0)