Skip to content

Commit a7acd32

Browse files
authored
Merge pull request #17 from sirbrillig/add/test-for-allowed-unused-import
Clarify ignoreUnimportedSymbols works for unused imports
2 parents bb4e927 + 41c45b4 commit a7acd32

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ You can ignore certain patterns by using the `ignoreUnimportedSymbols` config op
106106
</ruleset>
107107
```
108108

109+
Despite the name, you can also use the `ignoreUnimportedSymbols` pattern to ignore specific unused imports.
110+
109111
## Usage
110112

111113
Most editors have a phpcs plugin available, but you can also run phpcs manually. To run phpcs on a file in your project, just use the command-line as follows (the `-s` causes the sniff code to be shown, which is very important for learning about an error).

tests/Sniffs/Imports/RequireImportsAllowedPatternFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace CAR_APP\Vehicles\Greatness;
55

66
use function whitelisted_function;
7+
use function unused_function;
78

89
class GreatClass {
910
private function activate() {

tests/Sniffs/Imports/RequireImportsSniffTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ public function testRequireImportsSniffFindsUnimportedFunctionsWithNoConfig() {
5656
$phpcsFile->process();
5757
$lines = $helper->getWarningLineNumbersFromFile($phpcsFile);
5858
$expectedLines = [
59-
11,
59+
7,
6060
12,
61+
13,
6162
];
6263
$this->assertEquals($expectedLines, $lines);
6364
}
6465

65-
public function testRequireImportsSniffIgnoresAllowedImports() {
66+
public function testRequireImportsSniffIgnoresWhitelistedUnimportedSymbols() {
6667
$fixtureFile = __DIR__ . '/RequireImportsAllowedPatternFixture.php';
6768
$sniffFile = __DIR__ . '/../../../ImportDetection/Sniffs/Imports/RequireImportsSniff.php';
6869
$helper = new SniffTestHelper();
@@ -74,7 +75,23 @@ public function testRequireImportsSniffIgnoresAllowedImports() {
7475
);
7576
$phpcsFile->process();
7677
$lines = $helper->getWarningLineNumbersFromFile($phpcsFile);
77-
$expectedLines = [ 12 ];
78+
$expectedLines = [ 7, 13 ];
79+
$this->assertEquals($expectedLines, $lines);
80+
}
81+
82+
public function testRequireImportsSniffIgnoresWhitelistedUnusedImports() {
83+
$fixtureFile = __DIR__ . '/RequireImportsAllowedPatternFixture.php';
84+
$sniffFile = __DIR__ . '/../../../ImportDetection/Sniffs/Imports/RequireImportsSniff.php';
85+
$helper = new SniffTestHelper();
86+
$phpcsFile = $helper->prepareLocalFileForSniffs($sniffFile, $fixtureFile);
87+
$phpcsFile->ruleset->setSniffProperty(
88+
'ImportDetection\Sniffs\Imports\RequireImportsSniff',
89+
'ignoreUnimportedSymbols',
90+
'/^(unused_function)$/'
91+
);
92+
$phpcsFile->process();
93+
$lines = $helper->getWarningLineNumbersFromFile($phpcsFile);
94+
$expectedLines = [ 12, 13 ];
7895
$this->assertEquals($expectedLines, $lines);
7996
}
8097

0 commit comments

Comments
 (0)