Skip to content

Commit 00df6ec

Browse files
authored
Merge pull request #976 from PHPCSStandards/phpcs-4.0/feature/sq-1983-ruleset-remove-support-old-array-syntax
Ruleset: remove support for the deprecated ruleset.xml array property string-based syntax
2 parents 2f3d62e + a4911f5 commit 00df6ec

10 files changed

+155
-132
lines changed

src/Ruleset.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,17 @@ private function processRule($rule, $newSniffs, $depth=0)
11991199
if (isset($prop['type']) === true
12001200
&& (string) $prop['type'] === 'array'
12011201
) {
1202+
if (isset($prop['value']) === true) {
1203+
$message = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL;
1204+
$message .= 'is no longer supported since PHP_CodeSniffer 4.0.0.'.PHP_EOL;
1205+
$message .= "The unsupported syntax was used for property \"$name\"".PHP_EOL;
1206+
$message .= "for sniff \"$code\".".PHP_EOL;
1207+
$message .= 'Pass array values via <element [key="..." ]value="..."> nodes instead.';
1208+
$this->msgCache->add($message, MessageCollector::ERROR);
1209+
1210+
continue;
1211+
}
1212+
12021213
$values = [];
12031214
if (isset($prop['extend']) === true
12041215
&& (string) $prop['extend'] === 'true'
@@ -1226,27 +1237,7 @@ private function processRule($rule, $newSniffs, $depth=0)
12261237
}
12271238

12281239
$printValue = rtrim($printValue, ',');
1229-
} else if (isset($prop['value']) === true) {
1230-
$message = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL;
1231-
$message .= 'was deprecated in PHP_CodeSniffer 3.3.0. Support will be removed in PHPCS 4.0.0.'.PHP_EOL;
1232-
$message .= "The deprecated syntax was used for property \"$name\"".PHP_EOL;
1233-
$message .= "for sniff \"$code\".".PHP_EOL;
1234-
$message .= 'Pass array values via <element [key="..." ]value="..."> nodes instead.';
1235-
$this->msgCache->add($message, MessageCollector::DEPRECATED);
1236-
1237-
$value = (string) $prop['value'];
1238-
$printValue = $value;
1239-
if ($value !== '') {
1240-
foreach (explode(',', $value) as $val) {
1241-
list($k, $v) = explode('=>', $val.'=>');
1242-
if ($v !== '') {
1243-
$values[trim($k)] = trim($v);
1244-
} else {
1245-
$values[] = trim($k);
1246-
}
1247-
}
1248-
}
1249-
}//end if
1240+
}
12501241

