Skip to content

Ruleset::setSniffProperty(): remove BC-layer for direct calls passing incorrect $settings array format #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
138 changes: 0 additions & 138 deletions tests/Core/Ruleset/SetSniffPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array<string, mixed>>
*/
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