diff --git a/src/Ruleset.php b/src/Ruleset.php index f71620c477..b5b089115e 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -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 diff --git a/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc b/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc index d904b847d3..4c06f2c497 100644 --- a/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc +++ b/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc @@ -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!'; diff --git a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php index a6fc1200e6..8ee815a579 100644 --- a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php +++ b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php @@ -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 + */ + public $expectsEmptyArray; + /** * Used to verify that array properties passed as a string get parsed to a proper array. * @@ -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 + */ + public $expectsOldSchoolEmptyArray; + public function register() { return [T_ECHO]; diff --git a/tests/Core/Ruleset/PropertyTypeHandlingTest.php b/tests/Core/Ruleset/PropertyTypeHandlingTest.php index 3ffa838ca2..ecb82ffd98 100644 --- a/tests/Core/Ruleset/PropertyTypeHandlingTest.php +++ b/tests/Core/Ruleset/PropertyTypeHandlingTest.php @@ -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, @@ -187,6 +191,10 @@ public static function dataTypeHandling() 'propertyName' => 'expectsOldSchoolArrayWithExtendedKeysAndValues', 'expected' => $expectedArrayKeysAndValuesExtended, ], + 'Empty array (old style)' => [ + 'propertyName' => 'expectsOldSchoolEmptyArray', + 'expected' => [], + ], ]; }//end dataTypeHandling() diff --git a/tests/Core/Ruleset/PropertyTypeHandlingTest.xml b/tests/Core/Ruleset/PropertyTypeHandlingTest.xml index a28c0ef6d2..612928e0c1 100644 --- a/tests/Core/Ruleset/PropertyTypeHandlingTest.xml +++ b/tests/Core/Ruleset/PropertyTypeHandlingTest.xml @@ -54,6 +54,8 @@ + + @@ -63,6 +65,8 @@ + +