Skip to content

Commit 6003dcd

Browse files
MC-19366: Adds sniff for type names
1 parent ba0bbce commit 6003dcd

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff;
9+
10+
/**
11+
* Detects types (<kbd>type</kbd>, <kbd>interface</kbd> and <kbd>enum</kbd>) that are not specified in
12+
* <kbd>UpperCamelCase</kbd>.
13+
*/
14+
class ValidTypeNameSniff extends ValidClassNameSniff
15+
{
16+
17+
/**
18+
* Defines the tokenizers that this sniff is using.
19+
*
20+
* @var array
21+
*/
22+
public $supportedTokenizers = ['GraphQL'];
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function register()
28+
{
29+
return [T_CLASS];
30+
}
31+
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Valid type names.
2+
type ValidCamelCaseType {}
3+
interface ValidCamelCaseInterface {}
4+
enum ValidCamelCaseEnum {}
5+
6+
# Incorrect usage of camel case.
7+
type invalidCamelCaseType {}
8+
type Invalid_Camel_Case_Type_With_Underscores {}
9+
interface invalidCamelCaseInterface {}
10+
interface Invalid_Camel_Case_Interface_With_Underscores {}
11+
enum invalidCamelCaseEnum {}
12+
enum Invalid_Camel_Case_Enum_With_Underscores {}
13+
14+
# All lowercase
15+
type invalidlowercasetype {}
16+
interface invalidlowercaseinterface {}
17+
enum invalidlowercaseenum {}
18+
19+
# All uppercase
20+
type VALIDUPPERCASETYPE {}
21+
type INVALID_UPPERCASE_TYPE_WITH_UNDERSCORES {}
22+
interface VALIDUPPERCASEINTERFACE {}
23+
interface INVALID_UPPERCASE_INTERFACE_WITH_UNDERSCORES {}
24+
enum VALIDUPPERCASECENUM {}
25+
enum INVALID_UPPERCASE_ENUM_WITH_UNDERSCORES {}
26+
27+
# Mix camel case with uppercase
28+
type ValidCamelCaseTypeWITHUPPERCASE {}
29+
interface ValidCamelCaseInterfaceWITHUPPERCASE {}
30+
enum ValidCamelCaseEnumWITHUPPERCASE {}
31+
32+
# Usage of numeric characters
33+
type ValidCamelCaseTypeWith1Number {}
34+
type ValidCamelCaseTypeWith12345Numbers {}
35+
type 5InvalidCamelCaseTypeStartingWithNumber {}
36+
type ValidCamelCaseTypeEndingWithNumber4 {}
37+
interface ValidCamelCaseInterfaceWith1Number {}
38+
interface ValidCamelCaseInterfaceWith12345Numbers {}
39+
interface 5InvalidCamelCaseInterfaceStartingWithNumber {}
40+
interface ValidCamelCaseInterfaceEndingWithNumber4 {}
41+
enum ValidCamelCaseEnumWith1Number {}
42+
enum ValidCamelCaseEnumWith12345Numbers {}
43+
enum 5InvalidCamelCaseEnumStartingWithNumber {}
44+
enum ValidCamelCaseEnumEndingWithNumber4 {}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\GraphQL;
7+
8+
use PHP_CodeSniffer\Config;
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
11+
/**
12+
* Covers {@link \agento2\Sniffs\GraphQL\ValidTypeNameSniff}.
13+
*/
14+
class ValidTypeNameUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
protected function setUp()
18+
{
19+
//let parent do its job
20+
parent::setUp();
21+
22+
//generate a config that allows ro use our GraphQL tokenizer
23+
$config = new Config();
24+
$config->extensions = array_merge(
25+
$config->extensions,
26+
[
27+
'graphqls' => 'GraphQL'
28+
]
29+
);
30+
31+
//and write back to a global that is used in base class
32+
$GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config;
33+
}
34+
35+
/**
36+
* Returns the lines where errors should occur.
37+
*
38+
* The key of the array should represent the line number and the value
39+
* should represent the number of errors that should occur on that line.
40+
*
41+
* @return array<int, int>
42+
*/
43+
protected function getErrorList()
44+
{
45+
return [
46+
7 => 1,
47+
8 => 1,
48+
9 => 1,
49+
10 => 1,
50+
11 => 1,
51+
12 => 1,
52+
15 => 1,
53+
16 => 1,
54+
17 => 1,
55+
21 => 1,
56+
23 => 1,
57+
25 => 1,
58+
35 => 1,
59+
39 => 1,
60+
43 => 1,
61+
];
62+
}
63+
64+
/**
65+
* Returns the lines where warnings should occur.
66+
*
67+
* The key of the array should represent the line number and the value
68+
* should represent the number of warnings that should occur on that line.
69+
*
70+
* @return array<int, int>
71+
*/
72+
protected function getWarningList()
73+
{
74+
return [];
75+
}
76+
}
77+

0 commit comments

Comments
 (0)