Skip to content

Commit 170bb73

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 2f7d1f2 commit 170bb73

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ public static function dataTypeHandling()
171171
'propertyName' => 'expectsArrayWithExtendedKeysAndValues',
172172
'expected' => $expectedArrayKeysAndValuesExtended,
173173
],
174-
174+
'Empty array' => [
175+
'propertyName' => 'expectsEmptyArray',
176+
'expected' => [],
177+
],
175178
];
176179

177180
}//end dataTypeHandling()

tests/Core/Ruleset/PropertyTypeHandlingTest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
<element key="15" value="15"/>
5454
<element key="another string" value="another string"/>
5555
</property>
56+
57+
<property name="expectsEmptyArray" type="array"/>
5658
</properties>
5759
</rule>
5860

0 commit comments

Comments
 (0)