diff --git a/src/Ruleset.php b/src/Ruleset.php index 474517f4ae..38b1a91d50 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -1646,23 +1646,27 @@ public function setSniffProperty($sniffClass, $name, $settings) return; } - $value = $this->getRealPropertyValue($settings['value']); + $value = $settings['value']; // Handle properties set inline via phpcs:set. if (substr($name, -2) === '[]') { $values = []; if (is_string($value) === true) { - foreach (explode(',', $value) as $val) { - list($k, $v) = explode('=>', $val.'=>'); - if ($v !== '') { - $values[trim($k)] = $v; - } else { - $values[] = $k; + if (trim($value) !== '') { + foreach (explode(',', $value) as $val) { + list($k, $v) = explode('=>', $val.'=>'); + if ($v !== '') { + $values[trim($k)] = $v; + } else { + $values[] = $k; + } } } } $value = $this->getRealPropertyValue($values); + } else { + $value = $this->getRealPropertyValue($value); } if (isset($settings['extend']) === true diff --git a/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc b/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc index f2b4d1c6b1..beae30a52f 100644 --- a/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc +++ b/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc @@ -25,4 +25,8 @@ // phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false // phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsEmptyArray[] +// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithJustValueTrue[] true +// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithJustValueFalse[] false +// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithJustValueNull[] null + 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 825f8ac5fe..63d88a64e5 100644 --- a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php +++ b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php @@ -239,6 +239,27 @@ final class PropertyTypeHandlingSniff implements Sniff 'predefinedB' => ' null ', ]; + /** + * Used to verify that - in particular inline - array properties with only a "special" value get handled correctly. + * + * @var array + */ + public $expectsArrayWithJustValueTrue = []; + + /** + * Used to verify that - in particular inline - array properties with only a "special" value get handled correctly. + * + * @var array + */ + public $expectsArrayWithJustValueFalse = []; + + /** + * Used to verify that - in particular inline - array properties with only a "special" value get handled correctly. + * + * @var array + */ + public $expectsArrayWithJustValueNull = []; + /** * Used to verify that if `extend` is used on a non-array property, the value still gets set, but not as an array. * diff --git a/tests/Core/Ruleset/PropertyTypeHandlingTest.php b/tests/Core/Ruleset/PropertyTypeHandlingTest.php index b39536cab0..a472c159fc 100644 --- a/tests/Core/Ruleset/PropertyTypeHandlingTest.php +++ b/tests/Core/Ruleset/PropertyTypeHandlingTest.php @@ -174,6 +174,18 @@ public static function dataTypeHandling() 'propertyName' => 'expectsEmptyArray', 'expected' => [], ], + 'Array with just the value "true"' => [ + 'propertyName' => 'expectsArrayWithJustValueTrue', + 'expected' => [true], + ], + 'Array with just the value "false"' => [ + 'propertyName' => 'expectsArrayWithJustValueFalse', + 'expected' => [false], + ], + 'Array with just the value "null"' => [ + 'propertyName' => 'expectsArrayWithJustValueNull', + 'expected' => [null], + ], ]; }//end dataTypeHandling() diff --git a/tests/Core/Ruleset/PropertyTypeHandlingTest.xml b/tests/Core/Ruleset/PropertyTypeHandlingTest.xml index 0533fa3afb..6766b29c33 100644 --- a/tests/Core/Ruleset/PropertyTypeHandlingTest.xml +++ b/tests/Core/Ruleset/PropertyTypeHandlingTest.xml @@ -108,6 +108,18 @@ + + + + + + + + + + + +