Skip to content

Commit 2ffa09b

Browse files
committed
Helper::setConfigData(): PHPCS 4.x compatibility
As of PHPCS 4.x, the `PHP_CodeSniffer\Config::setConfigData()` method is no longer static. Ref: squizlabs/PHP_CodeSniffer@10a89a2 This commit adds a new `$config` parameter to the `Helper::setConfigData()` method which will be a required parameter for PHPCS 4.x. Includes adjusted unit tests.
1 parent f29c62f commit 2ffa09b

File tree

3 files changed

+95
-14
lines changed

3 files changed

+95
-14
lines changed

PHPCSUtils/BackCompat/Helper.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHPCSUtils\BackCompat;
1212

1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Exceptions\RuntimeException;
1415

1516
/**
1617
* Utility methods to retrieve (configuration) information from PHP_CodeSniffer.
@@ -60,21 +61,35 @@ public static function getVersion()
6061
*
6162
* @since 1.0.0
6263
*
63-
* @param string $key The name of the config value.
64-
* @param string|null $value The value to set. If null, the config entry
65-
* is deleted, reverting it to the default value.
66-
* @param bool $temp Set this config data temporarily for this script run.
67-
* This will not write the config data to the config file.
64+
* @param string $key The name of the config value.
65+
* @param string|null $value The value to set. If null, the config entry
66+
* is deleted, reverting it to the default value.
67+
* @param bool $temp Set this config data temporarily for this script run.
68+
* This will not write the config data to the config file.
69+
* @param \PHP_CodeSniffer\Config $config The PHPCS config object.
70+
* This parameter is required for PHPCS 4.x, optional
71+
* for PHPCS 3.x and not possible to pass for PHPCS 2.x.
72+
* Passing the `$phpcsFile->config` property should work
73+
* in PHPCS 3.x and higher.
6874
*
6975
* @return bool Whether the setting of the data was successfull.
7076
*/
71-
public static function setConfigData($key, $value, $temp = false)
77+
public static function setConfigData($key, $value, $temp = false, $config = null)
7278
{
7379
if (\method_exists('\PHP_CodeSniffer\Config', 'setConfigData') === false) {
7480
// PHPCS 2.x.
7581
return \PHP_CodeSniffer::setConfigData($key, $value, $temp);
7682
}
7783

84+
if (isset($config) === true) {
85+
// PHPCS 3.x and 4.x.
86+
return $config->setConfigData($key, $value, $temp);
87+
}
88+
89+
if (version_compare(self::getVersion(), '3.99.99', '>') === true) {
90+
throw new RuntimeException('Passing the $config parameter is required in PHPCS 4.x');
91+
}
92+
7893
// PHPCS 3.x.
7994
return \PHP_CodeSniffer\Config::setConfigData($key, $value, $temp);
8095
}

Tests/BackCompat/Helper/ConfigDataTest.php

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\BackCompat\Helper;
1212

