@@ -75,48 +75,40 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
75
75
76
76
return ;
77
77
}
78
- } else if ($ tokens [$ calledClassName ]['code ' ] === T_STRING ) {
79
- // If the class is called with a namespace prefix, build fully qualified
80
- // namespace calls for both current scope class and requested class.
81
- $ prevNonEmpty = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ calledClassName - 1 ), null , true );
82
- if ($ prevNonEmpty !== false && $ tokens [$ prevNonEmpty ]['code ' ] === T_NS_SEPARATOR ) {
83
- $ declarationName = $ this ->getDeclarationNameWithNamespace ($ tokens , $ calledClassName );
84
- $ declarationName = ltrim ($ declarationName , '\\' );
85
- $ fullQualifiedClassName = $ this ->getNamespaceOfScope ($ phpcsFile , $ currScope );
86
- if ($ fullQualifiedClassName === '\\' ) {
87
- $ fullQualifiedClassName = '' ;
88
- } else {
89
- $ fullQualifiedClassName .= '\\' ;
90
- }
78
+ } else if (isset (Tokens::$ nameTokens [$ tokens [$ calledClassName ]['code ' ]]) === true ) {
79
+ // Work out the fully qualified name for both the class declaration
80
+ // as well as the class usage to see if they match.
81
+ $ namespaceName = $ this ->getNamespaceName ($ phpcsFile , $ currScope );
82
+ if ($ namespaceName === '\\' ) {
83
+ $ namespaceName = '' ;
84
+ }
85
+
86
+ $ declarationName = $ namespaceName .'\\' .$ phpcsFile ->getDeclarationName ($ currScope );
87
+ $ inlineName = '' ;
88
+
89
+ switch ($ tokens [$ calledClassName ]['code ' ]) {
90
+ case T_NAME_FULLY_QUALIFIED :
91
+ $ inlineName = $ tokens [$ calledClassName ]['content ' ];
92
+ break ;
93
+
94
+ case T_NAME_QUALIFIED :
95
+ case T_STRING :
96
+ $ inlineName = $ namespaceName .'\\' .$ tokens [$ calledClassName ]['content ' ];
97
+ break ;
91
98
92
- $ fullQualifiedClassName .= $ phpcsFile ->getDeclarationName ($ currScope );
93
- } else {
94
- $ declarationName = $ phpcsFile ->getDeclarationName ($ currScope );
95
- $ fullQualifiedClassName = $ tokens [$ calledClassName ]['content ' ];
99
+ case T_NAME_RELATIVE :
100
+ $ inlineName = $ namespaceName .substr ($ tokens [$ calledClassName ]['content ' ], 9 );
101
+ break ;
96
102
}
97
103
98
- if ($ declarationName === $ fullQualifiedClassName ) {
104
+ if ($ declarationName === $ inlineName ) {
99
105
// Class name is the same as the current class, which is not allowed.
100
106
$ error = 'Must use "self::" for local static member reference ' ;
101
107
$ fix = $ phpcsFile ->addFixableError ($ error , $ calledClassName , 'NotUsed ' );
102
108
103
109
if ($ fix === true ) {
104
110
$ phpcsFile ->fixer ->beginChangeset ();
105
-
106
- $ currentPointer = ($ stackPtr - 1 );
107
- while ($ tokens [$ currentPointer ]['code ' ] === T_NS_SEPARATOR
108
- || $ tokens [$ currentPointer ]['code ' ] === T_STRING
109
- || isset (Tokens::$ emptyTokens [$ tokens [$ currentPointer ]['code ' ]]) === true
110
- ) {
111
- if (isset (Tokens::$ emptyTokens [$ tokens [$ currentPointer ]['code ' ]]) === true ) {
112
- --$ currentPointer ;
113
- continue ;
114
- }
115
-
116
- $ phpcsFile ->fixer ->replaceToken ($ currentPointer , '' );
117
- --$ currentPointer ;
118
- }
119
-
111
+ $ phpcsFile ->fixer ->replaceToken ($ calledClassName , '' );
120
112
$ phpcsFile ->fixer ->replaceToken ($ stackPtr , 'self:: ' );
121
113
$ phpcsFile ->fixer ->endChangeset ();
122
114
@@ -180,69 +172,33 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
180
172
181
173
182
174
/**
183
- * Returns the declaration names for classes/interfaces/functions with a namespace.
184
- *
185
- * @param array $tokens Token stack for this file.
186
- * @param int $stackPtr The position where the namespace building will start.
187
- *
188
- * @return string
189
- */
190
- protected function getDeclarationNameWithNamespace (array $ tokens , $ stackPtr )
191
- {
192
- $ nameParts = [];
193
- $ currentPointer = $ stackPtr ;
194
- while ($ tokens [$ currentPointer ]['code ' ] === T_NS_SEPARATOR
195
- || $ tokens [$ currentPointer ]['code ' ] === T_STRING
196
- || isset (Tokens::$ emptyTokens [$ tokens [$ currentPointer ]['code ' ]]) === true
197
- ) {
198
- if (isset (Tokens::$ emptyTokens [$ tokens [$ currentPointer ]['code ' ]]) === true ) {
199
- --$ currentPointer ;
200
- continue ;
201
- }
202
-
203
- $ nameParts [] = $ tokens [$ currentPointer ]['content ' ];
204
- --$ currentPointer ;
205
- }
206
-
207
- $ nameParts = array_reverse ($ nameParts );
208
- return implode ('' , $ nameParts );
209
-
210
- }//end getDeclarationNameWithNamespace()
211
-
212
-
213
- /**
214
- * Returns the namespace declaration of a file.
175
+ * Returns the namespace name of the current scope.
215
176
*
216
177
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
217
178
* @param int $stackPtr The position where the search for the
218
179
* namespace declaration will start.
219
180
*
220
181
* @return string
221
182
*/
222
- protected function getNamespaceOfScope (File $ phpcsFile , $ stackPtr )
183
+ protected function getNamespaceName (File $ phpcsFile , $ stackPtr )
223
184
{
224
- $ namespace = '\\' ;
225
- $ tokens = $ phpcsFile ->getTokens ( );
185
+ $ namespace = '\\' ;
186
+ $ namespaceDeclaration = $ phpcsFile ->findPrevious ( T_NAMESPACE , $ stackPtr );
226
187
227
- while (($ namespaceDeclaration = $ phpcsFile ->findPrevious (T_NAMESPACE , $ stackPtr )) !== false ) {
188
+ if ($ namespaceDeclaration !== false ) {
189
+ $ tokens = $ phpcsFile ->getTokens ();
228
190
$ nextNonEmpty = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ namespaceDeclaration + 1 ), null , true );
229
- if ($ tokens [$ nextNonEmpty ]['code ' ] === T_NS_SEPARATOR ) {
230
- // Namespace operator. Ignore.
231
- $ stackPtr = ($ namespaceDeclaration - 1 );
232
- continue ;
191
+ if ($ nextNonEmpty !== false
192
+ && ($ tokens [$ nextNonEmpty ]['code ' ] === T_NAME_QUALIFIED
193
+ || $ tokens [$ nextNonEmpty ]['code ' ] === T_STRING )
194
+ ) {
195
+ $ namespace .= $ tokens [$ nextNonEmpty ]['content ' ];
233
196
}
234
-
235
- $ endOfNamespaceDeclaration = $ phpcsFile ->findNext ([T_SEMICOLON , T_OPEN_CURLY_BRACKET , T_CLOSE_TAG ], $ namespaceDeclaration );
236
- $ namespace = $ this ->getDeclarationNameWithNamespace (
237
- $ phpcsFile ->getTokens (),
238
- ($ endOfNamespaceDeclaration - 1 )
239
- );
240
- break ;
241
197
}
242
198
243
199
return $ namespace ;
244
200
245
- }//end getNamespaceOfScope ()
201
+ }//end getNamespaceName ()
246
202
247
203
248
204
}//end class
0 commit comments