Skip to content

Commit 8856f8b

Browse files
committed
UseStatements/KeywordSpacing: add missing "fixed" test file
The `Universal.UseStatement.KeywordSpacing` sniff has a difference in behaviour between PHP < 8.0 and PHP 8.0+ due to the change in how namespaced name tokens are tokenized in PHP 8.0+. For that reason, the `KeywordSpacingUnitTest.2.inc` did not have a ".fixed" file as whether or not a fix would be made depends on which PHP version the tests are being run on. As of PHPCS 3.9.0, PHPCS will throw a PHPUnit warning for missing "fixed" files and as of PHPCS 4.0.0, this will become an error. With that in mind, this commit adds the missing "fixed" file, but excludes that test from being run on PHP 8.0+ by overloading the `getTestFiles()` method and selectively removing the test case file from the list of files to test.
1 parent 11d387c commit 8856f8b

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/*
4+
* The behaviour of the sniff for this code sample will be different depending on the PHP version.
5+
* For PHP < 8.0, it will correctly flag this as "no space" after the `use` keyword.
6+
* For PHP 8.0+, this will no longer be flagged as keywords are allowed in namespaced names,
7+
* so the `use` is tokenized as `T_STRING` instead of `T_USE` and won't be flagged.
8+
*
9+
* For this reason, there is no "fixed" file included as the test would not be able to pass on PHP 8.0+.
10+
*/
11+
use \Util\MyOtherClass;

Universal/Tests/UseStatements/KeywordSpacingUnitTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@
2222
final class KeywordSpacingUnitTest extends AbstractSniffUnitTest
2323
{
2424

25+
/**
26+
* Get a list of all test files to check.
27+
*
28+
* @param string $testFileBase The base path that the unit tests files will have.
29+
*
30+
* @return array<string>
31+
*/
32+
protected function getTestFiles($testFileBase)
33+
{
34+
$testFiles = parent::getTestFiles($testFileBase);
35+
36+
if (\PHP_VERSION_ID < 80000) {
37+
return $testFiles;
38+
}
39+
40+
// The issue being tested in the "2" test case file cannot be flagged/fixed on PHP 8.0+.
41+
$target = 'KeywordSpacingUnitTest.2.inc';
42+
$length = \strlen($target);
43+
foreach ($testFiles as $i => $fileName) {
44+
if (\substr($fileName, -$length) === $target) {
45+
unset($testFiles[$i]);
46+
break;
47+
}
48+
}
49+
50+
return $testFiles;
51+
}
52+
2553
/**
2654
* Returns the lines where errors should occur.
2755
*
@@ -55,10 +83,6 @@ public function getErrorList($testFile = '')
5583
];
5684

5785
case 'KeywordSpacingUnitTest.2.inc':
58-
if (\PHP_VERSION_ID >= 80000) {
59-
return [];
60-
}
61-
6286
return [
6387
11 => 1,
6488
];

0 commit comments

Comments
 (0)