From 23546f63717ffdd60052df10fb7f854e26a40469 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 8 Aug 2025 15:34:14 +0200 Subject: [PATCH] 4.0 | Ruleset: minor tweak to property type handling for arrays When array properties are set inline to one of the "special" values, i.e. `true`, `false` or `null`, this would not be handled correctly and always result in an empty array. While it is highly debatable that this is something which will ever occur in real-life, as setting an array property like that would not give a sniff any usable information - basically the property should not have been an array property in that case -, it should still be handled correctly by the ruleset class. Fixed now. Includes additional tests. --- src/Ruleset.php | 18 +++++++++------- .../Fixtures/PropertyTypeHandlingInline.inc | 4 ++++ .../SetProperty/PropertyTypeHandlingSniff.php | 21 +++++++++++++++++++ .../Core/Ruleset/PropertyTypeHandlingTest.php | 12 +++++++++++ .../Core/Ruleset/PropertyTypeHandlingTest.xml | 12 +++++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) 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 @@ + + + + + + + + + + + +