diff --git a/src/Ruleset.php b/src/Ruleset.php index 8f04c8d7f8..b09b4e7562 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -1199,6 +1199,17 @@ private function processRule($rule, $newSniffs, $depth=0) if (isset($prop['type']) === true && (string) $prop['type'] === 'array' ) { + if (isset($prop['value']) === true) { + $message = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL; + $message .= 'is no longer supported since PHP_CodeSniffer 4.0.0.'.PHP_EOL; + $message .= "The unsupported syntax was used for property \"$name\"".PHP_EOL; + $message .= "for sniff \"$code\".".PHP_EOL; + $message .= 'Pass array values via nodes instead.'; + $this->msgCache->add($message, MessageCollector::ERROR); + + continue; + } + $values = []; if (isset($prop['extend']) === true && (string) $prop['extend'] === 'true' @@ -1226,27 +1237,7 @@ private function processRule($rule, $newSniffs, $depth=0) } $printValue = rtrim($printValue, ','); - } else if (isset($prop['value']) === true) { - $message = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL; - $message .= 'was deprecated in PHP_CodeSniffer 3.3.0. Support will be removed in PHPCS 4.0.0.'.PHP_EOL; - $message .= "The deprecated syntax was used for property \"$name\"".PHP_EOL; - $message .= "for sniff \"$code\".".PHP_EOL; - $message .= 'Pass array values via nodes instead.'; - $this->msgCache->add($message, MessageCollector::DEPRECATED); - - $value = (string) $prop['value']; - $printValue = $value; - if ($value !== '') { - foreach (explode(',', $value) as $val) { - list($k, $v) = explode('=>', $val.'=>'); - if ($v !== '') { - $values[trim($k)] = trim($v); - } else { - $values[] = trim($k); - } - } - } - }//end if + } $this->ruleset[$code]['properties'][$name] = [ 'value' => $values, diff --git a/tests/Core/Ruleset/ExplainTest.php b/tests/Core/Ruleset/ExplainTest.php index 3e52cef685..2a3b56b97b 100644 --- a/tests/Core/Ruleset/ExplainTest.php +++ b/tests/Core/Ruleset/ExplainTest.php @@ -184,9 +184,9 @@ public function testExplainWithDeprecatedSniffs() $ruleset = new Ruleset($config); $expected = PHP_EOL; - $expected .= 'The ShowSniffDeprecationsTest standard contains 11 sniffs'.PHP_EOL.PHP_EOL; + $expected .= 'The ShowSniffDeprecationsTest standard contains 12 sniffs'.PHP_EOL.PHP_EOL; - $expected .= 'TestStandard (11 sniffs)'.PHP_EOL; + $expected .= 'TestStandard (12 sniffs)'.PHP_EOL; $expected .= '------------------------'.PHP_EOL; $expected .= ' TestStandard.Deprecated.WithLongReplacement *'.PHP_EOL; $expected .= ' TestStandard.Deprecated.WithoutReplacement *'.PHP_EOL; @@ -198,6 +198,7 @@ public function testExplainWithDeprecatedSniffs() $expected .= ' TestStandard.SetProperty.AllowedViaStdClass'.PHP_EOL; $expected .= ' TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL; $expected .= ' TestStandard.SetProperty.PropertyTypeHandling'.PHP_EOL; + $expected .= ' TestStandard.SetProperty.PropertyTypeHandlingOldArrayFormat'.PHP_EOL; $expected .= ' TestStandard.ValidSniffs.RegisterEmptyArray'.PHP_EOL.PHP_EOL; $expected .= '* Sniffs marked with an asterix are deprecated.'.PHP_EOL; diff --git a/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc b/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc index 31caeb6198..240c5c7bb2 100644 --- a/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc +++ b/tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc @@ -24,8 +24,4 @@ // 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 expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false -// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false -// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolEmptyArray[] - echo 'hello!'; diff --git a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingOldArrayFormatSniff.php b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingOldArrayFormatSniff.php new file mode 100644 index 0000000000..fff048d1e1 --- /dev/null +++ b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingOldArrayFormatSniff.php @@ -0,0 +1,60 @@ + + */ + public $expectsOldSchoolArrayWithOnlyValues; + + /** + * Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0. + * + * @var array + */ + public $expectsOldSchoolArrayWithKeysAndValues; + + /** + * Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0. + * + * @var array + */ + public $expectsOldSchoolArrayWithExtendedValues; + + /** + * Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0. + * + * @var array + */ + public $expectsOldSchoolArrayWithExtendedKeysAndValues; + + /** + * Used to verify that array properties passed as a string is no longer supported since PHPCS 4.0.0. + * + * @var array + */ + public $expectsOldSchoolEmptyArray; + + public function register() + { + return [T_ECHO]; + } + + public function process(File $phpcsFile, $stackPtr) + { + // Do something. + } +} diff --git a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php index 3c5fceb999..3fd350cf44 100644 --- a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php +++ b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php @@ -143,41 +143,6 @@ final class PropertyTypeHandlingSniff implements Sniff */ public $expectsEmptyArray; - /** - * Used to verify that array properties passed as a string get parsed to a proper array. - * - * @var array - */ - public $expectsOldSchoolArrayWithOnlyValues; - - /** - * Used to verify that array properties passed as a string with keys get parsed to a proper array. - * - * @var array - */ - public $expectsOldSchoolArrayWithKeysAndValues; - - /** - * Used to verify that array properties passed as a string can get extended. - * - * @var array - */ - public $expectsOldSchoolArrayWithExtendedValues; - - /** - * Used to verify that array properties passed as a string can get extended. - * - * @var array - */ - public $expectsOldSchoolArrayWithExtendedKeysAndValues; - - /** - * Used to verify that array properties passed as a string allow for setting a property to an empty array. - * - * @var array - */ - public $expectsOldSchoolEmptyArray; - public function register() { return [T_ECHO]; diff --git a/tests/Core/Ruleset/ProcessRulesetTest.php b/tests/Core/Ruleset/ProcessRulesetTest.php index d37c031021..3ef1f67688 100644 --- a/tests/Core/Ruleset/ProcessRulesetTest.php +++ b/tests/Core/Ruleset/ProcessRulesetTest.php @@ -74,6 +74,7 @@ public function testAutoExpandSniffsDirectory() "$std.SetProperty.AllowedViaStdClass" => "$sniffDir\SetProperty\AllowedViaStdClassSniff", "$std.SetProperty.NotAllowedViaAttribute" => "$sniffDir\SetProperty\NotAllowedViaAttributeSniff", "$std.SetProperty.PropertyTypeHandling" => "$sniffDir\SetProperty\PropertyTypeHandlingSniff", + "$std.SetProperty.PropertyTypeHandlingOldArrayFormat" => "$sniffDir\SetProperty\PropertyTypeHandlingOldArrayFormatSniff", "$std.ValidSniffs.RegisterEmptyArray" => "$sniffDir\ValidSniffs\RegisterEmptyArraySniff", ]; diff --git a/tests/Core/Ruleset/PropertyTypeHandlingOldArrayFormatTest.php b/tests/Core/Ruleset/PropertyTypeHandlingOldArrayFormatTest.php new file mode 100644 index 0000000000..c7719aacd0 --- /dev/null +++ b/tests/Core/Ruleset/PropertyTypeHandlingOldArrayFormatTest.php @@ -0,0 +1,54 @@ + + * @copyright 2025 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Ruleset; + +use PHP_CodeSniffer\Ruleset; +use PHP_CodeSniffer\Tests\ConfigDouble; +use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; + +/** + * Test the handling of an array property value using the PHPCS < 4.0 format set via the ruleset. + * + * @covers \PHP_CodeSniffer\Ruleset::setSniffProperty + */ +final class PropertyTypeHandlingOldArrayFormatTest extends AbstractRulesetTestCase +{ + + + /** + * Verify an error is thrown when an array property is set from the ruleset using a comma-separated string. + * + * Support for this format was (soft) deprecated in PHPCS 3.3.0 and removed in PHPCS 4.0.0. + * + * @return void + */ + public function testUsingOldSchoolArrayFormatThrowsError() + { + $regex = '`^('; + $regex .= 'ERROR: Passing an array of values to a property using a comma-separated string\R'; + $regex .= 'is no longer supported since PHP_CodeSniffer 4\.0\.0\.\R'; + $regex .= 'The unsupported syntax was used for property "expectsOldSchool(?:EmptyArray|ArrayWith(?:Extended|Only)?(?:KeysAnd)?Values)"\R'; + $regex .= 'for sniff "'; + $regex .= '(?:\./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingOldArrayFormatSniff\.php|TestStandard\.SetProperty\.PropertyTypeHandlingOldArrayFormat)'; + $regex .= '"\.\R'; + $regex .= 'Pass array values via nodes instead\.\R'; + $regex .= '){14}\R$`'; + + $this->expectRuntimeExceptionRegex($regex); + + // Set up the ruleset. + $standard = __DIR__.'/PropertyTypeHandlingOldArrayFormatTest.xml'; + $config = new ConfigDouble(["--standard=$standard"]); + new Ruleset($config); + + }//end testUsingOldSchoolArrayFormatThrowsError() + + +}//end class diff --git a/tests/Core/Ruleset/PropertyTypeHandlingOldArrayFormatTest.xml b/tests/Core/Ruleset/PropertyTypeHandlingOldArrayFormatTest.xml new file mode 100644 index 0000000000..df7e0ab2f2 --- /dev/null +++ b/tests/Core/Ruleset/PropertyTypeHandlingOldArrayFormatTest.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/tests/Core/Ruleset/PropertyTypeHandlingTest.php b/tests/Core/Ruleset/PropertyTypeHandlingTest.php index 1db15aa5d2..f54ecc0dd0 100644 --- a/tests/Core/Ruleset/PropertyTypeHandlingTest.php +++ b/tests/Core/Ruleset/PropertyTypeHandlingTest.php @@ -38,35 +38,6 @@ final class PropertyTypeHandlingTest extends TestCase const SNIFF_CLASS = 'Fixtures\\TestStandard\\Sniffs\\SetProperty\\PropertyTypeHandlingSniff'; - /** - * Verify a deprecation notice is shown when an array property is set from the ruleset using a comma-separated string. - * - * Support for this format was (soft) deprecated in PHPCS 3.3.0. - * - * @return void - */ - public function testUsingOldSchoolArrayFormatShowsDeprecationNotice() - { - $regex = '`^('; - $regex .= 'DEPRECATED: Passing an array of values to a property using a comma-separated string\R'; - $regex .= 'was deprecated in PHP_CodeSniffer 3\.3\.0\. Support will be removed in PHPCS 4\.0\.0\.\R'; - $regex .= 'The deprecated syntax was used for property "expectsOldSchool(?:EmptyArray|ArrayWith(?:Extended|Only)?(?:KeysAnd)?Values)"\R'; - $regex .= 'for sniff "'; - $regex .= '(?:\./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff\.php|TestStandard\.SetProperty\.PropertyTypeHandling)'; - $regex .= '"\.\R'; - $regex .= 'Pass array values via nodes instead\.\R'; - $regex .= '){14}\R$`'; - - $this->expectOutputRegex($regex); - - // Set up the ruleset. - $standard = __DIR__.'/PropertyTypeHandlingTest.xml'; - $config = new ConfigDouble(["--standard=$standard"]); - new Ruleset($config); - - }//end testUsingOldSchoolArrayFormatShowsDeprecationNotice() - - /** * Test the value type handling for properties set via a ruleset. * @@ -186,30 +157,18 @@ public static function dataTypeHandling() 'propertyName' => 'expectsBooleanFalseTrimmed', 'expected' => false, ], - 'Array with only values (new style)' => [ + 'Array with only values' => [ 'propertyName' => 'expectsArrayWithOnlyValues', 'expected' => $expectedArrayOnlyValues, ], - 'Array with keys and values (new style)' => [ + 'Array with keys and values' => [ 'propertyName' => 'expectsArrayWithKeysAndValues', 'expected' => $expectedArrayKeysAndValues, ], - 'Empty array (new style)' => [ + 'Empty array' => [ 'propertyName' => 'expectsEmptyArray', 'expected' => [], ], - 'Array with only values (old style)' => [ - 'propertyName' => 'expectsOldSchoolArrayWithOnlyValues', - 'expected' => $expectedArrayOnlyValues, - ], - 'Array with keys and values (old style)' => [ - 'propertyName' => 'expectsOldSchoolArrayWithKeysAndValues', - 'expected' => $expectedArrayKeysAndValues, - ], - 'Empty array (old style)' => [ - 'propertyName' => 'expectsOldSchoolEmptyArray', - 'expected' => [], - ], ]; }//end dataTypeHandling() @@ -239,22 +198,14 @@ public static function dataArrayPropertyExtending() ]; return [ - 'Array with only values extended (new style)' => [ + 'Array with only values extended' => [ 'propertyName' => 'expectsArrayWithExtendedValues', 'expected' => $expectedArrayOnlyValuesExtended, ], - 'Array with keys and values extended (new style)' => [ + 'Array with keys and values extended' => [ 'propertyName' => 'expectsArrayWithExtendedKeysAndValues', 'expected' => $expectedArrayKeysAndValuesExtended, ], - 'Array with only values extended (old style)' => [ - 'propertyName' => 'expectsOldSchoolArrayWithExtendedValues', - 'expected' => $expectedArrayOnlyValuesExtended, - ], - 'Array with keys and values extended (old style)' => [ - 'propertyName' => 'expectsOldSchoolArrayWithExtendedKeysAndValues', - 'expected' => $expectedArrayKeysAndValuesExtended, - ], ]; }//end dataArrayPropertyExtending() @@ -263,9 +214,6 @@ public static function dataArrayPropertyExtending() /** * Test Helper. * - * Note: the deprecations for using comma-separated string to pass an array, are silenced in this helper - * as that's not what's being tested here. - * * @see self::testTypeHandlingWhenSetViaRuleset() * * @return \PHP_CodeSniffer\Sniffs\Sniff @@ -277,7 +225,7 @@ private function getSniffObjectForRuleset() if (isset($sniffObject) === false) { // Set up the ruleset. $standard = __DIR__.'/PropertyTypeHandlingTest.xml'; - $config = new ConfigDouble(["--standard=$standard", '-q']); + $config = new ConfigDouble(["--standard=$standard"]); $ruleset = new Ruleset($config); // Verify that our target sniff has been registered. diff --git a/tests/Core/Ruleset/PropertyTypeHandlingTest.xml b/tests/Core/Ruleset/PropertyTypeHandlingTest.xml index c69b0394ef..67a736afbc 100644 --- a/tests/Core/Ruleset/PropertyTypeHandlingTest.xml +++ b/tests/Core/Ruleset/PropertyTypeHandlingTest.xml @@ -59,18 +59,6 @@ - - - - - - - - - - - -