Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion Magento2/Sniffs/GraphQL/ValidTypeNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function process(File $phpcsFile, $stackPtr)
//compose entity name by making use of the next strings that we find until we hit a non-string token
$name = '';
for ($i=$stackPtr+1; $tokens[$i]['code'] === T_STRING; ++$i) {
$name .= $tokens[$i]['content'];
/**
* @see \PHP_CodeSniffer\Tokenizers\GRAPHQL::tokenize Removing EOL character artificially added to token
*/
$name .= rtrim($tokens[$i]['content']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to also allow white-space on the left of the name?

Suggested change
$name .= rtrim($tokens[$i]['content']);
$name .= trim($tokens[$i]['content']);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that tokenizer is never gonna create a T_STRING token with a whitespace on the left.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either fix the tokeniser to never include (leading nor) trailing whitespace in a T_STRING token, or run this through trim(). Using rtrim() seems incomplete.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation and highlighting where the extra white-space comes from (ie, the GraphQL tokeniser within this repository). Let's add a code comment here so others know why removing the trailing newline character(s) is safe in this context.

}

$valid = Common::isCamelCaps($name, true, true, false);
Expand Down
2 changes: 2 additions & 0 deletions Magento2/Tests/GraphQL/ValidTypeNameUnitTest.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Valid type names.
type ValidCamelCaseType {}
type ValidCamelCaseTypeNewLineBrace
{}
interface ValidCamelCaseInterface {}
enum ValidCamelCaseEnum {}

Expand Down
16 changes: 8 additions & 8 deletions Magento2/Tests/GraphQL/ValidTypeNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class ValidTypeNameUnitTest extends AbstractGraphQLSniffUnitTestCase
protected function getErrorList()
{
return [
7 => 1,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 1,
15 => 1,
16 => 1,
13 => 1,
14 => 1,
17 => 1,
21 => 1,
18 => 1,
19 => 1,
23 => 1,
25 => 1,
35 => 1,
39 => 1,
43 => 1,
27 => 1,
37 => 1,
41 => 1,
45 => 1,
];
}

Expand Down