7
7
namespace PHP_CodeSniffer \Tokenizers ;
8
8
9
9
use GraphQL \Language \Lexer ;
10
- use GraphQL \Language \Parser ;
11
10
use GraphQL \Language \Source ;
12
11
use GraphQL \Language \Token ;
13
12
@@ -70,6 +69,7 @@ public function processAdditional()
70
69
{
71
70
$ this ->logVerbose ('*** START ADDITIONAL GRAPHQL PROCESSING *** ' );
72
71
72
+ $ this ->fixErroneousKeywordTokens ();
73
73
$ this ->processFields ();
74
74
75
75
$ this ->logVerbose ('*** END ADDITIONAL GRAPHQL PROCESSING *** ' );
@@ -82,10 +82,10 @@ protected function tokenize($string)
82
82
{
83
83
$ this ->logVerbose ('*** START GRAPHQL TOKENIZING *** ' );
84
84
85
- $ string = str_replace ($ this ->eolChar , "\n" , $ string );
86
- $ tokens = [];
87
- $ source = new Source ($ string );
88
- $ lexer = new Lexer ($ source );
85
+ $ string = str_replace ($ this ->eolChar , "\n" , $ string );
86
+ $ tokens = [];
87
+ $ source = new Source ($ string );
88
+ $ lexer = new Lexer ($ source );
89
89
90
90
do {
91
91
$ kind = $ lexer ->token ->kind ;
@@ -141,6 +141,41 @@ protected function tokenize($string)
141
141
return $ tokens ;
142
142
}
143
143
144
+ /**
145
+ * Fixes that keywords may be used as field, argument etc. names and could thus have been marked as special tokens
146
+ * while tokenizing.
147
+ */
148
+ private function fixErroneousKeywordTokens ()
149
+ {
150
+ $ processingCodeBlock = false ;
151
+ $ numTokens = count ($ this ->tokens );
152
+
153
+ for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
154
+ $ tokenCode = $ this ->tokens [$ i ]['code ' ];
155
+ $ tokenContent = $ this ->tokens [$ i ]['content ' ];
156
+
157
+ switch (true ) {
158
+ case $ tokenCode === T_OPEN_CURLY_BRACKET :
159
+ //we have hit the beginning of a code block
160
+ $ processingCodeBlock = true ;
161
+ break ;
162
+ case $ tokenCode === T_CLOSE_CURLY_BRACKET :
163
+ //we have hit the end of a code block
164
+ $ processingCodeBlock = false ;
165
+ break ;
166
+ case $ processingCodeBlock
167
+ && $ tokenCode !== T_STRING
168
+ && isset ($ this ->keywordTokenTypeMap [$ tokenContent ]):
169
+ //we have hit a keyword within a code block that is of wrong token type
170
+ $ this ->tokens [$ i ]['code ' ] = T_STRING ;
171
+ $ this ->tokens [$ i ]['type ' ] = 'T_STRING ' ;
172
+ break ;
173
+ default :
174
+ //NOP All operations have already been executed
175
+ }
176
+ }
177
+ }
178
+
144
179
/**
145
180
* Returns tokens of empty new lines for the range <var>$lineStart</var> to <var>$lineEnd</var>
146
181
*
@@ -213,10 +248,10 @@ private function logVerbose($message, $level = 1)
213
248
*/
214
249
private function processFields ()
215
250
{
216
- $ processingEntity = false ;
217
- $ numTokens = count ($ this ->tokens );
218
- $ entityTypes = [T_CLASS , T_INTERFACE ];
219
- $ skipTypes = [T_COMMENT , T_WHITESPACE ];
251
+ $ processingEntity = false ;
252
+ $ numTokens = count ($ this ->tokens );
253
+ $ entityTypes = [T_CLASS , T_INTERFACE ];
254
+ $ skipTypes = [T_COMMENT , T_WHITESPACE ];
220
255
221
256
for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
222
257
$ tokenCode = $ this ->tokens [$ i ]['code ' ];
0 commit comments