Skip to content

Ruleset: bug fix - correctly handle empty array property setting #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -1174,12 +1174,14 @@ private function processRule($rule, $newSniffs, $depth=0)
} else {
$value = (string) $prop['value'];
$printValue = $value;
foreach (explode(',', $value) as $val) {
list($k, $v) = explode('=>', $val.'=>');
if ($v !== '') {
$values[trim($k)] = trim($v);
} else {
$values[] = trim($k);
if ($value !== '') {
foreach (explode(',', $value) as $val) {
list($k, $v) = explode('=>', $val.'=>');
if ($v !== '') {
$values[trim($k)] = trim($v);
} else {
$values[] = trim($k);
}
}
}
}//end if
Expand Down
2 changes: 2 additions & 0 deletions tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithExtendedValues[] string, 15, another string
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithExtendedKeysAndValues[] 10=>10,string=>string,15=>15,another string=>another string
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsEmptyArray[]

// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithExtendedValues[] string, 15, another string
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithExtendedKeysAndValues[] 10=>10,string=>string,15=>15,another string=>another string
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolEmptyArray[]

echo 'hello!';
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ final class PropertyTypeHandlingSniff implements Sniff
*/
public $expectsArrayWithExtendedKeysAndValues;

/**
* Used to verify that array properties allow for setting a property to an empty array.
*
* @var array<mixed>
*/
public $expectsEmptyArray;

/**
* Used to verify that array properties passed as a string get parsed to a proper array.
*
Expand Down Expand Up @@ -141,6 +148,13 @@ final class PropertyTypeHandlingSniff implements Sniff
*/
public $expectsOldSchoolArrayWithExtendedKeysAndValues;

/**
* Used to verify that array properties passed as a string allow for setting a property to an empty array.
*
* @var array<mixed>
*/
public $expectsOldSchoolEmptyArray;

public function register()
{
return [T_ECHO];
Expand Down
8 changes: 8 additions & 0 deletions tests/Core/Ruleset/PropertyTypeHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public static function dataTypeHandling()
'propertyName' => 'expectsArrayWithExtendedKeysAndValues',
'expected' => $expectedArrayKeysAndValuesExtended,
],
'Empty array (new style)' => [
'propertyName' => 'expectsEmptyArray',
'expected' => [],
],
'Array with only values (old style)' => [
'propertyName' => 'expectsOldSchoolArrayWithOnlyValues',
'expected' => $expectedArrayOnlyValues,
Expand All @@ -187,6 +191,10 @@ public static function dataTypeHandling()
'propertyName' => 'expectsOldSchoolArrayWithExtendedKeysAndValues',
'expected' => $expectedArrayKeysAndValuesExtended,
],
'Empty array (old style)' => [
'propertyName' => 'expectsOldSchoolEmptyArray',
'expected' => [],
],
];

}//end dataTypeHandling()
Expand Down
4 changes: 4 additions & 0 deletions tests/Core/Ruleset/PropertyTypeHandlingTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<element key="another string" value="another string"/>
</property>

<property name="expectsEmptyArray" type="array"/>

<property name="expectsOldSchoolArrayWithOnlyValues" type="array" value="string, 10, 1.5, null, true, false" />

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

<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" value="10=>10,string=>string" />
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" extend="true" value="15=>15,another string=>another string" />

<property name="expectsOldSchoolEmptyArray" type="array" value=""/>
</properties>
</rule>

Expand Down