1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @param int $code |
7
|
|
|
* @return string |
8
|
|
|
*/ |
9
|
|
|
function printCharCode(int $code): string |
10
|
|
|
{ |
11
|
|
|
if ($code === 0x0000) { |
12
|
|
|
return '<EOF>'; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
return $code < 0x007F |
16
|
|
|
// Trust JSON for ASCII. |
17
|
|
|
? \json_encode(\mb_chr($code)) |
|
|
|
|
18
|
|
|
// Otherwise print the escaped form. |
19
|
|
|
: '"\\u' . \dechex($code) . '"'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $string |
24
|
|
|
* @param int $start |
25
|
|
|
* @param int|null $end |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
function sliceString(string $string, int $start, int $end = null): string |
29
|
|
|
{ |
30
|
|
|
$length = $end !== null ? $end - $start : \mb_strlen($string) - $start; |
31
|
|
|
return \mb_substr($string, $start, $length); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param int $code |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
function isLetter(int $code): bool |
39
|
|
|
{ |
40
|
|
|
return ($code >= 65 && $code <= 90) || ($code >= 97 && $code <= 122); // a-z or A-Z |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int $code |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
function isNumber(int $code): bool |
48
|
|
|
{ |
49
|
|
|
return $code >= 48 && $code <= 57; // 0-9 |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $code |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
function isUnderscore(int $code): bool |
57
|
|
|
{ |
58
|
|
|
return $code === 95; // _ |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $code |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
function isAlphaNumeric(int $code): bool |
66
|
|
|
{ |
67
|
|
|
return isLetter($code) || isNumber($code) || isUnderscore($code); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int $code |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
function isLineTerminator(int $code): bool |
75
|
|
|
{ |
76
|
|
|
return $code === 0x000a || $code === 0x000d; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $code |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
function isSourceCharacter(int $code): bool |
84
|
|
|
{ |
85
|
|
|
return $code < 0x0020 && $code !== 0x0009; // any source character EXCEPT HT (Horizontal Tab) |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $value |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
function isOperation(string $value): bool |
93
|
|
|
{ |
94
|
|
|
return $value === 'query' || $value === 'mutation' || $value === 'subscription'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $location |
99
|
|
|
* @return array|null |
100
|
|
|
*/ |
101
|
|
|
function locationShorthandToArray(array $location): ?array |
102
|
|
|
{ |
103
|
|
|
return isset($location[0], $location[1]) ? ['line' => $location[0], 'column' => $location[1]] : null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $locations |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
function locationsShorthandToArray(array $locations): array |
111
|
|
|
{ |
112
|
|
|
return array_map(function ($shorthand) { |
113
|
|
|
return locationShorthandToArray($shorthand); |
114
|
|
|
}, $locations); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $array |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
function block(array $array): string |
122
|
|
|
{ |
123
|
|
|
return !empty($array) ? "{\n" . indent(implode("\n", $array)) . "\n}" : ''; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $start |
128
|
|
|
* @param null|string $maybeString |
129
|
|
|
* @param null|string $end |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
function wrap(string $start, ?string $maybeString = null, ?string $end = null): string |
133
|
|
|
{ |
134
|
|
|
return null !== $maybeString ? ($start . $maybeString . ($end ?? '')) : ''; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param null|string $maybeString |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
function indent(?string $maybeString): string |
142
|
|
|
{ |
143
|
|
|
return null !== $maybeString ? ' ' . preg_replace("/\n/", "\n ", $maybeString) : ''; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $str |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
function dedent(string $str): string |
151
|
|
|
{ |
152
|
|
|
$trimmed = \preg_replace("/^\n*|[ \t]*$/", '', $str); // Remove leading newline and trailing whitespace |
153
|
|
|
$matches = []; |
154
|
|
|
\preg_match("/^[ \t]*/", $trimmed, $matches); // Figure out indent |
155
|
|
|
$indent = $matches[0]; |
156
|
|
|
return \str_replace($indent, '', $trimmed); // Remove indent |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param mixed $value |
161
|
|
|
* @param bool $isDescription |
162
|
|
|
* @return null|string|string[] |
163
|
|
|
*/ |
164
|
|
|
function printBlockString($value, bool $isDescription) |
165
|
|
|
{ |
166
|
|
|
$escaped = \preg_replace('/"""/g', '\\"""', $value); |
167
|
|
|
return $value{0} === ' ' || ($value{0} === "\t" && false === strpos($value, "\n")) |
168
|
|
|
? '"""' . \preg_replace('/"$/', "\"\n", $escaped) . '"""' |
169
|
|
|
: '"""' . $isDescription ? $escaped : indent($escaped) . "\n" . '"""'; |
170
|
|
|
} |
171
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.