12511242
$this->ruleset[$code]['properties'][$name] = [
12521243
'value' => $values,

tests/Core/Ruleset/ExplainTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public function testExplainWithDeprecatedSniffs()
184184
$ruleset = new Ruleset($config);
185185

186186
$expected = PHP_EOL;
187-
$expected .= 'The ShowSniffDeprecationsTest standard contains 11 sniffs'.PHP_EOL.PHP_EOL;
187+
$expected .= 'The ShowSniffDeprecationsTest standard contains 12 sniffs'.PHP_EOL.PHP_EOL;
188188

189-
$expected .= 'TestStandard (11 sniffs)'.PHP_EOL;
189+
$expected .= 'TestStandard (12 sniffs)'.PHP_EOL;
190190
$expected .= '------------------------'.PHP_EOL;
191191
$expected .= ' TestStandard.Deprecated.WithLongReplacement *'.PHP_EOL;
192192
$expected .= ' TestStandard.Deprecated.WithoutReplacement *'.PHP_EOL;
@@ -198,6 +198,7 @@ public function testExplainWithDeprecatedSniffs()
198198
$expected .= ' TestStandard.SetProperty.AllowedViaStdClass'.PHP_EOL;
199199
$expected .= ' TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL;
200200
$expected .= ' TestStandard.SetProperty.PropertyTypeHandling'.PHP_EOL;
201+
$expected .= ' TestStandard.SetProperty.PropertyTypeHandlingOldArrayFormat'.PHP_EOL;
201202
$expected .= ' TestStandard.ValidSniffs.RegisterEmptyArray'.PHP_EOL.PHP_EOL;
202203

203204
$expected .= '* Sniffs marked with an asterix are deprecated.'.PHP_EOL;

tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc

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

27-
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false
28-
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
29-
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolEmptyArray[]
30-
3127
echo 'hello!';
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PropertyTypeHandlingOldArrayFormatTest
6+
*/
7+
8+
namespace Fixtures\TestStandard\Sniffs\SetProperty;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
final class PropertyTypeHandlingOldArrayFormatSniff implements Sniff
14+
{
15+
16+
/**
17+
* Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0.
18+
*
19+
* @var array<mixed>
20+
*/
21+
public $expectsOldSchoolArrayWithOnlyValues;
22+
23+
/**
24+
* Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0.
25+
*
26+
* @var array<string, mixed>
27+
*/
28+
public $expectsOldSchoolArrayWithKeysAndValues;
29+
30+
/**
31+
* Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0.
32+
*
33+
* @var array<string, mixed>
34+
*/
35+
public $expectsOldSchoolArrayWithExtendedValues;
36+
37+
/**
38+
* Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0.
39+
*
40+
* @var array<string, mixed>
41+
*/
42+
public $expectsOldSchoolArrayWithExtendedKeysAndValues;
43+
44+
/**
45+
* Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0.
46+
*
47+
* @var array<mixed>
48+
*/
49+
public $expectsOldSchoolEmptyArray;
50+
51+
public function register()
52+
{
53+
return [T_ECHO];
54+
}
55+
56+
public function process(File $phpcsFile, $stackPtr)
57+
{
58+
// Do something.
59+
}
60+
}

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,41 +143,6 @@ final class PropertyTypeHandlingSniff implements Sniff
143143
*/
144144
public $expectsEmptyArray;
145145

146-
/**
147-
* Used to verify that array properties passed as a string get parsed to a proper array.
148-
*
149-
* @var array<mixed>
150-
*/
151-
public $expectsOldSchoolArrayWithOnlyValues;
152-
153-
/**
154-
* Used to verify that array properties passed as a string with keys get parsed to a proper array.
155-
*
156-
* @var array<string, mixed>
157-
*/
158-
public $expectsOldSchoolArrayWithKeysAndValues;
159-
160-
/**
161-
* Used to verify that array properties passed as a string can get extended.
162-
*
163-
* @var array<string, mixed>
164-
*/
165-
public $expectsOldSchoolArrayWithExtendedValues;
166-
167-
/**
168-
* Used to verify that array properties passed as a string can get extended.
169-
*
170-
* @var array<string, mixed>
171-
*/
172-
public $expectsOldSchoolArrayWithExtendedKeysAndValues;
173-
174-
/**
175-
* Used to verify that array properties passed as a string allow for setting a property to an empty array.
176-
*
177-
* @var array<mixed>
178-
*/
179-
public $expectsOldSchoolEmptyArray;
180-
181146
public function register()
182147
{
183148
return [T_ECHO];

tests/Core/Ruleset/ProcessRulesetTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function testAutoExpandSniffsDirectory()
7474
"$std.SetProperty.AllowedViaStdClass" => "$sniffDir\SetProperty\AllowedViaStdClassSniff",
7575
"$std.SetProperty.NotAllowedViaAttribute" => "$sniffDir\SetProperty\NotAllowedViaAttributeSniff",
7676
"$std.SetProperty.PropertyTypeHandling" => "$sniffDir\SetProperty\PropertyTypeHandlingSniff",
77+
"$std.SetProperty.PropertyTypeHandlingOldArrayFormat" => "$sniffDir\SetProperty\PropertyTypeHandlingOldArrayFormatSniff",
7778
"$std.ValidSniffs.RegisterEmptyArray" => "$sniffDir\ValidSniffs\RegisterEmptyArraySniff",
7879
];
7980

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Tests for the handling of properties being set via the ruleset.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
15+
16+
/**
17+
* Test the handling of an array property value using the PHPCS < 4.0 format set via the ruleset.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::setSniffProperty
20+
*/
21+
final class PropertyTypeHandlingOldArrayFormatTest extends AbstractRulesetTestCase
22+
{
23+
24+
25+
/**
26+
* Verify an error is thrown when an array property is set from the ruleset using a comma-separated string.
27+
*
28+
* Support for this format was (soft) deprecated in PHPCS 3.3.0 and removed in PHPCS 4.0.0.
29+
*
30+
* @return void
31+
*/
32+
public function testUsingOldSchoolArrayFormatThrowsError()
33+
{
34+
$regex = '`^(';
35+
$regex .= 'ERROR: Passing an array of values to a property using a comma-separated string\R';
36+
$regex .= 'is no longer supported since PHP_CodeSniffer 4\.0\.0\.\R';
37+
$regex .= 'The unsupported syntax was used for property "expectsOldSchool(?:EmptyArray|ArrayWith(?:Extended|Only)?(?:KeysAnd)?Values)"\R';
38+
$regex .= 'for sniff "';
39+
$regex .= '(?:\./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingOldArrayFormatSniff\.php|TestStandard\.SetProperty\.PropertyTypeHandlingOldArrayFormat)';
40+
$regex .= '"\.\R';
41+
$regex .= 'Pass array values via <element \[key="\.\.\." \]value="\.\.\."> nodes instead\.\R';
42+
$regex .= '){14}\R$`';
43+
44+
$this->expectRuntimeExceptionRegex($regex);
45+
46+
// Set up the ruleset.
47+
$standard = __DIR__.'/PropertyTypeHandlingOldArrayFormatTest.xml';
48+
$config = new ConfigDouble(["--standard=$standard"]);
49+
new Ruleset($config);
50+
51+
}//end testUsingOldSchoolArrayFormatThrowsError()
52+
53+
54+
}//end class
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PropertyTypeHandlingOldArrayFormatTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingOldArrayFormatSniff.php">
5+
<properties>
6+
<property name="expectsOldSchoolArrayWithOnlyValues" type="array" value="string, 10, 1.5, null, true, false" />
7+
8+
<property name="expectsOldSchoolArrayWithKeysAndValues" type="array" value="string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false" />
9+
10+
<property name="expectsOldSchoolArrayWithExtendedValues" type="array" value="string" />
11+
<property name="expectsOldSchoolArrayWithExtendedValues" type="array" extend="true" value="15,another string" />
12+
13+
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" value="10=>10,string=>string" />
14+
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" extend="true" value="15=>15,another string=>another string" />
15+
16+
<property name="expectsOldSchoolEmptyArray" type="array" value=""/>
17+
</properties>
18+
</rule>
19+
</ruleset>

tests/Core/Ruleset/PropertyTypeHandlingTest.php

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,6 @@ final class PropertyTypeHandlingTest extends TestCase
3838
const SNIFF_CLASS = 'Fixtures\\TestStandard\\Sniffs\\SetProperty\\PropertyTypeHandlingSniff';
3939

4040

41-
/**
42-
* Verify a deprecation notice is shown when an array property is set from the ruleset using a comma-separated string.
43-
*
44-
* Support for this format was (soft) deprecated in PHPCS 3.3.0.
45-
*
46-
* @return void
47-
*/
48-
public function testUsingOldSchoolArrayFormatShowsDeprecationNotice()
49-
{
50-
$regex = '`^(';
51-
$regex .= 'DEPRECATED: Passing an array of values to a property using a comma-separated string\R';
52-
$regex .= 'was deprecated in PHP_CodeSniffer 3\.3\.0\. Support will be removed in PHPCS 4\.0\.0\.\R';
53-
$regex .= 'The deprecated syntax was used for property "expectsOldSchool(?:EmptyArray|ArrayWith(?:Extended|Only)?(?:KeysAnd)?Values)"\R';
54-
$regex .= 'for sniff "';
55-
$regex .= '(?:\./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff\.php|TestStandard\.SetProperty\.PropertyTypeHandling)';
56-
$regex .= '"\.\R';
57-
$regex .= 'Pass array values via <element \[key="\.\.\." \]value="\.\.\."> nodes instead\.\R';
58-
$regex .= '){14}\R$`';
59-
60-
$this->expectOutputRegex($regex);
61-
62-
// Set up the ruleset.
63-
$standard = __DIR__.'/PropertyTypeHandlingTest.xml';
64-
$config = new ConfigDouble(["--standard=$standard"]);
65-
new Ruleset($config);
66-
67-
}//end testUsingOldSchoolArrayFormatShowsDeprecationNotice()
68-
69-
7041
/**
7142
* Test the value type handling for properties set via a ruleset.
7243
*
@@ -186,30 +157,18 @@ public static function dataTypeHandling()
186157
'propertyName' => 'expectsBooleanFalseTrimmed',
187158
'expected' => false,
188159
],
189-
'Array with only values (new style)' => [
160+
'Array with only values' => [
190161
'propertyName' => 'expectsArrayWithOnlyValues',
191162
'expected' => $expectedArrayOnlyValues,
192163
],
193-
'Array with keys and values (new style)' => [
164+
'Array with keys and values' => [
194165
'propertyName' => 'expectsArrayWithKeysAndValues',
195166
'expected' => $expectedArrayKeysAndValues,
196167
],
197-
'Empty array (new style)' => [
168+
'Empty array' => [
198169
'propertyName' => 'expectsEmptyArray',
199170
'expected' => [],
200171
],
201-
'Array with only values (old style)' => [
202-
'propertyName' => 'expectsOldSchoolArrayWithOnlyValues',
203-
'expected' => $expectedArrayOnlyValues,
204-
],
205-
'Array with keys and values (old style)' => [
206-
'propertyName' => 'expectsOldSchoolArrayWithKeysAndValues',
207-
'expected' => $expectedArrayKeysAndValues,
208-
],
209-
'Empty array (old style)' => [
210-
'propertyName' => 'expectsOldSchoolEmptyArray',
211-
'expected' => [],
212-
],
213172
];
214173

215174
}//end dataTypeHandling()
@@ -239,22 +198,14 @@ public static function dataArrayPropertyExtending()
239198
];
240199

241200
return [
242-
'Array with only values extended (new style)' => [
201+
'Array with only values extended' => [
243202
'propertyName' => 'expectsArrayWithExtendedValues',
244203
'expected' => $expectedArrayOnlyValuesExtended,
245204
],
246-
'Array with keys and values extended (new style)' => [
205+
'Array with keys and values extended' => [
247206
'propertyName' => 'expectsArrayWithExtendedKeysAndValues',
248207
'expected' => $expectedArrayKeysAndValuesExtended,
249208
],
250-
'Array with only values extended (old style)' => [
251-
'propertyName' => 'expectsOldSchoolArrayWithExtendedValues',
252-
'expected' => $expectedArrayOnlyValuesExtended,
253-
],
254-
'Array with keys and values extended (old style)' => [
255-
'propertyName' => 'expectsOldSchoolArrayWithExtendedKeysAndValues',
256-
'expected' => $expectedArrayKeysAndValuesExtended,
257-
],
258209
];
259210

260211
}//end dataArrayPropertyExtending()
@@ -263,9 +214,6 @@ public static function dataArrayPropertyExtending()
263214
/**
264215
* Test Helper.
265216
*
266-
* Note: the deprecations for using comma-separated string to pass an array, are silenced in this helper
267-
* as that's not what's being tested here.
268-
*
269217
* @see self::testTypeHandlingWhenSetViaRuleset()
270218
*
271219
* @return \PHP_CodeSniffer\Sniffs\Sniff
@@ -277,7 +225,7 @@ private function getSniffObjectForRuleset()
277225
if (isset($sniffObject) === false) {
278226
// Set up the ruleset.
279227
$standard = __DIR__.'/PropertyTypeHandlingTest.xml';
280-
$config = new ConfigDouble(["--standard=$standard", '-q']);
228+
$config = new ConfigDouble(["--standard=$standard"]);
281229
$ruleset = new Ruleset($config);
282230

283231
// Verify that our target sniff has been registered.

tests/Core/Ruleset/PropertyTypeHandlingTest.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,6 @@
5959
</property>
6060

6161
<property name="expectsEmptyArray" type="array"/>
62-
63-
<property name="expectsOldSchoolArrayWithOnlyValues" type="array" value="string, 10, 1.5, null, true, false" />
64-
65-
<property name="expectsOldSchoolArrayWithKeysAndValues" type="array" value="string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false" />
66-
67-
<property name="expectsOldSchoolArrayWithExtendedValues" type="array" value="string" />
68-
<property name="expectsOldSchoolArrayWithExtendedValues" type="array" extend="true" value="15,another string" />
69-
70-
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" value="10=>10,string=>string" />
71-
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" extend="true" value="15=>15,another string=>another string" />
72-
73-
<property name="expectsOldSchoolEmptyArray" type="array" value=""/>
7462
</properties>
7563
</rule>
7664

0 commit comments

Comments
 (0)