33 * Copyright © Magento. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace PHP_CodeSniffer \Tokenizers ;
78
89use GraphQL \Language \Lexer ;
@@ -67,29 +68,7 @@ public function processAdditional()
6768 {
6869 $ this ->logVerbose ('*** START ADDITIONAL GRAPHQL PROCESSING *** ' );
6970
70- $ processingEntity = false ;
71- $ numTokens = count ($ this ->tokens );
72- $ entityTypes = [T_CLASS , T_INTERFACE ];
73-
74- for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
75- $ tokenCode = $ this ->tokens [$ i ]['code ' ];
76-
77- //have we found a new entity or its end?
78- if (in_array ($ tokenCode , $ entityTypes ) && $ this ->tokens [$ i ]['content ' ] !== 'enum ' ) {
79- $ processingEntity = true ;
80- continue ;
81- } elseif ($ tokenCode === T_CLOSE_CURLY_BRACKET ) {
82- $ processingEntity = false ;
83- continue ;
84- }
85-
86- //if we are processing an entity, are we currently seeing a field?
87- if ($ processingEntity && $ this ->isFieldToken ($ i )) {
88- $ this ->tokens [$ i ]['code ' ] = T_VARIABLE ;
89- $ this ->tokens [$ i ]['type ' ] = 'T_VARIABLE ' ;
90- continue ;
91- }
92- }
71+ $ this ->processFields ();
9372
9473 $ this ->logVerbose ('*** END ADDITIONAL GRAPHQL PROCESSING *** ' );
9574 }
@@ -203,18 +182,18 @@ private function isFieldToken($stackPointer)
203182 //if next token is an opening parenthesis, we seek for the closing parenthesis
204183 if ($ nextToken ['code ' ] === T_OPEN_PARENTHESIS ) {
205184 $ nextPointer = $ stackPointer + 1 ;
206- $ numTokens = count ($ this ->tokens );
185+ $ numTokens = count ($ this ->tokens );
207186
208- for ($ i= $ nextPointer ; $ i< $ numTokens ; ++$ i ) {
187+ for ($ i = $ nextPointer ; $ i < $ numTokens ; ++$ i ) {
209188 if ($ this ->tokens [$ i ]['code ' ] === T_CLOSE_PARENTHESIS ) {
210189 $ nextToken = $ this ->tokens [$ i + 1 ];
211190 break ;
212191 }
213192 }
214193 }
215194
216- //return whether next token is a colon
217- return $ nextToken ['code ' ] === T_COLON ;
195+ //return whether current token is a string and next token is a colon
196+ return $ this -> tokens [ $ stackPointer ][ ' code ' ] === T_STRING && $ nextToken ['code ' ] === T_COLON ;
218197 }
219198
220199 /**
@@ -229,4 +208,50 @@ private function logVerbose($message, $level = 1)
229208 printf ("\t%s " . PHP_EOL , $ message );
230209 }
231210 }
211+
212+ /**
213+ * Processes field tokens, setting their type to {@link T_VARIABLE}.
214+ */
215+ private function processFields ()
216+ {
217+ $ processingArgumentList = false ;
218+ $ processingEntity = false ;
219+ $ numTokens = count ($ this ->tokens );
220+ $ entityTypes = [T_CLASS , T_INTERFACE ];
221+ $ skipTypes = [T_COMMENT , T_WHITESPACE ];
222+
223+ for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
224+ $ tokenCode = $ this ->tokens [$ i ]['code ' ];
225+
226+ //process current token
227+ switch (true ) {
228+ case in_array ($ tokenCode , $ skipTypes ):
229+ //we have hit a token that needs to be skipped -> NOP
230+ break ;
231+ case in_array ($ tokenCode , $ entityTypes ) && $ this ->tokens [$ i ]['content ' ] !== 'enum ' :
232+ //we have hit an entity declaration
233+ $ processingEntity = true ;
234+ break ;
235+ case $ tokenCode === T_CLOSE_CURLY_BRACKET :
236+ //we have hit the end of an entity declaration
237+ $ processingEntity = false ;
238+ break ;
239+ case $ tokenCode === T_OPEN_PARENTHESIS :
240+ //we have hit an argument list
241+ $ processingArgumentList = true ;
242+ break ;
243+ case $ tokenCode === T_CLOSE_PARENTHESIS :
244+ //we have hit an argument list end
245+ $ processingArgumentList = false ;
246+ break ;
247+ case $ processingEntity && !$ processingArgumentList && $ this ->isFieldToken ($ i ):
248+ //we have hit a field
249+ $ this ->tokens [$ i ]['code ' ] = T_VARIABLE ;
250+ $ this ->tokens [$ i ]['type ' ] = 'T_VARIABLE ' ;
251+ break ;
252+ default :
253+ //NOP All operations have already been executed
254+ }
255+ }
256+ }
232257}
0 commit comments