Skip to content

Commit 586244a

Browse files
MC-19366: Adds sniff for field names
1 parent 8cb90c8 commit 586244a

File tree

8 files changed

+249
-84
lines changed

8 files changed

+249
-84
lines changed

Magento2/Sniffs/GraphQL/ValidArgumentNameSniff.php

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
use PHP_CodeSniffer\Util\Common;
11+
12+
/**
13+
* Detects field names the are not specified in <kbd>snake_case</kbd>.
14+
*/
15+
class ValidFieldNameSniff implements Sniff
16+
{
17+
18+
/**
19+
* Defines the tokenizers that this sniff is using.
20+
*
21+
* @var array
22+
*/
23+
public $supportedTokenizers = ['GraphQL'];
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function register()
29+
{
30+
return [T_VARIABLE];
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function process(File $phpcsFile, $stackPtr)
37+
{
38+
$tokens = $phpcsFile->getTokens();
39+
$name = $tokens[$stackPtr]['content'];
40+
41+
if (!$this->isSnakeCase($name)) {
42+
$type = 'Field';
43+
$error = '%s name "%s" is not in snake_case format';
44+
$data = [
45+
$type,
46+
$name,
47+
];
48+
$phpcsFile->addError($error, $stackPtr, 'NotSnakeCase', $data);
49+
$phpcsFile->recordMetric($stackPtr, 'SnakeCase field name', 'no');
50+
} else {
51+
$phpcsFile->recordMetric($stackPtr, 'SnakeCase field name', 'yes');
52+
}
53+
}
54+
55+
/**
56+
* Returns whether <var>$name</var> is strictly lower case, potentially separated by underscores.
57+
*
58+
* @param string $name
59+
* @return bool
60+
*/
61+
private function isSnakeCase($name)
62+
{
63+
return preg_match('/^[a-z][a-z0-9_]*$/', $name);
64+
}
65+
}

Magento2/Sniffs/GraphQL/ValidTypeNameSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function process(File $phpcsFile, $stackPtr)
3838
{
3939
$tokens = $phpcsFile->getTokens();
4040

41-
//compose entity name by making use of the next strings the we find until we hit a non-string token
41+
//compose entity name by making use of the next strings that we find until we hit a non-string token
4242
$name = '';
4343
for ($i=$stackPtr+1; $tokens[$i]['code'] === T_STRING; ++$i) {
4444
$name .= $tokens[$i]['content'];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
* Implements an abstract base for unit tests that cover GraphQL sniffs.
13+
*/
14+
abstract class AbstractGraphQLSniffUnitTestCase 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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
type FooType {
2+
# Valid snake case field names
3+
valid_snake_case_field: String
4+
validlowercasefield: String
5+
valid_snake_case_field_ending_with_number_5: String
6+
valid_snake_case_field_with_ending_numbers_12345: String
7+
valid_snake_case_field_5_with_number5_inline: String
8+
9+
# Incorrect use of snake case
10+
INVALIDUPPERCASEFIELD: String
11+
INVALID_SCREAMING_SNAKE_CASE_FIELD: String
12+
Invalid_Upper_Snake_Case_Field: String
13+
invalid_mixed_case_FIELD: String
14+
InvalidCameCaseFieldName: String
15+
}
16+
17+
interface FooInterface {
18+
# Valid snake case field names
19+
valid_snake_case_field: String
20+
validlowercasefield: String
21+
valid_snake_case_field_ending_with_number_5: String
22+
valid_snake_case_field_with_ending_numbers_12345: String
23+
valid_snake_case_field_5_with_number5_inline: String
24+
25+
# Incorrect use of snake case
26+
INVALIDUPPERCASEFIELD: String
27+
INVALID_SCREAMING_SNAKE_CASE_FIELD: String
28+
Invalid_Upper_Snake_Case_Field: String
29+
invalid_mixed_case_FIELD: String
30+
InvalidCameCaseFieldName: String
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\ValidFieldNameUnitTest}.
10+
*/
11+
class ValidFieldNameUnitTest extends AbstractGraphQLSniffUnitTestCase
12+
{
13+
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @return array<int, int>
21+
*/
22+
protected function getErrorList()
23+
{
24+
return [
25+
10 => 1,
26+
11 => 1,
27+
12 => 1,
28+
13 => 1,
29+
14 => 1,
30+
26 => 1,
31+
27 => 1,
32+
28 => 1,
33+
29 => 1,
34+
30 => 1,
35+
];
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
protected function getWarningList()
42+
{
43+
return [];
44+
}
45+
}

Magento2/Tests/GraphQL/ValidTypeNameUnitTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,12 @@
66
namespace Magento2\Tests\GraphQL;
77

88
use PHP_CodeSniffer\Config;
9-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
109

1110
/**
12-
* Covers {@link \agento2\Sniffs\GraphQL\ValidTypeNameSniff}.
11+
* Covers {@link \Magento2\Sniffs\GraphQL\ValidTypeNameSniff}.
1312
*/
14-
class ValidTypeNameUnitTest extends AbstractSniffUnitTest
13+
class ValidTypeNameUnitTest extends AbstractGraphQLSniffUnitTestCase
1514
{
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-
3515
/**
3616
* Returns the lines where errors should occur.
3717
*

0 commit comments

Comments
 (0)