Skip to content

MC-19366: Adds GraphQL sniffs #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ba0bbce
MC-19366: Adds a tokenizer for GraphQL schema files
jean-bernard-valentaten Sep 6, 2019
6003dcd
MC-19366: Adds sniff for type names
jean-bernard-valentaten Sep 6, 2019
8cb90c8
MC-19366: Improves GraphQL tokenizer by making use of webonyx/graphql…
jean-bernard-valentaten Sep 9, 2019
586244a
MC-19366: Adds sniff for field names
jean-bernard-valentaten Sep 10, 2019
27e99de
MC-19366: Adds sniff for argument names
jean-bernard-valentaten Sep 11, 2019
47fb203
MC-19366: Adds sniff for top level fields
jean-bernard-valentaten Sep 11, 2019
6f5d136
MC-19366: Downgrade of package webonyx/graphql-php to match lowest ph…
jean-bernard-valentaten Sep 11, 2019
7a5b266
MC-19366: Fixes code style issues
jean-bernard-valentaten Sep 11, 2019
e310c5c
MC-19366: Fixes code style issue
jean-bernard-valentaten Sep 11, 2019
2a8d521
MC-19366: Deactivates field name validation
jean-bernard-valentaten Sep 12, 2019
505c9b2
MC-19366: Adds sniff for valid enum values
jean-bernard-valentaten Sep 12, 2019
19cdbec
MC-19366: Activates sniff for enum values
jean-bernard-valentaten Sep 12, 2019
01eee92
MC-19366: Fixes file doc
jean-bernard-valentaten Sep 12, 2019
d99c922
MC-19366: Fixes that tokenizer for GraphQL files could not be loaded …
jean-bernard-valentaten Sep 13, 2019
49f0b0b
MC-19366: Fixes that agruments with directives would result in errone…
jean-bernard-valentaten Sep 13, 2019
0b8eeaa
MC-19366: Fixes that field name validation was executed although it s…
jean-bernard-valentaten Sep 13, 2019
571b642
MC-19366: Fixes that field and argument names were marked as entities…
jean-bernard-valentaten Sep 13, 2019
c574003
MC-19366: Fixes code style issue
jean-bernard-valentaten Sep 13, 2019
43ef0a4
MC-19366: Removes obsolete TODOs
jean-bernard-valentaten Sep 16, 2019
c539ed4
MC-19366: Fixes order of rules and silences GraphQL field name valida…
jean-bernard-valentaten Sep 17, 2019
af9c041
MC-19366: Fixes that list type and non null arguments would not be pa…
jean-bernard-valentaten Sep 17, 2019
c4069e0
MC-19366: Fixes that enum values with directives would be parsed as e…
jean-bernard-valentaten Sep 17, 2019
874c9d7
MC-19366: Fixes code style issue
jean-bernard-valentaten Sep 17, 2019
4d99ee4
MC-19366: Fixes warning when composer install was run
jean-bernard-valentaten Sep 18, 2019
e252b19
MC-19366: Fixes invalid tag in PHPDoc
jean-bernard-valentaten Sep 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions Magento2/Sniffs/GraphQL/AbstractGraphQLSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\GraphQL;

use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Defines an abstract base class for GraphQL sniffs.
*/
abstract class AbstractGraphQLSniff implements Sniff
{
/**
* Defines the tokenizers that this sniff is using.
*
* @var array
*/
public $supportedTokenizers = ['GRAPHQL'];

/**
* Returns whether <var>$name</var> starts with a lower case character and is written in camel case.
*
* @param string $name
* @return bool
*/
protected function isCamelCase($name)
{
return (preg_match('/^[a-z][a-zA-Z0-9]+$/', $name) !== 0);
}

/**
* Returns whether <var>$name</var> is specified in snake case (either all lower case or all upper case).
*
* @param string $name
* @param bool $upperCase If set to <kbd>true</kbd> checks for all upper case, otherwise all lower case
* @return bool
*/
protected function isSnakeCase($name, $upperCase = false)
{
$pattern = $upperCase ? '/^[A-Z][A-Z0-9_]*$/' : '/^[a-z][a-z0-9_]*$/';
return preg_match($pattern, $name);
}

/**
* Returns the pointer to the last token of a directive if the token at <var>$startPointer</var> starts a directive.
*
* @param array $tokens
* @param int $startPointer
* @return int The end of the directive if one is found, the start pointer otherwise
*/
protected function seekEndOfDirective(array $tokens, $startPointer)
{
$endPointer = $startPointer;

if ($tokens[$startPointer]['code'] === T_DOC_COMMENT_TAG) {
//advance to next token
++$endPointer;

//if next token is an opening parenthesis, we consume everything up to the closing parenthesis
if ($tokens[$endPointer + 1]['code'] === T_OPEN_PARENTHESIS) {
$endPointer = $tokens[$endPointer + 1]['parenthesis_closer'];
}
}

return $endPointer;
}

/**
* Searches for the first token that has <var>$tokenCode</var> in <var>$tokens</var> from position
* <var>$startPointer</var> (excluded).
*
* @param mixed $tokenCode
* @param array $tokens
* @param int $startPointer
* @return bool|int If token was found, returns its pointer, <kbd>false</kbd> otherwise
*/
protected function seekToken($tokenCode, array $tokens, $startPointer = 0)
{
$numTokens = count($tokens);

for ($i = $startPointer + 1; $i < $numTokens; ++$i) {
if ($tokens[$i]['code'] === $tokenCode) {
return $i;
}
}

//if we came here we could not find the requested token
return false;
}
}
203 changes: 203 additions & 0 deletions Magento2/Sniffs/GraphQL/ValidArgumentNameSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\GraphQL;

