Skip to content

Commit 47fb203

Browse files
MC-19366: Adds sniff for top level fields
1 parent 27e99de commit 47fb203

9 files changed

+194
-48
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE
4+
*
5+
* Copyright (c) 2019 TechDivision GmbH <info@techdivision.com> - TechDivision GmbH
6+
* All rights reserved
7+
*
8+
* This product includes proprietary software developed at TechDivision GmbH, Germany
9+
* For more information see https://www.techdivision.com/
10+
*
11+
* To obtain a valid license for using this software please contact us at license@techdivision.com
12+
*/
13+
namespace Magento2\Sniffs\GraphQL;
14+
15+
use PHP_CodeSniffer\Sniffs\Sniff;
16+
17+
/**
18+
* Defines an abstract base class for GraphQL sniffs.
19+
*/
20+
abstract class AbstractGraphQLSniff implements Sniff
21+
{
22+
23+
/**
24+
* Defines the tokenizers that this sniff is using.
25+
*
26+
* @var array
27+
*/
28+
public $supportedTokenizers = ['GraphQL'];
29+
30+
/**
31+
* Returns whether <var>$name</var> starts with a lower case character and is written in camel case.
32+
*
33+
* @param string $name
34+
* @return bool
35+
*/
36+
protected function isCamelCase($name)
37+
{
38+
return (preg_match('/^[a-z][a-zA-Z0-9]+$/', $name) !== 0);
39+
}
40+
41+
/**
42+
* Returns whether <var>$name</var> is strictly lower case, potentially separated by underscores.
43+
*
44+
* @param string $name
45+
* @return bool
46+
*/
47+
protected function isSnakeCase($name)
48+
{
49+
return preg_match('/^[a-z][a-z0-9_]*$/', $name);
50+
}
51+
52+
}

Magento2/Sniffs/GraphQL/ValidArgumentNameSniff.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,16 @@
33
* Copyright © Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento2\Sniffs\GraphQL;
87

98
use PHP_CodeSniffer\Files\File;
10-
use PHP_CodeSniffer\Sniffs\Sniff;
119

1210
/**
1311
* Detects argument names that are not specified in <kbd>cameCase</kbd>.
1412
*/
15-
class ValidArgumentNameSniff implements Sniff
13+
class ValidArgumentNameSniff extends AbstractGraphQLSniff
1614
{
1715

18-
/**
19-
* Defines the tokenizers that this sniff is using.
20-
*
21-
* @var array
22-
*/
23-
public $supportedTokenizers = ['GraphQL'];
24-
2516
/**
2617
* @inheritDoc
2718
*/
@@ -132,14 +123,4 @@ private function getCloseParenthesisPointer($stackPointer, array $tokens)
132123
return false;
133124
}
134125

135-
/**
136-
* Returns whether <var>$name</var> starts with a lower case character and is written in camel case.
137-
*
138-
* @param string $argumentName
139-
* @return bool
140-
*/
141-
private function isCamelCase($argumentName)
142-
{
143-
return (preg_match('/^[a-z][a-zA-Z0-9]+$/', $argumentName) !== 0);
144-
}
145126
}

Magento2/Sniffs/GraphQL/ValidFieldNameSniff.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@
66
namespace Magento2\Sniffs\GraphQL;
77

88
use PHP_CodeSniffer\Files\File;
9-
use PHP_CodeSniffer\Sniffs\Sniff;
109

1110
/**
1211
* Detects field names the are not specified in <kbd>snake_case</kbd>.
1312
*/
14-
class ValidFieldNameSniff implements Sniff
13+
class ValidFieldNameSniff extends AbstractGraphQLSniff
1514
{
1615

17-
/**
18-
* Defines the tokenizers that this sniff is using.
19-
*
20-
* @var array
21-
*/
22-
public $supportedTokenizers = ['GraphQL'];
23-
2416
/**
2517
* @inheritDoc
2618
*/
@@ -51,14 +43,4 @@ public function process(File $phpcsFile, $stackPtr)
5143
}
5244
}
5345

54-
/**
55-
* Returns whether <var>$name</var> is strictly lower case, potentially separated by underscores.
56-
*
57-
* @param string $name
58-
* @return bool
59-
*/
60-
private function isSnakeCase($name)
61-
{
62-
return preg_match('/^[a-z][a-z0-9_]*$/', $name);
63-
}
6446
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\GraphQL;
7+
8+
use PHP_CodeSniffer\Files\File;
9+
10+
/**
11+
* Detects top level field names that are not specified in <kbd>cameCase</kbd>.
12+
*/
13+
class ValidTopLevelFieldNameSniff extends AbstractGraphQLSniff
14+
{
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function register()
20+
{
21+
return [T_FUNCTION];
22+
}
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function process(File $phpcsFile, $stackPtr)
28+
{
29+
$tokens = $phpcsFile->getTokens();
30+
31+
//compose function name by making use of the next strings that we find until we hit a non-string token
32+
$name = '';
33+
for ($i=$stackPtr+1; $tokens[$i]['code'] === T_STRING; ++$i) {
34+
$name .= $tokens[$i]['content'];
35+
}
36+
37+
if (strlen($name) > 0 && !$this->isCamelCase($name)) {
38+
$type = ucfirst($tokens[$stackPtr]['content']);
39+
$error = '%s name "%s" is not in PascalCase format';
40+
$data = [
41+
$type,
42+
$name,
43+
];
44+
$phpcsFile->addError($error, $stackPtr, 'NotCamelCase', $data);
45+
$phpcsFile->recordMetric($stackPtr, 'CamelCase top level field name', 'no');
46+
} else {
47+
$phpcsFile->recordMetric($stackPtr, 'CamelCase top level field name', 'yes');
48+
}
49+
}
50+
51+
}

