Skip to content

Commit 571b642

Browse files
MC-19366: Fixes that field and argument names were marked as entities in case their name was a keyword
1 parent 0b8eeaa commit 571b642

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

PHP_CodeSniffer/Tokenizers/GRAPHQL.php

+44-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace PHP_CodeSniffer\Tokenizers;
88

99
use GraphQL\Language\Lexer;
10-
use GraphQL\Language\Parser;
1110
use GraphQL\Language\Source;
1211
use GraphQL\Language\Token;
1312

@@ -70,6 +69,7 @@ public function processAdditional()
7069
{
7170
$this->logVerbose('*** START ADDITIONAL GRAPHQL PROCESSING ***');
7271

72+
$this->fixErroneousKeywordTokens();
7373
$this->processFields();
7474

7575
$this->logVerbose('*** END ADDITIONAL GRAPHQL PROCESSING ***');
@@ -82,10 +82,10 @@ protected function tokenize($string)
8282
{
8383
$this->logVerbose('*** START GRAPHQL TOKENIZING ***');
8484

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);
8989

9090
do {
9191
$kind = $lexer->token->kind;
@@ -141,6 +141,41 @@ protected function tokenize($string)
141141
return $tokens;
142142
}
143143

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+
144179
/**
145180
* Returns tokens of empty new lines for the range <var>$lineStart</var> to <var>$lineEnd</var>
146181
*
@@ -213,10 +248,10 @@ private function logVerbose($message, $level = 1)
213248
*/
214249
private function processFields()
215250
{
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];
220255

221256
for ($i = 0; $i < $numTokens; ++$i) {
222257
$tokenCode = $this->tokens[$i]['code'];

0 commit comments

Comments
 (0)