Skip to content

Commit 12b604d

Browse files
authored
Merge pull request #978 from PHPCSStandards/phpcs-4.0/feature/sq-3629-ruleset-setsniffproperty-remove-bclayer-old-property-format
Ruleset::setSniffProperty(): remove BC-layer for direct calls passing incorrect $settings array format
2 parents 72ad3e1 + d6a0296 commit 12b604d

File tree

2 files changed

+0
-166
lines changed

2 files changed

+0
-166
lines changed

src/Ruleset.php

-28
Original file line numberDiff line numberDiff line change
@@ -1599,34 +1599,6 @@ public function setSniffProperty($sniffClass, $name, $settings)
15991599
$propertyName = substr($propertyName, 0, -2);
16001600
}
16011601

1602-
/*
1603-
* BC-compatibility layer for $settings using the pre-PHPCS 3.8.0 format.
1604-
*
1605-
* Prior to PHPCS 3.8.0, `$settings` was expected to only contain the new _value_
1606-
* for the property (which could be an array).
1607-
* Since PHPCS 3.8.0, `$settings` is expected to be an array with two keys: 'scope'
1608-
* and 'value', where 'scope' indicates whether the property should be set to the given 'value'
1609-
* for one individual sniff or for all sniffs in a standard.
1610-
*
1611-
* This BC-layer is only for integrations with PHPCS which may call this method directly
1612-
* and will be removed in PHPCS 4.0.0.
1613-
*/
1614-
1615-
if (is_array($settings) === false
1616-
|| isset($settings['scope'], $settings['value']) === false
1617-
) {
1618-
// This will be an "old" format value.
1619-
$settings = [
1620-
'value' => $settings,
1621-
'scope' => 'standard',
1622-
];
1623-
1624-
trigger_error(
1625-
__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.',
1626-
E_USER_DEPRECATED
1627-
);
1628-
}
1629-
16301602
$isSettable = false;
16311603
$sniffObject = $this->sniffs[$sniffClass];
16321604
if (property_exists($sniffObject, $propertyName) === true

tests/Core/Ruleset/SetSniffPropertyTest.php

-138
Original file line numberDiff line numberDiff line change
@@ -280,142 +280,4 @@ public function testDirectCallWithNewArrayFormatSetsProperty()
280280
}//end testDirectCallWithNewArrayFormatSetsProperty()
281281

282282

283-
/**
284-
* Test that setting a property via a direct call to the Ruleset::setSniffProperty() method
285-
* sets the property correctly when using the old $settings array format.
286-
*
287-
* Tested by silencing the deprecation notice as otherwise the test would fail on the deprecation notice.
288-
*
289-
* @param mixed $propertyValue Value for the property to set.
290-
*
291-
* @dataProvider dataDirectCallWithOldArrayFormatSetsProperty
292-
*
293-
* @return void
294-
*/
295-
public function testDirectCallWithOldArrayFormatSetsProperty($propertyValue)
296-
{
297-
$name = 'AllowedAsDeclared';
298-
$sniffCode = "TestStandard.SetProperty.{$name}";
299-
$sniffClass = 'Fixtures\TestStandard\Sniffs\SetProperty\\'.$name.'Sniff';
300-
301-
// Set up the ruleset.
302-
$standard = __DIR__."/SetProperty{$name}Test.xml";
303-
$config = new ConfigDouble(["--standard=$standard"]);
304-
$ruleset = new Ruleset($config);
305-
306-
$propertyName = 'arbitrarystring';
307-
308-
@$ruleset->setSniffProperty(
309-
$sniffClass,
310-
$propertyName,
311-
$propertyValue
312-
);
313-
314-
// Verify that the sniff has been registered.
315-
$this->assertGreaterThan(0, count($ruleset->sniffCodes), 'No sniff codes registered');
316-
317-
// Verify that our target sniff has been registered.
318-
$this->assertArrayHasKey($sniffCode, $ruleset->sniffCodes, 'Target sniff not registered');
319-
$this->assertSame($sniffClass, $ruleset->sniffCodes[$sniffCode], 'Target sniff not registered with the correct class');
320-
321-
// Test that the property as declared in the ruleset has been set on the sniff.
322-
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class not listed in registered sniffs');
323-
324-
$sniffObject = $ruleset->sniffs[$sniffClass];
325-
$this->assertSame($propertyValue, $sniffObject->$propertyName, 'Property value not set to expected value');
326-
327-
}//end testDirectCallWithOldArrayFormatSetsProperty()
328-
329-
330-
/**
331-
* Data provider.
332-
*
333-
* @see self::testDirectCallWithOldArrayFormatSetsProperty()
334-
*
335-
* @return array<string, array<string, mixed>>
336-
*/
337-
public static function dataDirectCallWithOldArrayFormatSetsProperty()
338-
{
339-
return [
340-
'Property value is not an array (boolean)' => [
341-
'propertyValue' => false,
342-
],
343-
'Property value is not an array (string)' => [
344-
'propertyValue' => 'a string',
345-
],
346-
'Property value is an empty array' => [
347-
'propertyValue' => [],
348-
],
349-
'Property value is an array without keys' => [
350-
'propertyValue' => [
351-
'value',
352-
false,
353-
],
354-
],
355-
'Property value is an array without the "scope" or "value" keys' => [
356-
'propertyValue' => [
357-
'key1' => 'value',
358-
'key2' => false,
359-
],
360-
],
361-
'Property value is an array without the "scope" key' => [
362-
'propertyValue' => [
363-
'key1' => 'value',
364-
'value' => true,
365-
],
366-
],
367-
'Property value is an array without the "value" key' => [
368-
'propertyValue' => [
369-
'scope' => 'value',
370-
'key2' => 1234,
371-
],
372-
],
373-
];
374-
375-
}//end dataDirectCallWithOldArrayFormatSetsProperty()
376-
377-
378-
/**
379-
* Test that setting a property via a direct call to the Ruleset::setSniffProperty() method
380-
* throws a deprecation notice when using the old $settings array format.
381-
*
382-
* Note: as PHPUnit stops as soon as it sees the deprecation notice, the setting of the property
383-
* value is not tested here.
384-
*
385-
* @return void
386-
*/
387-
public function testDirectCallWithOldArrayFormatThrowsDeprecationNotice()
388-
{
389-
$exceptionClass = 'PHPUnit\Framework\Error\Deprecated';
390-
if (class_exists($exceptionClass) === false) {
391-
$exceptionClass = 'PHPUnit_Framework_Error_Deprecated';
392-
}
393-
394-
$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.';
395-
396-
if (method_exists($this, 'expectException') === true) {
397-
$this->expectException($exceptionClass);
398-
$this->expectExceptionMessage($exceptionMsg);
399-
} else {
400-
// PHPUnit < 5.2.0.
401-
$this->setExpectedException($exceptionClass, $exceptionMsg);
402-
}
403-
404-
$name = 'AllowedAsDeclared';
405-
$sniffClass = 'Fixtures\TestStandard\Sniffs\SetProperty\\'.$name.'Sniff';
406-
407-
// Set up the ruleset.
408-
$standard = __DIR__."/SetProperty{$name}Test.xml";
409-
$config = new ConfigDouble(["--standard=$standard"]);
410-
$ruleset = new Ruleset($config);
411-
412-
$ruleset->setSniffProperty(
413-
$sniffClass,
414-
'arbitrarystring',
415-
['key' => 'value']
416-
);
417-
418-
}//end testDirectCallWithOldArrayFormatThrowsDeprecationNotice()
419-
420-
421283
}//end class

0 commit comments

Comments
 (0)