use GraphQL\Error\SyntaxError;
use GraphQL\Language\AST\DocumentNode;
use PHP_CodeSniffer\Files\File;

/**
* Detects argument names that are not specified in <kbd>cameCase</kbd>.
*/
class ValidArgumentNameSniff extends AbstractGraphQLSniff
{

/**
* @inheritDoc
*/
public function register()
{
return [T_VARIABLE];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

//get the pointer to the argument list opener or bail out if none was found
//since then the field does not have arguments
$openArgumentListPointer = $this->getArgumentListOpenPointer($stackPtr, $tokens);
if ($openArgumentListPointer === false) {
return;
}

//get the pointer to the argument list closer or add a warning and terminate as we have an unbalanced file
$closeArgumentListPointer = $this->getArgumentListClosePointer($openArgumentListPointer, $tokens);
if ($closeArgumentListPointer === false) {
$error = 'Possible parse error: Missing closing parenthesis for argument list in line %d';
$data = [
$tokens[$stackPtr]['line'],
];
$phpcsFile->addWarning($error, $stackPtr, 'UnclosedArgumentList', $data);
return;
}

$arguments = $this->getArguments($openArgumentListPointer, $closeArgumentListPointer, $tokens);

foreach ($arguments as $pointer => $argument) {
if (!$this->isCamelCase($argument)) {
$type = 'Argument';
$error = '%s name "%s" is not in CamelCase format';
$data = [
$type,
$argument,
];

$phpcsFile->addError($error, $pointer, 'NotCamelCase', $data);
$phpcsFile->recordMetric($pointer, 'CamelCase argument name', 'no');
} else {
$phpcsFile->recordMetric($pointer, 'CamelCase argument name', 'yes');
}
}

//return stack pointer of closing parenthesis
return $closeArgumentListPointer;
}

/**
* Seeks the last token of an argument definition and returns its pointer.
*
* Arguments are defined as follows:
* <pre>
* {ArgumentName}: {ArgumentType}[ = {DefaultValue}][{Directive}]*
* </pre>
*
* @param int $argumentDefinitionStartPointer
* @param array $tokens
* @return int
*/
private function getArgumentDefinitionEndPointer($argumentDefinitionStartPointer, array $tokens)
{
$endPointer = $this->seekToken(T_COLON, $tokens, $argumentDefinitionStartPointer);

//the colon is always followed by the type, which we can consume. it could be a list type though, thus we check
if ($tokens[$endPointer + 1]['code'] === T_OPEN_SQUARE_BRACKET) {
//consume everything up to closing bracket
$endPointer = $tokens[$endPointer + 1]['bracket_closer'];
} else {
//consume everything up to type
++$endPointer;
}

//the type may be non null, meaning that it is followed by an exclamation mark, which we consume
if ($tokens[$endPointer + 1]['code'] === T_BOOLEAN_NOT) {
++$endPointer;
}

//if argument has a default value, we advance to the default definition end
if ($tokens[$endPointer + 1]['code'] === T_EQUAL) {
$endPointer += 2;
}

//while next token starts a directive, we advance to the end of the directive
while ($tokens[$endPointer + 1]['code'] === T_DOC_COMMENT_TAG) {
$endPointer = $this->seekEndOfDirective($tokens, $endPointer + 1);
}

return $endPointer;
}

/**
* Returns the closing parenthesis for the token found at <var>$openParenthesisPointer</var> in <var>$tokens</var>.
*
* @param int $openParenthesisPointer
* @param array $tokens
* @return bool|int
*/
private function getArgumentListClosePointer($openParenthesisPointer, array $tokens)
{
$openParenthesisToken = $tokens[$openParenthesisPointer];
return $openParenthesisToken['parenthesis_closer'];
}

/**
* Seeks the next available {@link T_OPEN_PARENTHESIS} token that comes directly after <var>$stackPointer</var>.
* token.
*
* @param int $stackPointer
* @param array $tokens
* @return bool|int
*/
private function getArgumentListOpenPointer($stackPointer, array $tokens)
{
//get next open parenthesis pointer or bail out if none was found
$openParenthesisPointer = $this->seekToken(T_OPEN_PARENTHESIS, $tokens, $stackPointer);
if ($openParenthesisPointer === false) {
return false;
}

//bail out if open parenthesis does not directly come after current stack pointer
if ($openParenthesisPointer !== $stackPointer + 1) {
return false;
}

//we have found the appropriate opening parenthesis
return $openParenthesisPointer;
}

/**
* Finds all argument names contained in <var>$tokens</var> in range <var>$startPointer</var> to
* <var>$endPointer</var>.
*
* The returned array uses token pointers as keys and argument names as values.
*
* @param int $startPointer
* @param int $endPointer
* @param array $tokens
* @return array<int, string>
*/
private function getArguments($startPointer, $endPointer, array $tokens)
{
$argumentTokenPointer = null;
$argument = '';
$names = [];
$skipTypes = [T_COMMENT, T_WHITESPACE];

for ($i = $startPointer + 1; $i < $endPointer; ++$i) {
$tokenCode = $tokens[$i]['code'];

switch (true) {
case in_array($tokenCode, $skipTypes):
//NOP This is a token that we have to skip
break;
case $tokenCode === T_COLON:
//we have reached the end of the argument name, thus we store its pointer and value
$names[$argumentTokenPointer] = $argument;

//advance to end of argument definition
$i = $this->getArgumentDefinitionEndPointer($argumentTokenPointer, $tokens);

//and reset temporary variables
$argument = '';
$argumentTokenPointer = null;
break;
default:
//this seems to be part of the argument name
$argument .= $tokens[$i]['content'];

if ($argumentTokenPointer === null) {
$argumentTokenPointer = $i;
}
}
}

return $names;
}
}
Loading