Skip to content

Commit f2274bd

Browse files
committed
Ruleset: hard deprecate use of the old property array format
This format was (soft) deprecated in PHPCS 3.3.0 (June 2018) and it is expected that most rulesets will have been updated. With the new "error handling" functionality in the Ruleset class in place (PR 857), the old format can now be hard deprecated to make users who haven't updated their rulesets yet, aware of the upcoming removal of support for the old array format. Refs: * https://github.com/PHPCSStandards/PHP_CodeSniffer/releases/tag/3.3.0 * squizlabs/PHP_CodeSniffer 1665 Closes 693
1 parent ee7e4f0 commit f2274bd

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Ruleset.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,14 @@ private function processRule($rule, $newSniffs, $depth=0)
12191219
}
12201220

12211221
$printValue = rtrim($printValue, ',');
1222-
} else {
1222+
} else if (isset($prop['value']) === true) {
1223+
$message = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL;
1224+
$message .= 'was deprecated in PHP_CodeSniffer 3.3.0. Support will be removed in PHPCS 4.0.0.'.PHP_EOL;
1225+
$message .= "The deprecated syntax was used for property \"$name\"".PHP_EOL;
1226+
$message .= "for sniff \"$code\".".PHP_EOL;
1227+
$message .= 'Pass array values via <element [key="..." ]value="..."> nodes instead.';
1228+
$this->msgCache->add($message, MessageCollector::DEPRECATED);
1229+
12231230
$value = (string) $prop['value'];
12241231
$printValue = $value;
12251232
if ($value !== '') {

tests/Core/Ruleset/PropertyTypeHandlingTest.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ 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+
4170
/**
4271
* Test the value type handling for properties set via a ruleset.
4372
*
@@ -215,6 +244,9 @@ public static function dataTypeHandling()
215244
/**
216245
* Test Helper.
217246
*
247+
* Note: the deprecations for using comma-separated string to pass an array, are silenced in this helper
248+
* as that's not what's being tested here.
249+
*
218250
* @see self::testTypeHandlingWhenSetViaRuleset()
219251
*
220252
* @return \PHP_CodeSniffer\Sniffs\Sniff
@@ -226,7 +258,7 @@ private function getSniffObjectForRuleset()
226258
if (isset($sniffObject) === false) {
227259
// Set up the ruleset.
228260
$standard = __DIR__."/PropertyTypeHandlingTest.xml";
229-
$config = new ConfigDouble(["--standard=$standard"]);
261+
$config = new ConfigDouble(["--standard=$standard", '-q']);
230262
$ruleset = new Ruleset($config);
231263

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

0 commit comments

Comments
 (0)