Skip to content

Commit 71d9f67

Browse files
authored
Make test suite compatible with PHPCS 3.8.0 (#304)
As per the deprecation notice being thrown: > `setSniffProperty: 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.` ... the format of the `$settings` parameter for the `Ruleset::setSniffProperty()` method has changed to allow PHPCS to prevent notices about dynamic properties. This commit updates the test suite to allow for both the PHPCS < 3.8.0 as well as the PHPCS 3.8.0+ way of setting properties directly on the Ruleset by adding a helper method to the `BaseTestCase` to set the properties in the correct way depending on the PHPCS version used. Ref: squizlabs/php_codesniffer 3629 Co-authored-by: jrfnl <[email protected]>
1 parent dc5582d commit 71d9f67

File tree

7 files changed

+66
-230
lines changed

7 files changed

+66
-230
lines changed

Tests/BaseTestCase.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,24 @@ public function getFixture($fixtureFilename)
5858
{
5959
return realpath(__DIR__ . '/VariableAnalysisSniff/fixtures/' . $fixtureFilename);
6060
}
61+
62+
public function setSniffProperty($phpcsFile, $property, $value)
63+
{
64+
if (version_compare(Config::VERSION, '3.8.0', '>=')) {
65+
$phpcsFile->ruleset->setSniffProperty(
66+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
67+
$property,
68+
[
69+
'scope' => 'sniff',
70+
'value' => $value,
71+
]
72+
);
73+
} else {
74+
$phpcsFile->ruleset->setSniffProperty(
75+
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
76+
$property,
77+
$value
78+
);
79+
}
80+
}
6181
}

Tests/VariableAnalysisSniff/ArrowFunctionTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ public function testArrowFunctions()
1010
{
1111
$fixtureFile = $this->getFixture('ArrowFunctionFixture.php');
1212
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
13-
$phpcsFile->ruleset->setSniffProperty(
14-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
15-
'allowUnusedParametersBeforeUsed',
16-
'true'
17-
);
13+
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
1814
$phpcsFile->process();
1915
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
2016
$expectedWarnings = [
@@ -41,11 +37,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed()
4137
{
4238
$fixtureFile = $this->getFixture('ArrowFunctionFixture.php');
4339
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
44-
$phpcsFile->ruleset->setSniffProperty(
45-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
46-
'allowUnusedParametersBeforeUsed',
47-
'false'
48-
);
40+
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'false');
4941
$phpcsFile->process();
5042
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
5143
$expectedWarnings = [

Tests/VariableAnalysisSniff/GlobalScopeTest.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ public function testGlobalScopeWarnings()
1010
{
1111
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
1212
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
13-
$phpcsFile->ruleset->setSniffProperty(
14-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
15-
'allowUndefinedVariablesInFileScope',
16-
'false'
17-
);
13+
$this->setSniffProperty($phpcsFile, 'allowUndefinedVariablesInFileScope', 'false');
1814
$phpcsFile->process();
1915
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
2016
$expectedErrors = [
@@ -31,11 +27,7 @@ public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope()
3127
{
3228
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
3329
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
34-
$phpcsFile->ruleset->setSniffProperty(
35-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
36-
'allowUndefinedVariablesInFileScope',
37-
'true'
38-
);
30+
$this->setSniffProperty($phpcsFile, 'allowUndefinedVariablesInFileScope', 'true');
3931
$phpcsFile->process();
4032
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
4133
$expectedErrors = [
@@ -51,11 +43,7 @@ public function testGlobalScopeWarningsWithAllowUnusedVariablesInFileScope()
5143
{
5244
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
5345
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
54-
$phpcsFile->ruleset->setSniffProperty(
55-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
56-
'allowUnusedVariablesInFileScope',
57-
'true'
58-
);
46+
$this->setSniffProperty($phpcsFile, 'allowUnusedVariablesInFileScope', 'true');
5947
$phpcsFile->process();
6048
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
6149
$expectedErrors = [

Tests/VariableAnalysisSniff/IfConditionTest.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ public function testIfConditionWarnings()
1010
{
1111
$fixtureFile = $this->getFixture('FunctionWithIfConditionFixture.php');
1212
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
13-
$phpcsFile->ruleset->setSniffProperty(
14-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
15-
'allowUnusedParametersBeforeUsed',
16-
'true'
17-
);
13+
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
1814
$phpcsFile->process();
1915
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
2016
$expectedWarnings = [
@@ -43,16 +39,8 @@ public function testIfConditionWarningsWithValidUndefinedVariableNames()
4339
{
4440
$fixtureFile = $this->getFixture('FunctionWithIfConditionFixture.php');
4541
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
46-
$phpcsFile->ruleset->setSniffProperty(
47-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
48-
'validUndefinedVariableNames',
49-
'second'
50-
);
51-
$phpcsFile->ruleset->setSniffProperty(
52-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
53-
'allowUnusedParametersBeforeUsed',
54-
'true'
55-
);
42+
$this->setSniffProperty($phpcsFile, 'validUndefinedVariableNames', 'second');
43+
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
5644
$phpcsFile->process();
5745
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
5846
$expectedWarnings = [
@@ -75,11 +63,7 @@ public function testInlineIfConditionWarnings()
7563
{
7664
$fixtureFile = $this->getFixture('FunctionWithInlineIfConditionFixture.php');
7765
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
78-
$phpcsFile->ruleset->setSniffProperty(
79-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
80-
'allowUnusedParametersBeforeUsed',
81-
'true'
82-
);
66+
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
8367
$phpcsFile->process();
8468
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
8569
$expectedWarnings = [
@@ -110,16 +94,8 @@ public function testInlineIfConditionWarningsWithValidUndefinedVariableNames()
11094
{
11195
$fixtureFile = $this->getFixture('FunctionWithInlineIfConditionFixture.php');
11296
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
113-
$phpcsFile->ruleset->setSniffProperty(
114-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
115-
'validUndefinedVariableNames',
116-
'second'
117-
);
118-
$phpcsFile->ruleset->setSniffProperty(
119-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
120-
'allowUnusedParametersBeforeUsed',
121-
'true'
122-
);
97+
$this->setSniffProperty($phpcsFile, 'validUndefinedVariableNames', 'second');
98+
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
12399
$phpcsFile->process();
124100
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
125101
$expectedWarnings = [

Tests/VariableAnalysisSniff/UnusedFollowedByRequireTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ public function testUnusedFollowedByRequireDoesNotWarnWhenSet()
3333
{
3434
$fixtureFile = $this->getFixture('UnusedFollowedByRequireFixture.php');
3535
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
36-
$phpcsFile->ruleset->setSniffProperty(
37-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
38-
'allowUnusedVariablesBeforeRequire',
39-
'true'
40-
);
36+
$this->setSniffProperty($phpcsFile, 'allowUnusedVariablesBeforeRequire', 'true');
4137
$phpcsFile->process();
4238
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
4339
$expectedWarnings = [
@@ -53,11 +49,7 @@ public function testUnusedFollowedByRequireDoesNotBreakOtherThingsWhenSet()
5349
{
5450
$fixtureFile = $this->getFixture('FunctionWithoutParamFixture.php');
5551
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
56-
$phpcsFile->ruleset->setSniffProperty(
57-
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
58-
'allowUnusedVariablesBeforeRequire',
59-
'true'
60-
);
52+
$this->setSniffProperty($phpcsFile, 'allowUnusedVariablesBeforeRequire', 'true');
6153
$phpcsFile->process();
6254
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
6355
$expectedWarnings = [

0 commit comments

Comments
 (0)