-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathAbstractGraphQLSniff.php
94 lines (82 loc) · 2.74 KB
/
AbstractGraphQLSniff.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\GraphQL;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Defines an abstract base class for GraphQL sniffs.
*/
abstract class AbstractGraphQLSniff implements Sniff
{
/**
* Defines the tokenizers that this sniff is using.
*
* @var array
*/
public $supportedTokenizers = ['GRAPHQL'];
/**
* Returns whether <var>$name</var> starts with a lower case character and is written in camel case.
*
* @param string $name
* @return bool
*/
protected function isCamelCase($name)
{
return (preg_match('/^[a-z][a-zA-Z0-9]+$/', $name) !== 0);
}
/**
* Returns whether <var>$name</var> is specified in snake case (either all lower case or all upper case).
*
* @param string $name
* @param bool $upperCase If set to <kbd>true</kbd> checks for all upper case, otherwise all lower case
* @return bool
*/
protected function isSnakeCase($name, $upperCase = false)
{
$pattern = $upperCase ? '/^[A-Z][A-Z0-9_]*$/' : '/^[a-z][a-z0-9_]*$/';
return preg_match($pattern, $name);
}
/**
* Returns the pointer to the last token of a directive if the token at <var>$startPointer</var> starts a directive.
*
* @param array $tokens
* @param int $startPointer
* @return int The end of the directive if one is found, the start pointer otherwise
*/
protected function seekEndOfDirective(array $tokens, $startPointer)
{
$endPointer = $startPointer;
if ($tokens[$startPointer]['code'] === T_DOC_COMMENT_TAG) {
//advance to next token
++$endPointer;
//if next token is an opening parenthesis, we consume everything up to the closing parenthesis
if ($tokens[$endPointer + 1]['code'] === T_OPEN_PARENTHESIS) {
$endPointer = $tokens[$endPointer + 1]['parenthesis_closer'];
}
}
return $endPointer;
}
/**
* Searches for the first token that has <var>$tokenCode</var> in <var>$tokens</var> from position
* <var>$startPointer</var> (excluded).
*
* @param mixed $tokenCode
* @param array $tokens
* @param int $startPointer
* @return bool|int If token was found, returns its pointer, <kbd>false</kbd> otherwise
*/
protected function seekToken($tokenCode, array $tokens, $startPointer = 0)
{
$numTokens = count($tokens);
for ($i = $startPointer + 1; $i < $numTokens; ++$i) {
if ($tokens[$i]['code'] === $tokenCode) {
return $i;
}
}
//if we came here we could not find the requested token
return false;
}
}