Skip to content

Commit 23546f6

Browse files
committed
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.
1 parent c8c773e commit 23546f6

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

src/Ruleset.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,23 +1646,27 @@ public function setSniffProperty($sniffClass, $name, $settings)
16461646
return;
16471647
}
16481648

1649-
$value = $this->getRealPropertyValue($settings['value']);
1649+
$value = $settings['value'];
16501650

16511651
// Handle properties set inline via phpcs:set.
16521652
if (substr($name, -2) === '[]') {
16531653
$values = [];
16541654
if (is_string($value) === true) {
1655-
foreach (explode(',', $value) as $val) {
1656-
list($k, $v) = explode('=>', $val.'=>');
1657-
if ($v !== '') {
1658-
$values[trim($k)] = $v;
1659-
} else {
1660-
$values[] = $k;
1655+
if (trim($value) !== '') {
1656+
foreach (explode(',', $value) as $val) {
1657+
list($k, $v) = explode('=>', $val.'=>');
1658+
if ($v !== '') {
1659+
$values[trim($k)] = $v;
1660+
} else {
1661+
$values[] = $k;
1662+
}
16611663
}
16621664
}
16631665
}
16641666

16651667
$value = $this->getRealPropertyValue($values);
1668+
} else {
1669+
$value = $this->getRealPropertyValue($value);
16661670
}
16671671

16681672
if (isset($settings['extend']) === true

tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@
2525
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
2626
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsEmptyArray[]
2727

28+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithJustValueTrue[] true
29+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithJustValueFalse[] false
30+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithJustValueNull[] null
31+
2832
echo 'hello!';

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ final class PropertyTypeHandlingSniff implements Sniff
239239
'predefinedB' => ' null ',
240240
];
241241

242+
/**
243+
* Used to verify that - in particular inline - array properties with only a "special" value get handled correctly.
244+
*
245+
* @var array<mixed>
246+
*/
247+
public $expectsArrayWithJustValueTrue = [];
248+
249+
/**
250+
* Used to verify that - in particular inline - array properties with only a "special" value get handled correctly.
251+
*
252+
* @var array<mixed>
253+
*/
254+
public $expectsArrayWithJustValueFalse = [];
255+
256+
/**
257+
* Used to verify that - in particular inline - array properties with only a "special" value get handled correctly.
258+
*
259+
* @var array<mixed>
260+
*/
261+
public $expectsArrayWithJustValueNull = [];
262+
242263
/**
243264
* Used to verify that if `extend` is used on a non-array property, the value still gets set, but not as an array.
244265
*

tests/Core/Ruleset/PropertyTypeHandlingTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ public static function dataTypeHandling()
174174
'propertyName' => 'expectsEmptyArray',
175175
'expected' => [],
176176
],
177+
'Array with just the value "true"' => [
178+
'propertyName' => 'expectsArrayWithJustValueTrue',
179+
'expected' => [true],
180+
],
181+
'Array with just the value "false"' => [
182+
'propertyName' => 'expectsArrayWithJustValueFalse',
183+
'expected' => [false],
184+
],
185+
'Array with just the value "null"' => [
186+
'propertyName' => 'expectsArrayWithJustValueNull',
187+
'expected' => [null],
188+
],
177189
];
178190

179191
}//end dataTypeHandling()

tests/Core/Ruleset/PropertyTypeHandlingTest.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@
108108
<element value="true"/>
109109
</property>
110110

111+
<property name="expectsArrayWithJustValueTrue" type="array">
112+
<element value="true"/>
113+
</property>
114+
115+
<property name="expectsArrayWithJustValueFalse" type="array">
116+
<element value="false"/>
117+
</property>
118+
119+
<property name="expectsArrayWithJustValueNull" type="array">
120+
<element value="null"/>
121+
</property>
122+
111123
<property name="expectsStringNotArray" extend="true" value="some value"/>
112124

113125
</properties>

0 commit comments

Comments
 (0)