From d6a0296a64a29a0358b246ee1b41741dbbf0b7aa Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 8 Mar 2025 23:00:13 +0100 Subject: [PATCH] Ruleset::setSniffProperty(): remove BC-layer for direct calls passing incorrect $settings array format The old array format for the `$settings` parameter of `Ruleset::setSniffProperty()` was deprecated in PHPCS 3.8.0. Ref: * squizlabs/PHP_CodeSniffer 3629 Related to 6 --- src/Ruleset.php | 28 ---- tests/Core/Ruleset/SetSniffPropertyTest.php | 138 -------------------- 2 files changed, 166 deletions(-) diff --git a/src/Ruleset.php b/src/Ruleset.php index b09b4e7562..7a25bf31ca 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -1599,34 +1599,6 @@ public function setSniffProperty($sniffClass, $name, $settings) $propertyName = substr($propertyName, 0, -2); } - /* - * BC-compatibility layer for $settings using the pre-PHPCS 3.8.0 format. - * - * Prior to PHPCS 3.8.0, `$settings` was expected to only contain the new _value_ - * for the property (which could be an array). - * Since PHPCS 3.8.0, `$settings` is expected to be an array with two keys: 'scope' - * and 'value', where 'scope' indicates whether the property should be set to the given 'value' - * for one individual sniff or for all sniffs in a standard. - * - * This BC-layer is only for integrations with PHPCS which may call this method directly - * and will be removed in PHPCS 4.0.0. - */ - - if (is_array($settings) === false - || isset($settings['scope'], $settings['value']) === false - ) { - // This will be an "old" format value. - $settings = [ - 'value' => $settings, - 'scope' => 'standard', - ]; - - trigger_error( - __FUNCTION__.': the format of the $settings parameter has changed from (mixed) $value to array(\'scope\' => \'sniff|standard\', \'value\' => $value). Please update your integration code. See PR #3629 for more information.', - E_USER_DEPRECATED - ); - } - $isSettable = false; $sniffObject = $this->sniffs[$sniffClass]; if (property_exists($sniffObject, $propertyName) === true diff --git a/tests/Core/Ruleset/SetSniffPropertyTest.php b/tests/Core/Ruleset/SetSniffPropertyTest.php index b9d9ac1f29..fa12138a82 100644 --- a/tests/Core/Ruleset/SetSniffPropertyTest.php +++ b/tests/Core/Ruleset/SetSniffPropertyTest.php @@ -280,142 +280,4 @@ public function testDirectCallWithNewArrayFormatSetsProperty() }//end testDirectCallWithNewArrayFormatSetsProperty() - /** - * Test that setting a property via a direct call to the Ruleset::setSniffProperty() method - * sets the property correctly when using the old $settings array format. - * - * Tested by silencing the deprecation notice as otherwise the test would fail on the deprecation notice. - * - * @param mixed $propertyValue Value for the property to set. - * - * @dataProvider dataDirectCallWithOldArrayFormatSetsProperty - * - * @return void - */ - public function testDirectCallWithOldArrayFormatSetsProperty($propertyValue) - { - $name = 'AllowedAsDeclared'; - $sniffCode = "TestStandard.SetProperty.{$name}"; - $sniffClass = 'Fixtures\TestStandard\Sniffs\SetProperty\\'.$name.'Sniff'; - - // Set up the ruleset. - $standard = __DIR__."/SetProperty{$name}Test.xml"; - $config = new ConfigDouble(["--standard=$standard"]); - $ruleset = new Ruleset($config); - - $propertyName = 'arbitrarystring'; - - @$ruleset->setSniffProperty( - $sniffClass, - $propertyName, - $propertyValue - ); - - // Verify that the sniff has been registered. - $this->assertGreaterThan(0, count($ruleset->sniffCodes), 'No sniff codes registered'); - - // Verify that our target sniff has been registered. - $this->assertArrayHasKey($sniffCode, $ruleset->sniffCodes, 'Target sniff not registered'); - $this->assertSame($sniffClass, $ruleset->sniffCodes[$sniffCode], 'Target sniff not registered with the correct class'); - - // Test that the property as declared in the ruleset has been set on the sniff. - $this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class not listed in registered sniffs'); - - $sniffObject = $ruleset->sniffs[$sniffClass]; - $this->assertSame($propertyValue, $sniffObject->$propertyName, 'Property value not set to expected value'); - - }//end testDirectCallWithOldArrayFormatSetsProperty() - - - /** - * Data provider. - * - * @see self::testDirectCallWithOldArrayFormatSetsProperty() - * - * @return array> - */ - public static function dataDirectCallWithOldArrayFormatSetsProperty() - { - return [ - 'Property value is not an array (boolean)' => [ - 'propertyValue' => false, - ], - 'Property value is not an array (string)' => [ - 'propertyValue' => 'a string', - ], - 'Property value is an empty array' => [ - 'propertyValue' => [], - ], - 'Property value is an array without keys' => [ - 'propertyValue' => [ - 'value', - false, - ], - ], - 'Property value is an array without the "scope" or "value" keys' => [ - 'propertyValue' => [ - 'key1' => 'value', - 'key2' => false, - ], - ], - 'Property value is an array without the "scope" key' => [ - 'propertyValue' => [ - 'key1' => 'value', - 'value' => true, - ], - ], - 'Property value is an array without the "value" key' => [ - 'propertyValue' => [ - 'scope' => 'value', - 'key2' => 1234, - ], - ], - ]; - - }//end dataDirectCallWithOldArrayFormatSetsProperty() - - - /** - * Test that setting a property via a direct call to the Ruleset::setSniffProperty() method - * throws a deprecation notice when using the old $settings array format. - * - * Note: as PHPUnit stops as soon as it sees the deprecation notice, the setting of the property - * value is not tested here. - * - * @return void - */ - public function testDirectCallWithOldArrayFormatThrowsDeprecationNotice() - { - $exceptionClass = 'PHPUnit\Framework\Error\Deprecated'; - if (class_exists($exceptionClass) === false) { - $exceptionClass = 'PHPUnit_Framework_Error_Deprecated'; - } - - $exceptionMsg = 'the format of the $settings parameter has changed from (mixed) $value to array(\'scope\' => \'sniff|standard\', \'value\' => $value). Please update your integration code. See PR #3629 for more information.'; - - if (method_exists($this, 'expectException') === true) { - $this->expectException($exceptionClass); - $this->expectExceptionMessage($exceptionMsg); - } else { - // PHPUnit < 5.2.0. - $this->setExpectedException($exceptionClass, $exceptionMsg); - } - - $name = 'AllowedAsDeclared'; - $sniffClass = 'Fixtures\TestStandard\Sniffs\SetProperty\\'.$name.'Sniff'; - - // Set up the ruleset. - $standard = __DIR__."/SetProperty{$name}Test.xml"; - $config = new ConfigDouble(["--standard=$standard"]); - $ruleset = new Ruleset($config); - - $ruleset->setSniffProperty( - $sniffClass, - 'arbitrarystring', - ['key' => 'value'] - ); - - }//end testDirectCallWithOldArrayFormatThrowsDeprecationNotice() - - }//end class