-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
LowerCaseConstantSniff.php
228 lines (194 loc) · 7.8 KB
/
LowerCaseConstantSniff.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Checks that all uses of true, false and null are lowercase.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class LowerCaseConstantSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [
'PHP',
'JS',
];
/**
* The tokens this sniff is targetting.
*
* @var array
*/
private $targets = [
T_TRUE => T_TRUE,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
];
/**
* Token types which can be encountered in a property type declaration.
*
* @var array<int|string, int|string>
*/
private $propertyTypeTokens = [
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_FALSE => T_FALSE,
T_TRUE => T_TRUE,
T_NULL => T_NULL,
T_STRING => T_STRING,
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE => T_NAME_RELATIVE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_NAMESPACE => T_NAMESPACE,
T_TYPE_UNION => T_TYPE_UNION,
T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
T_NULLABLE => T_NULLABLE,
];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
$targets = $this->targets;
// Register scope modifiers to filter out property type declarations.
$targets += Tokens::$scopeModifiers;
$targets[] = T_VAR;
$targets[] = T_STATIC;
$targets[] = T_READONLY;
// Register function keywords to filter out param/return type declarations.
$targets[] = T_FUNCTION;
$targets[] = T_CLOSURE;
$targets[] = T_FN;
return $targets;
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
* pointer is reached.
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
/*
* Skip over type declarations for properties.
*
* Note: for other uses of the visibility modifiers (functions, constants, trait use),
* nothing relevant will be skipped as the next non-empty token will be an "non-skippable"
* one.
* Functions are handled separately below (and then skip to their scope opener), so
* this should also not cause any confusion for constructor property promotion.
*
* For other uses of the "static" keyword, it also shouldn't be problematic as the only
* time the next non-empty token will be a "skippable" token will be in return type
* declarations, in which case, it is correct to skip over them.
*/
if (isset(Tokens::$scopeModifiers[$tokens[$stackPtr]['code']]) === true
|| $tokens[$stackPtr]['code'] === T_VAR
|| $tokens[$stackPtr]['code'] === T_STATIC
|| $tokens[$stackPtr]['code'] === T_READONLY
) {
$skipOver = (Tokens::$emptyTokens + $this->propertyTypeTokens);
$skipTo = $phpcsFile->findNext($skipOver, ($stackPtr + 1), null, true);
if ($skipTo !== false) {
return $skipTo;
}
// If we're at the end of the file, just return.
return;
}
// Handle function declarations separately as they may contain the keywords in type declarations.
if ($tokens[$stackPtr]['code'] === T_FUNCTION
|| $tokens[$stackPtr]['code'] === T_CLOSURE
|| $tokens[$stackPtr]['code'] === T_FN
) {
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
return;
}
// Make sure to skip over return type declarations.
$end = $tokens[$stackPtr]['parenthesis_closer'];
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
$end = $tokens[$stackPtr]['scope_opener'];
} else {
$skipTo = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], ($end + 1), null, false, null, true);
if ($skipTo !== false) {
$end = $skipTo;
}
}
// Do a quick check if any of the targets exist in the declaration.
$found = $phpcsFile->findNext($this->targets, $tokens[$stackPtr]['parenthesis_opener'], $end);
if ($found === false) {
// Skip forward, no need to examine these tokens again.
return $end;
}
// Handle the whole function declaration in one go.
$params = $phpcsFile->getMethodParameters($stackPtr);
foreach ($params as $param) {
if (isset($param['default_token']) === false) {
continue;
}
$paramEnd = $param['comma_token'];
if ($param['comma_token'] === false) {
$paramEnd = $tokens[$stackPtr]['parenthesis_closer'];
}
for ($i = $param['default_token']; $i < $paramEnd; $i++) {
if (isset($this->targets[$tokens[$i]['code']]) === true) {
$this->processConstant($phpcsFile, $i);
}
}
}
// Skip over return type declarations.
return $end;
}//end if
// Handle everything else.
$this->processConstant($phpcsFile, $stackPtr);
}//end process()
/**
* Processes a non-type declaration constant.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
protected function processConstant(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$keyword = $tokens[$stackPtr]['content'];
$expected = strtolower($keyword);
if ($keyword !== $expected) {
if ($keyword === strtoupper($keyword)) {
$phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'upper');
} else {
$phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'mixed');
}
$error = 'TRUE, FALSE and NULL must be lowercase; expected "%s" but found "%s"';
$data = [
$expected,
$keyword,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
}
} else {
$phpcsFile->recordMetric($stackPtr, 'PHP constant case', 'lower');
}
}//end processConstant()
}//end class