Skip to content

Commit 0e49a1f

Browse files
committed
Ruleset: bug fix - correctly handle empty array property setting
While already handled correctly for inline properties set via `phpcs:set`, setting a property to an empty array from a ruleset file was not handled correctly until now. As things were, when setting an array property from the ruleset to an empty array like so: ```xml <rule ref="Standard.Category.SniffName"> <properties> <property name="arrayProperty" type="array"/> </properties> </rule> ``` the property value would be set to: ```php public $arrayProperty = [ 0 => '', ]; ``` ... while the expected behaviour would be: ```php public $arrayProperty = []; ``` While it is not expected that this type of property setting is encountered a lot in the wild, it should still work correctly. Fixed now. Includes test safeguarding the fix.
1 parent 8894b66 commit 0e49a1f

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/Ruleset.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,12 +1174,14 @@ private function processRule($rule, $newSniffs, $depth=0)
11741174
} else {
11751175
$value = (string) $prop['value'];
11761176
$printValue = $value;
1177-
foreach (explode(',', $value) as $val) {
1178-
list($k, $v) = explode('=>', $val.'=>');
1179-
if ($v !== '') {
1180-
$values[trim($k)] = trim($v);
1181-
} else {
1182-
$values[] = trim($k);
1177+
if ($value !== '') {
1178+
foreach (explode(',', $value) as $val) {
1179+
list($k, $v) = explode('=>', $val.'=>');
1180+
if ($v !== '') {
1181+
$values[trim($k)] = trim($v);
1182+
} else {
1183+
$values[] = trim($k);
1184+
}
11831185
}
11841186
}
11851187
}//end if

tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
2222
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithExtendedValues[] string, 15, another string
2323
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithExtendedKeysAndValues[] 10=>10,string=>string,15=>15,another string=>another string
24+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsEmptyArray[]
2425

2526
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false
2627
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
2728
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithExtendedValues[] string, 15, another string
2829
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithExtendedKeysAndValues[] 10=>10,string=>string,15=>15,another string=>another string
30+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolEmptyArray[]
2931

3032
echo 'hello!';

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ final class PropertyTypeHandlingSniff implements Sniff
113113
*/
114114
public $expectsArrayWithExtendedKeysAndValues;
115115

116+
/**
117+
* Used to verify that array properties allow for setting a property to an empty array.
118+
*
119+
* @var array<mixed>
120+
*/
121+
public $expectsEmptyArray;
122+
116123
/**
117124
* Used to verify that array properties passed as a string get parsed to a proper array.
118125
*
@@ -141,6 +148,13 @@ final class PropertyTypeHandlingSniff implements Sniff
141148
*/
142149
public $expectsOldSchoolArrayWithExtendedKeysAndValues;
143150

151+
/**
152+
* Used to verify that array properties passed as a string allow for setting a property to an empty array.
153+
*
154+
* @var array<mixed>
155+
*/
156+
public $expectsOldSchoolEmptyArray;
157+
144158
public function register()
145159
{
146160
return [T_ECHO];

tests/Core/Ruleset/PropertyTypeHandlingTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ public static function dataTypeHandling()
171171
'propertyName' => 'expectsArrayWithExtendedKeysAndValues',
172172
'expected' => $expectedArrayKeysAndValuesExtended,
173173
],
174+
'Empty array (new style)' => [
175+
'propertyName' => 'expectsEmptyArray',
176+
'expected' => [],
177+
],
174178
'Array with only values (old style)' => [
175179
'propertyName' => 'expectsOldSchoolArrayWithOnlyValues',
176180
'expected' => $expectedArrayOnlyValues,
@@ -187,6 +191,10 @@ public static function dataTypeHandling()
187191
'propertyName' => 'expectsOldSchoolArrayWithExtendedKeysAndValues',
188192
'expected' => $expectedArrayKeysAndValuesExtended,
189193
],
194+
'Empty array (old style)' => [
195+
'propertyName' => 'expectsOldSchoolEmptyArray',
196+
'expected' => [],
197+
],
190198
];
191199

192200
}//end dataTypeHandling()

tests/Core/Ruleset/PropertyTypeHandlingTest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
<element key="another string" value="another string"/>
5555
</property>
5656

57+
<property name="expectsEmptyArray" type="array"/>
58+
5759
<property name="expectsOldSchoolArrayWithOnlyValues" type="array" value="string, 10, 1.5, null, true, false" />
5860

5961
<property name="expectsOldSchoolArrayWithKeysAndValues" type="array" value="string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false" />
@@ -63,6 +65,8 @@
6365

6466
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" value="10=>10,string=>string" />
6567
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" extend="true" value="15=>15,another string=>another string" />
68+
69+
<property name="expectsOldSchoolEmptyArray" type="array" value=""/>
6670
</properties>
6771
</rule>
6872

0 commit comments

Comments
 (0)