Skip to content

Commit af9c041

Browse files
MC-19366: Fixes that list type and non null arguments would not be parsed as expected and would thus lead to false positives
1 parent c539ed4 commit af9c041

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

Magento2/Sniffs/GraphQL/ValidArgumentNameSniff.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,21 @@ public function process(File $phpcsFile, $stackPtr)
8585
*/
8686
private function getArgumentDefinitionEndPointer($argumentDefinitionStartPointer, array $tokens)
8787
{
88-
$colonPointer = $this->seekToken(T_COLON, $tokens, $argumentDefinitionStartPointer);
88+
$endPointer = $this->seekToken(T_COLON, $tokens, $argumentDefinitionStartPointer);
89+
90+
//the colon is always followed by the type, which we can consume. it could be a list type though, thus we check
91+
if ($tokens[$endPointer + 1]['code'] === T_OPEN_SQUARE_BRACKET) {
92+
//consume everything up to closing bracket
93+
$endPointer = $tokens[$endPointer + 1]['bracket_closer'];
94+
} else {
95+
//consume everything up to type
96+
++$endPointer;
97+
}
8998

90-
//the colon is always followed by a type so we can consume the token after the colon
91-
$endPointer = $colonPointer + 1;
99+
//the type may be non null, meaning that it is followed by an exclamation mark, which we consume
100+
if ($tokens[$endPointer + 1]['code'] === T_BOOLEAN_NOT) {
101+
++$endPointer;
102+
}
92103

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

Magento2/Tests/GraphQL/ValidArgumentNameUnitTest.graphqls

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ type Foo @doc(descripton: "Foo Bar Baz") {
4141
# A comment
4242
description: "This is a valid argument, spanned over multiple lines."
4343
) @foo
44-
varlidArgumentWithDefaultValue: Int = 20 @doc(description: "This is another valid argument with a default value.")
44+
validArgumentWithDefaultValue: Int = 20 @doc(description: "This is another valid argument with a default value.")
45+
validArgumentListType: [String] @doc(description: "This is a valid argument that uses a list type.")
46+
validArgumentNonNullType: String! @doc(description: "This is a valid argument that uses a non null type.")
47+
validArgumentNonNullListType: [String]! @doc(description: "This is a valid argument that uses a non null type.")
48+
validArgumentNonNullTypeInListType: [String!] @doc(description: "This is a valid argument that uses a non null type within a list.")
49+
validArgumentNonNullTypeInNonNullListType: [String!]! @doc(description: "This is a valid argument that uses a non null type within a non null list type.")
4550
# Invalid argument
4651
invalid_argument: String @doc(description: "This is an invalid argument."),
4752
invalid_argument_with_default_value: Int = 20 @doc(description: "This is another invalid argument with a default value")
53+
invalid_argument_list_type: [String] @doc(description: "This is an invalid argument that uses a list type.")
54+
invalid_argument_non_null_type: String! @doc(description: "This is an invalid argument that uses a non null type.")
55+
invalid_argument_non_null_list_type: [String]! @doc(description: "This is an invalid argument that uses a non null type.")
56+
invalid_argument_non_null_type_in_list_type: [String!] @doc(description: "This is an invalid argument that uses a non null type within a list.")
57+
invalid_argument_non_null_type_in_non_null_list_type: [String!]! @doc(description: "This is a valid argument that uses a non null type within a non null list type.")
4858
): String @doc(description: "Foo Bar")
4959
}

Magento2/Tests/GraphQL/ValidArgumentNameUnitTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ protected function getErrorList()
2424
28 => 1,
2525
29 => 1,
2626
30 => 1,
27-
46 => 1,
28-
47 => 1,
27+
51 => 1,
28+
52 => 1,
29+
53 => 1,
30+
54 => 1,
31+
55 => 1,
32+
56 => 1,
33+
57 => 1,
2934
];
3035
}
3136

PHP_CodeSniffer/Tokenizers/GRAPHQL.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class GRAPHQL extends Tokenizer
2727
*/
2828
private $tokenTypeMap = [
2929
Token::AT => 'T_DOC_COMMENT_TAG',
30-
Token::BANG => null,
30+
Token::BANG => 'T_BOOLEAN_NOT',
3131
Token::BLOCK_STRING => 'T_COMMENT',
3232
Token::BRACE_L => 'T_OPEN_CURLY_BRACKET',
3333
Token::BRACE_R => 'T_CLOSE_CURLY_BRACKET',
3434
Token::BRACKET_L => 'T_OPEN_SQUARE_BRACKET',
35-
Token::BRACKET_R => 'T_CLOSE_CURLY_BRACKET',
35+
Token::BRACKET_R => 'T_CLOSE_SQUARE_BRACKET',
3636
Token::COLON => 'T_COLON',
3737
Token::COMMENT => 'T_COMMENT',
3838
Token::DOLLAR => 'T_DOLLAR',
@@ -112,6 +112,9 @@ protected function tokenize($string)
112112
case Token::PAREN_L:
113113
case Token::PAREN_R:
114114
case Token::COLON:
115+
case Token::BRACKET_L:
116+
case Token::BRACKET_R:
117+
case Token::BANG:
115118
$value = $kind;
116119
break;
117120
default:

0 commit comments

Comments
 (0)