-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathValidFieldNameSniff.php
46 lines (41 loc) · 1.06 KB
/
ValidFieldNameSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\GraphQL;
use PHP_CodeSniffer\Files\File;
/**
* Detects field names the are not specified in <kbd>snake_case</kbd>.
*/
class ValidFieldNameSniff extends AbstractGraphQLSniff
{
/**
* @inheritDoc
*/
public function register()
{
return [T_VARIABLE];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$name = $tokens[$stackPtr]['content'];
if (!$this->isSnakeCase($name)) {
$type = 'Field';
$error = '%s name "%s" is not in snake_case format';
$data = [
$type,
$name,
];
$phpcsFile->addError($error, $stackPtr, 'NotSnakeCase', $data);
$phpcsFile->recordMetric($stackPtr, 'SnakeCase field name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'SnakeCase field name', 'yes');
}
}
}