1111
1212use PHP_CodeSniffer \Files \File ;
1313use PHP_CodeSniffer \Sniffs \Sniff ;
14+ use PHP_CodeSniffer \Util \Tokens ;
1415
1516/**
1617 * Checks that class references do not use FQN but use statements.
@@ -30,7 +31,10 @@ class FullyQualifiedNamespaceSniff implements Sniff
3031 */
3132 public function register ()
3233 {
33- return [T_NS_SEPARATOR ];
34+ return [
35+ T_NAME_FULLY_QUALIFIED ,
36+ T_NAME_QUALIFIED ,
37+ ];
3438
3539 }//end register()
3640
@@ -61,7 +65,9 @@ public function process(File $phpcsFile, $stackPtr)
6165
6266 // We are only interested in a backslash embedded between strings, which
6367 // means this is a class reference with more than one namespace part.
64- if ($ tokens [($ stackPtr - 1 )]['code ' ] !== T_STRING || $ tokens [($ stackPtr + 1 )]['code ' ] !== T_STRING ) {
68+ if (substr_count ($ tokens [$ stackPtr ]['content ' ], '\\' ) === 1
69+ && strpos ($ tokens [$ stackPtr ]['content ' ], '\\' ) === 0
70+ ) {
6571 return ;
6672 }
6773
@@ -71,25 +77,12 @@ public function process(File $phpcsFile, $stackPtr)
7177 }
7278
7379 // Check if this is a use statement and ignore those.
74- $ before = $ phpcsFile ->findPrevious ([ T_STRING , T_NS_SEPARATOR , T_WHITESPACE , T_COMMA , T_AS ], $ stackPtr , null , true );
80+ $ before = $ phpcsFile ->findPrevious ((Tokens:: EMPTY_TOKENS + Tokens:: NAME_TOKENS + [ T_COMMA => T_COMMA , T_AS => T_AS ]), ( $ stackPtr - 1 ) , null , true );
7581 if ($ tokens [$ before ]['code ' ] === T_USE || $ tokens [$ before ]['code ' ] === T_NAMESPACE ) {
76- return $ phpcsFile ->findNext ([T_STRING , T_NS_SEPARATOR , T_WHITESPACE , T_COMMA , T_AS ], ($ stackPtr + 1 ), null , true );
77- } else {
78- $ before = $ phpcsFile ->findPrevious ([T_STRING , T_NS_SEPARATOR , T_WHITESPACE ], $ stackPtr , null , true );
79- }
80-
81- // If this is a namespaced function call then ignore this because use
82- // statements for functions are not possible in PHP 5.5 and lower.
83- $ after = $ phpcsFile ->findNext ([T_STRING , T_NS_SEPARATOR , T_WHITESPACE ], $ stackPtr , null , true );
84- if ($ tokens [$ after ]['code ' ] === T_OPEN_PARENTHESIS
85- && $ tokens [$ before ]['code ' ] !== T_NEW
86- && $ tokens [$ before ]['code ' ] !== T_ATTRIBUTE
87- ) {
88- return ($ after + 1 );
82+ return ;
8983 }
9084
91- $ fullName = $ phpcsFile ->getTokensAsString (($ before + 1 ), ($ after - 1 - $ before ));
92- $ fullName = trim ($ fullName , "\ \n" );
85+ $ fullName = trim ($ tokens [$ stackPtr ]['content ' ], '\\ ' );
9386 $ parts = explode ('\\' , $ fullName );
9487 $ className = end ($ parts );
9588
@@ -101,7 +94,7 @@ public function process(File $phpcsFile, $stackPtr)
10194 $ useStatement = $ phpcsFile ->findNext (T_USE , 0 );
10295 while ($ useStatement !== false && empty ($ tokens [$ useStatement ]['conditions ' ]) === true ) {
10396 $ endPtr = $ phpcsFile ->findEndOfStatement ($ useStatement );
104- $ useEnd = ($ phpcsFile ->findNext ([ T_STRING , T_NS_SEPARATOR , T_WHITESPACE ] , ($ useStatement + 1 ), null , true ) - 1 );
97+ $ useEnd = ($ phpcsFile ->findNext ((Tokens:: EMPTY_TOKENS + Tokens:: NAME_TOKENS ) , ($ useStatement + 1 ), null , true ) - 1 );
10598 $ useFullName = trim ($ phpcsFile ->getTokensAsString (($ useStatement + 1 ), ($ useEnd - $ useStatement )));
10699
107100 // Check if use statement contains an alias.
@@ -163,18 +156,11 @@ public function process(File $phpcsFile, $stackPtr)
163156 if ($ fix === true ) {
164157 $ phpcsFile ->fixer ->beginChangeset ();
165158
166- // Replace the fully qualified name with the local name.
167- for ($ i = ($ before + 1 ); $ i < $ after ; $ i ++) {
168- if ($ tokens [$ i ]['code ' ] !== T_WHITESPACE ) {
169- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
170- }
171- }
172-
173159 // Use alias name if available.
174160 if ($ aliasName !== false ) {
175- $ phpcsFile ->fixer ->addContentBefore (( $ after - 1 ) , $ aliasName );
161+ $ phpcsFile ->fixer ->replaceToken ( $ stackPtr , $ aliasName );
176162 } else {
177- $ phpcsFile ->fixer ->addContentBefore (( $ after - 1 ) , $ className );
163+ $ phpcsFile ->fixer ->replaceToken ( $ stackPtr , $ className );
178164 }
179165
180166 // Insert use statement at the beginning of the file if it is not there
@@ -216,10 +202,6 @@ public function process(File $phpcsFile, $stackPtr)
216202 $ phpcsFile ->fixer ->endChangeset ();
217203 }//end if
218204
219- // Continue after this class reference so that errors for this are not
220- // flagged multiple times.
221- return $ phpcsFile ->findNext ([T_STRING , T_NS_SEPARATOR ], ($ stackPtr + 1 ), null , true );
222-
223205 }//end process()
224206
225207
0 commit comments