Magento2/Sniffs/GraphQL/ValidTypeNameSniff.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
namespace Magento2\Sniffs\GraphQL;
77

88
use PHP_CodeSniffer\Files\File;
9-
use PHP_CodeSniffer\Sniffs\Sniff;
109
use PHP_CodeSniffer\Util\Common;
1110

1211
/**
1312
* Detects types (<kbd>type</kbd>, <kbd>interface</kbd> and <kbd>enum</kbd>) that are not specified in
1413
* <kbd>UpperCamelCase</kbd>.
1514
*/
16-
class ValidTypeNameSniff implements Sniff
15+
class ValidTypeNameSniff extends AbstractGraphQLSniff
1716
{
1817

19-
/**
20-
* Defines the tokenizers that this sniff is using.
21-
*
22-
* @var array
23-
*/
24-
public $supportedTokenizers = ['GraphQL'];
25-
2618
/**
2719
* @inheritDoc
2820
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Valid names
2+
query validCamelCaseQuery {}
3+
mutation validCamelCaseMutation {}
4+
5+
# Incorrect use of camel cyse
6+
query InvalidCamelCaseQuery {}
7+
query invalid_Camel_Case_Query_With_Underscores {}
8+
mutation InvalidCamelCaseMutation {}
9+
mutation invalid_Camel_Case_Mutation_With_Underscores {}
10+
11+
# All lower case
12+
query validlowercasequery {}
13+
mutation validlowercasemutation {}
14+
15+
# All upper case
16+
query INVALIDUPPERCASEQUERY {}
17+
mutation INVALIDUPPERCASEMUTATION {}
18+
19+
# Mix camel case with uppercase
20+
query validCamelCaseQueryWithUPPERCASE {}
21+
mutation validCamelCaseMutationWithUPPERCASE {}
22+
23+
# Usage of numeric characters
24+
query validCamelCaseQueryWith1Number {}
25+
query validCamelCaseQueryWith12345Numbers {}
26+
query validCamelCaseQueryEndingWithNumber4 {}
27+
query 5invalidCamelCaseQueryStartingWithNumber {}
28+
mutation validCamelCaseMutationWith1Number {}
29+
mutation validCamelCaseMutationWith12345Numbers {}
30+
mutation validCamelCaseMutationEndingWithNumber4 {}
31+
mutation 5invalidCamelCaseMutationStartingWithNumber {}
32+
33+
# Empty names (valid by definition of GraphQL)
34+
query {}
35+
mutation {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\GraphQL;
7+
8+
/**
9+
* Covers {@link \Magento2\Sniffs\GraphQL\ValidTopLevelFieldNameSniff}.
10+
*/
11+
class ValidTopLevelFieldNameUnitTest extends AbstractGraphQLSniffUnitTestCase
12+
{
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
protected function getErrorList()
18+
{
19+
return [
20+
6 => 1,
21+
7 => 1,
22+
8 => 1,
23+
9 => 1,
24+
16 => 1,
25+
17 => 1,
26+
27 => 1,
27+
31 => 1,
28+
];
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
protected function getWarningList()
35+
{
36+
return [];
37+
}
38+
39+
}

Magento2/ruleset.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@
526526
<severity>6</severity>
527527
<type>warning</type>
528528
</rule>
529+
<rule ref="Magento2.GraphQL.ValidFieldName">
530+
<severity>6</severity>
531+
<type>warning</type>
532+
</rule>
533+
<rule ref="Magento2.GraphQL.ValidArgumentName">
534+
<severity>6</severity>
535+
<type>warning</type>
536+
</rule>
537+
<rule ref="Magento2.GraphQL.ValidTopLevelFieldName">
538+
<severity>6</severity>
539+
<type>warning</type>
540+
</rule>
529541

530542
<!-- Severity 5 warnings: PHPDoc formatting and commenting issues. -->
531543
<rule ref="Magento2.Commenting.ClassAndInterfacePHPDocFormatting">

PHP_CodeSniffer/Tokenizers/GraphQL.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class GraphQL extends Tokenizer
5858
'implements' => 'T_IMPLEMENTS',
5959
'type' => 'T_CLASS',
6060
'union' => 'T_CLASS',
61+
'query' => 'T_FUNCTION',
62+
'mutation' => 'T_FUNCTION',
6163
//TODO We may have to add further types
6264
];
6365

0 commit comments

Comments
 (0)