13+
use PHP_CodeSniffer\Config;
1314
use PHPCSUtils\BackCompat\Helper;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -27,12 +28,41 @@ class ConfigDataTest extends TestCase
2728
{
2829

2930
/**
30-
* Test the getConfigData() and setConfigData() method.
31+
* Test the getConfigData() and setConfigData() method when used in a cross-version compatible manner.
3132
*
3233
* @return void
3334
*/
34-
public function testConfigData()
35+
public function testConfigData34()
3536
{
37+
if (version_compare(Helper::getVersion(), '2.99.99', '<=') === true) {
38+
$this->markTestSkipped('Test only applicable to PHPCS > 2.x');
39+
}
40+
41+
$config = new Config();
42+
$original = Helper::getConfigData('arbitrary_name');
43+
$expected = 'expected';
44+
45+
$return = Helper::setConfigData('arbitrary_name', $expected, true, $config);
46+
$this->assertTrue($return);
47+
48+
$result = Helper::getConfigData('arbitrary_name');
49+
$this->assertSame($expected, $result);
50+
51+
// Reset the value after the test.
52+
Helper::setConfigData('arbitrary_name', $original, true, $config);
53+
}
54+
55+
/**
56+
* Test the getConfigData() and setConfigData() method when used in a non-PHPCS 4.x compatible manner.
57+
*
58+
* @return void
59+
*/
60+
public function testConfigDataPHPCS23()
61+
{
62+
if (version_compare(Helper::getVersion(), '3.99.99', '>') === true) {
63+
$this->markTestSkipped('Test only applicable to PHPCS < 4.x');
64+
}
65+
3666
$original = Helper::getConfigData('arbitrary_name');
3767
$expected = 'expected';
3868

@@ -43,6 +73,32 @@ public function testConfigData()
4373
$this->assertSame($expected, $result);
4474

4575
// Reset the value after the test.
46-
$return = Helper::setConfigData('arbitrary_name', $original, true);
76+
Helper::setConfigData('arbitrary_name', $original, true);
77+
}
78+
79+
/**
80+
* Test the getConfigData() and setConfigData() method when used in a non-PHPCS 4.x compatible manner.
81+
*
82+
* @return void
83+
*/
84+
public function testConfigDataPHPCS4Exception()
85+
{
86+
if (version_compare(Helper::getVersion(), '3.99.99', '<=') === true) {
87+
$this->markTestSkipped('Test only applicable to PHPCS 4.x');
88+
}
89+
90+
$msg = 'Passing the $config parameter is required in PHPCS 4.x';
91+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
92+
93+
if (\method_exists($this, 'expectException')) {
94+
// PHPUnit 5+.
95+
$this->expectException($exception);
96+
$this->expectExceptionMessage($msg);
97+
} else {
98+
// PHPUnit 4.
99+
$this->setExpectedException($exception, $msg);
100+
}
101+
102+
Helper::setConfigData('arbitrary_name', 'test', true);
47103
}
48104
}

Tests/BackCompat/Helper/GetCommandLineDataTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,21 @@ public function testGetEncodingWithoutPHPCSFile()
132132
$expected = \version_compare(static::$phpcsVersion, '2.99.99', '>') ? 'utf-8' : 'iso-8859-1';
133133
$this->assertSame($expected, $result, 'Failed retrieving the default encoding');
134134

135-
Helper::setConfigData('encoding', 'utf-16', true);
135+
$config = null;
136+
if (isset(self::$phpcsFile->config) === true) {
137+
$config = self::$phpcsFile->config;
138+
}
139+
140+
Helper::setConfigData('encoding', 'utf-16', true, $config);
136141

137142
$result = Helper::getEncoding();
138143
$this->assertSame('utf-16', $result, 'Failed retrieving the custom set encoding');
139144

140145
// Restore defaults before moving to the next test.
141146
if (\version_compare(static::$phpcsVersion, '2.99.99', '>') === true) {
142-
Helper::setConfigData('encoding', 'utf-8', true);
147+
Helper::setConfigData('encoding', 'utf-8', true, $config);
143148
} else {
144-
Helper::setConfigData('encoding', 'iso-8859-1', true);
149+
Helper::setConfigData('encoding', 'iso-8859-1', true, $config);
145150
}
146151
}
147152

@@ -197,13 +202,18 @@ public function testIgnoreAnnotationsV3SetViaMethod()
197202
$this->markTestSkipped('Test only applicable to PHPCS 3.x');
198203
}
199204

200-
Helper::setConfigData('annotations', false, true);
205+
$config = null;
206+
if (isset(self::$phpcsFile->config) === true) {
207+
$config = self::$phpcsFile->config;
208+
}
209+
210+
Helper::setConfigData('annotations', false, true, $config);
201211

202212
$result = Helper::ignoreAnnotations();
203213
$this->assertTrue($result);
204214

205215
// Restore defaults before moving to the next test.
206-
Helper::setConfigData('annotations', true, true);
216+
Helper::setConfigData('annotations', true, true, $config);
207217
}
208218

209219
/**

0 commit comments

Comments
 (0)