@@ -26,60 +26,81 @@ public function process(File $phpcsFile, $stackPtr) {
2626 $ tokens = $ phpcsFile ->getTokens ();
2727 $ token = $ tokens [$ stackPtr ];
2828 if ($ token ['type ' ] === 'T_WHITESPACE ' ) {
29+ $ this ->debug ('found whitespace ' );
2930 return $ this ->processEndOfFile ($ phpcsFile , $ stackPtr );
3031 }
3132 if ($ token ['type ' ] === 'T_USE ' ) {
33+ $ this ->debug ('found import ' );
3234 return $ this ->processUse ($ phpcsFile , $ stackPtr );
3335 }
3436 $ symbol = $ helper ->getFullSymbol ($ phpcsFile , $ stackPtr );
3537 // If the symbol has been seen before (if this is a duplicate), ignore it
3638 if (in_array ($ symbol , $ this ->seenSymbols )) {
39+ $ this ->debug ('found duplicate symbol ' . $ symbol ->getName ());
3740 return ;
3841 }
3942 $ this ->seenSymbols [] = $ symbol ;
4043 // If the symbol is in the ignore list, ignore it
4144 if ($ this ->isSymbolIgnored ($ symbol )) {
45+ $ this ->debug ('found ignored symbol ' . $ symbol ->getName ());
4246 $ this ->markSymbolUsed ($ symbol );
4347 return ;
4448 }
4549 // If the symbol is a fully-qualified namespace, ignore it
4650 if ($ symbol ->isAbsoluteNamespace ()) {
51+ $ this ->debug ('found absolute namespaced symbol ' . $ symbol ->getName ());
4752 return ;
4853 }
4954 // If this symbol is a definition, ignore it
5055 if ($ helper ->isSymbolADefinition ($ phpcsFile , $ symbol )) {
56+ $ this ->debug ('found definition symbol ' . $ symbol ->getName ());
5157 return ;
5258 }
5359 // If this symbol is a static reference or an object reference, ignore it
5460 if ($ helper ->isStaticReference ($ phpcsFile , $ stackPtr ) || $ helper ->isObjectReference ($ phpcsFile , $ stackPtr )) {
61+ $ this ->debug ('found static symbol ' . $ symbol ->getName ());
5562 return ;
5663 }
5764 // If this symbol is a namespace definition, ignore it
5865 if ($ helper ->isWithinNamespaceStatement ($ phpcsFile , $ symbol ->getSymbolPosition ())) {
66+ $ this ->debug ('found namespace definition symbol ' . $ symbol ->getName ());
5967 return ;
6068 }
6169 // If this symbol is an import, ignore it
62- if ($ helper ->isWithinUseStatement ($ phpcsFile , $ symbol ->getSymbolPosition ())) {
70+ if ($ helper ->isWithinImportStatement ($ phpcsFile , $ symbol ->getSymbolPosition ())) {
71+ $ this ->debug ('found symbol inside an import ' . $ symbol ->getName ());
6372 return ;
6473 }
6574 // If the symbol is predefined, ignore it
6675 if ($ helper ->isPredefinedConstant ($ phpcsFile , $ stackPtr ) || $ helper ->isBuiltInFunction ($ phpcsFile , $ stackPtr )) {
76+ $ this ->debug ('found predefined symbol ' . $ symbol ->getName ());
6777 return ;
6878 }
6979 // If this symbol is a predefined typehint, ignore it
7080 if ($ helper ->isPredefinedTypehint ($ phpcsFile , $ stackPtr )) {
81+ $ this ->debug ('found typehint symbol ' . $ symbol ->getName ());
7182 return ;
7283 }
7384 // If the symbol's namespace is imported or defined, ignore it
7485 // If the symbol has no namespace and is itself is imported or defined, ignore it
7586 if ($ this ->isSymbolDefined ($ phpcsFile , $ symbol )) {
87+ $ this ->debug ('found defined symbol ' . $ symbol ->getName ());
7688 $ this ->markSymbolUsed ($ symbol );
7789 return ;
7890 }
7991 $ error = "Found unimported symbol ' {$ symbol ->getName ()}'. " ;
8092 $ phpcsFile ->addWarning ($ error , $ stackPtr , 'Symbol ' );
8193 }
8294
95+ private function debug (string $ message ) {
96+ if (! defined ('PHP_CODESNIFFER_VERBOSITY ' )) {
97+ return ;
98+ }
99+ if (PHP_CODESNIFFER_VERBOSITY > 3 ) {
100+ echo PHP_EOL . "RequireImportsSniff: DEBUG: $ message " . PHP_EOL ;
101+ }
102+ }
103+
83104 private function isSymbolIgnored (Symbol $ symbol ): bool {
84105 $ symbolName = $ symbol ->getName ();
85106 $ pattern = $ this ->getIgnoredSymbolPattern ();
0 commit comments