55use ImportDetection \Symbol ;
66use ImportDetection \ImportedSymbol ;
77use ImportDetection \SniffHelpers ;
8+ use ImportDetection \FileSymbolRecord ;
89use PHP_CodeSniffer \Sniffs \Sniff ;
910use PHP_CodeSniffer \Files \File ;
1011
1112class RequireImportsSniff implements Sniff {
1213 public $ ignoreUnimportedSymbols = null ;
1314
14- private $ importedFunctions = [];
15- private $ importedConsts = [];
16- private $ importedClasses = [];
17- private $ importedSymbolRecords = [];
18- private $ seenSymbols = [];
15+ private $ symbolRecordsByFile = [];
1916
2017 public function register () {
2118 return [T_USE , T_STRING , T_RETURN_TYPE , T_WHITESPACE ];
@@ -25,6 +22,7 @@ public function process(File $phpcsFile, $stackPtr) {
2522 $ helper = new SniffHelpers ();
2623 $ tokens = $ phpcsFile ->getTokens ();
2724 $ token = $ tokens [$ stackPtr ];
25+ $ this ->symbolRecordsByFile [$ phpcsFile ->path ] = $ this ->symbolRecordsByFile [$ phpcsFile ->path ] ?? new FileSymbolRecord ;
2826 if ($ token ['type ' ] === 'T_WHITESPACE ' ) {
2927 $ this ->debug ('found whitespace ' );
3028 return $ this ->processEndOfFile ($ phpcsFile , $ stackPtr );
@@ -35,15 +33,15 @@ public function process(File $phpcsFile, $stackPtr) {
3533 }
3634 $ symbol = $ helper ->getFullSymbol ($ phpcsFile , $ stackPtr );
3735 // If the symbol has been seen before (if this is a duplicate), ignore it
38- if (in_array ($ symbol , $ this ->seenSymbols )) {
36+ if (in_array ($ symbol , $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> seenSymbols )) {
3937 $ this ->debug ('found duplicate symbol ' . $ symbol ->getName ());
4038 return ;
4139 }
42- $ this ->seenSymbols [] = $ symbol ;
40+ $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> seenSymbols [] = $ symbol ;
4341 // If the symbol is in the ignore list, ignore it
4442 if ($ this ->isSymbolIgnored ($ symbol )) {
4543 $ this ->debug ('found ignored symbol ' . $ symbol ->getName ());
46- $ this ->markSymbolUsed ($ symbol );
44+ $ this ->markSymbolUsed ($ phpcsFile , $ symbol );
4745 return ;
4846 }
4947 // If the symbol is a fully-qualified namespace, ignore it
@@ -85,7 +83,7 @@ public function process(File $phpcsFile, $stackPtr) {
8583 // If the symbol has no namespace and is itself is imported or defined, ignore it
8684 if ($ this ->isSymbolDefined ($ phpcsFile , $ symbol )) {
8785 $ this ->debug ('found defined symbol ' . $ symbol ->getName ());
88- $ this ->markSymbolUsed ($ symbol );
86+ $ this ->markSymbolUsed ($ phpcsFile , $ symbol );
8987 return ;
9088 }
9189 $ error = "Found unimported symbol ' {$ symbol ->getName ()}'. " ;
@@ -130,11 +128,11 @@ private function isSymbolDefined(File $phpcsFile, Symbol $symbol): bool {
130128
131129 private function isNamespaceImportedOrDefined (File $ phpcsFile , string $ namespace ): bool {
132130 return (
133- $ this ->isClassImported ($ namespace )
131+ $ this ->isClassImported ($ phpcsFile , $ namespace )
134132 || $ this ->isClassDefined ($ phpcsFile , $ namespace )
135- || $ this ->isFunctionImported ($ namespace )
133+ || $ this ->isFunctionImported ($ phpcsFile , $ namespace )
136134 || $ this ->isFunctionDefined ($ phpcsFile , $ namespace )
137- || $ this ->isConstImported ($ namespace )
135+ || $ this ->isConstImported ($ phpcsFile , $ namespace )
138136 || $ this ->isConstDefined ($ phpcsFile , $ namespace )
139137 );
140138 }
@@ -152,43 +150,43 @@ private function processUse(File $phpcsFile, $stackPtr) {
152150 }
153151 }
154152
155- private function recordImportedSymbols (int $ stackPtr , array $ importNames ) {
153+ private function recordImportedSymbols (File $ phpcsFile , int $ stackPtr , array $ importNames ) {
156154 foreach ($ importNames as $ symbol ) {
157- $ this ->importedSymbolRecords [] = new ImportedSymbol ($ stackPtr , $ symbol );
155+ $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> importedSymbolRecords [] = new ImportedSymbol ($ stackPtr , $ symbol );
158156 }
159157 }
160158
161159 private function saveFunctionImport (File $ phpcsFile , $ stackPtr ) {
162160 $ helper = new SniffHelpers ();
163161 $ importNames = $ helper ->getImportNames ($ phpcsFile , $ stackPtr );
164- $ this ->recordImportedSymbols ($ stackPtr , $ importNames );
165- $ this ->importedFunctions = array_merge ( $ this -> importedFunctions , $ importNames );
162+ $ this ->recordImportedSymbols ($ phpcsFile , $ stackPtr , $ importNames );
163+ $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> addImportedFunctions ( $ importNames );
166164 }
167165
168166 private function saveConstImport (File $ phpcsFile , $ stackPtr ) {
169167 $ helper = new SniffHelpers ();
170168 $ importNames = $ helper ->getImportNames ($ phpcsFile , $ stackPtr );
171- $ this ->recordImportedSymbols ($ stackPtr , $ importNames );
172- $ this ->importedConsts = array_merge ( $ this -> importedConsts , $ importNames );
169+ $ this ->recordImportedSymbols ($ phpcsFile , $ stackPtr , $ importNames );
170+ $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> addImportedConsts ( $ importNames );
173171 }
174172
175173 private function saveClassImport (File $ phpcsFile , $ stackPtr ) {
176174 $ helper = new SniffHelpers ();
177175 $ importNames = $ helper ->getImportNames ($ phpcsFile , $ stackPtr );
178- $ this ->recordImportedSymbols ($ stackPtr , $ importNames );
179- $ this ->importedClasses = array_merge ( $ this -> importedClasses , $ importNames );
176+ $ this ->recordImportedSymbols ($ phpcsFile , $ stackPtr , $ importNames );
177+ $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> addImportedClasses ( $ importNames );
180178 }
181179
182- private function isFunctionImported (string $ functionName ): bool {
183- return in_array ($ functionName , $ this ->importedFunctions );
180+ private function isFunctionImported (File $ phpcsFile , string $ functionName ): bool {
181+ return in_array ($ functionName , $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> importedFunctions );
184182 }
185183
186- private function isConstImported (string $ constName ): bool {
187- return in_array ($ constName , $ this ->importedConsts );
184+ private function isConstImported (File $ phpcsFile , string $ constName ): bool {
185+ return in_array ($ constName , $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> importedConsts );
188186 }
189187
190- private function isClassImported (string $ name ): bool {
191- return in_array ($ name , $ this ->importedClasses );
188+ private function isClassImported (File $ phpcsFile , string $ name ): bool {
189+ return in_array ($ name , $ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> importedClasses );
192190 }
193191
194192 private function isClassDefined (File $ phpcsFile , string $ className ): bool {
@@ -229,16 +227,16 @@ private function isConstDefined(File $phpcsFile, string $functionName): bool {
229227 return false ;
230228 }
231229
232- private function markSymbolUsed (Symbol $ symbol ) {
233- $ record = $ this ->getSymbolRecord ($ symbol );
230+ private function markSymbolUsed (File $ phpcsFile , Symbol $ symbol ) {
231+ $ record = $ this ->getSymbolRecord ($ phpcsFile , $ symbol );
234232 if (! $ record ) {
235233 return ;
236234 }
237235 $ record ->markUsed ();
238236 }
239237
240- private function getSymbolRecord (Symbol $ symbol ) {
241- foreach ($ this ->importedSymbolRecords as $ record ) {
238+ private function getSymbolRecord (File $ phpcsFile , Symbol $ symbol ) {
239+ foreach ($ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> importedSymbolRecords as $ record ) {
242240 if ($ record ->getName () === $ symbol ->getTopLevelNamespace ()) {
243241 return $ record ;
244242 }
@@ -253,7 +251,7 @@ private function processEndOfFile(File $phpcsFile, int $stackPtr) {
253251 return ;
254252 }
255253 // For each import, if the Symbol was not used, mark a warning
256- foreach ($ this ->importedSymbolRecords as $ record ) {
254+ foreach ($ this ->symbolRecordsByFile [ $ phpcsFile -> path ]-> importedSymbolRecords as $ record ) {
257255 if (! $ record ->isUsed ()) {
258256 $ error = "Found unused symbol ' {$ record ->getName ()}'. " ;
259257 $ phpcsFile ->addWarning ($ error , $ record ->getPtr (), 'Import ' );
0 commit comments