Skip to content

Commit

Permalink
MC-19366: Fixes that field and argument names were marked as entities…
Browse files Browse the repository at this point in the history
… in case their name was a keyword
  • Loading branch information
jean-bernard-valentaten committed Sep 13, 2019
1 parent 0b8eeaa commit 571b642
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions PHP_CodeSniffer/Tokenizers/GRAPHQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace PHP_CodeSniffer\Tokenizers;

use GraphQL\Language\Lexer;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Language\Token;

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

$this->fixErroneousKeywordTokens();
$this->processFields();

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

$string = str_replace($this->eolChar, "\n", $string);
$tokens = [];
$source = new Source($string);
$lexer = new Lexer($source);
$string = str_replace($this->eolChar, "\n", $string);
$tokens = [];
$source = new Source($string);
$lexer = new Lexer($source);

do {
$kind = $lexer->token->kind;
Expand Down Expand Up @@ -141,6 +141,41 @@ protected function tokenize($string)
return $tokens;
}

/**
* Fixes that keywords may be used as field, argument etc. names and could thus have been marked as special tokens
* while tokenizing.
*/
private function fixErroneousKeywordTokens()
{
$processingCodeBlock = false;
$numTokens = count($this->tokens);

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

switch (true) {
case $tokenCode === T_OPEN_CURLY_BRACKET:
//we have hit the beginning of a code block
$processingCodeBlock = true;
break;
case $tokenCode === T_CLOSE_CURLY_BRACKET:
//we have hit the end of a code block
$processingCodeBlock = false;
break;
case $processingCodeBlock
&& $tokenCode !== T_STRING
&& isset($this->keywordTokenTypeMap[$tokenContent]):
//we have hit a keyword within a code block that is of wrong token type
$this->tokens[$i]['code'] = T_STRING;
$this->tokens[$i]['type'] = 'T_STRING';
break;
default:
//NOP All operations have already been executed
}
}
}

/**
* Returns tokens of empty new lines for the range <var>$lineStart</var> to <var>$lineEnd</var>
*
Expand Down Expand Up @@ -213,10 +248,10 @@ private function logVerbose($message, $level = 1)
*/
private function processFields()
{
$processingEntity = false;
$numTokens = count($this->tokens);
$entityTypes = [T_CLASS, T_INTERFACE];
$skipTypes = [T_COMMENT, T_WHITESPACE];
$processingEntity = false;
$numTokens = count($this->tokens);
$entityTypes = [T_CLASS, T_INTERFACE];
$skipTypes = [T_COMMENT, T_WHITESPACE];

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

0 comments on commit 571b642

Please sign in to comment.