-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathPropertyDeclarationSniff.php
More file actions
311 lines (261 loc) · 12.6 KB
/
Copy pathPropertyDeclarationSniff.php
File metadata and controls
311 lines (261 loc) · 12.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* Verifies that properties are declared correctly.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2023 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes;
use Exception;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;
class PropertyDeclarationSniff extends AbstractVariableSniff
{
/**
* Only listen to variables within OO scopes.
*/
public function __construct()
{
AbstractScopeSniff::__construct(Tokens::OO_SCOPE_TOKENS, [T_VARIABLE], false);
}
/**
* Processes the function tokens within the class.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processMemberVar(File $phpcsFile, int $stackPtr)
{
try {
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
} catch (Exception $e) {
// Parse error: property in enum. Ignore.
return;
}
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'][1] === '_') {
$error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
}
// Detect multiple properties defined at the same time. Throw an error
// for this, but also only process the first property in the list so we don't
// repeat errors.
$find = Tokens::SCOPE_MODIFIERS;
$find[] = T_VARIABLE;
$find[] = T_VAR;
$find[] = T_STATIC;
$find[] = T_READONLY;
$find[] = T_FINAL;
$find[] = T_ABSTRACT;
$find[] = T_SEMICOLON;
$find[] = T_OPEN_CURLY_BRACKET;
$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
if ($tokens[$prev]['code'] === T_VARIABLE) {
return;
}
if ($tokens[$prev]['code'] === T_VAR) {
$error = 'The var keyword must not be used to declare a property';
$phpcsFile->addError($error, $stackPtr, 'VarUsed');
}
$next = $phpcsFile->findNext([T_VARIABLE, T_SEMICOLON], ($stackPtr + 1));
if ($next !== false && $tokens[$next]['code'] === T_VARIABLE) {
$error = 'There must not be more than one property declared per statement';
$phpcsFile->addError($error, $stackPtr, 'Multiple');
}
if ($propertyInfo['type'] !== '') {
$typeToken = $propertyInfo['type_end_token'];
$error = 'There must be 1 space after the property type declaration; %s found';
if ($tokens[($typeToken + 1)]['code'] !== T_WHITESPACE) {
$data = ['0'];
$fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data);
if ($fix === true) {
$phpcsFile->fixer->addContent($typeToken, ' ');
}
} elseif ($tokens[($typeToken + 1)]['content'] !== ' ') {
$next = $phpcsFile->findNext(T_WHITESPACE, ($typeToken + 1), null, true);
if ($tokens[$next]['line'] !== $tokens[$typeToken]['line']) {
$found = 'newline';
} else {
$found = $tokens[($typeToken + 1)]['length'];
}
$data = [$found];
$nextNonWs = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($typeToken + 1), null, true);
if ($nextNonWs !== $next) {
$phpcsFile->addError($error, $typeToken, 'SpacingAfterType', $data);
} else {
$fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data);
if ($fix === true) {
if ($found === 'newline') {
$phpcsFile->fixer->beginChangeset();
for ($x = ($typeToken + 1); $x < $next; $x++) {
$phpcsFile->fixer->replaceToken($x, '');
}
$phpcsFile->fixer->addContent($typeToken, ' ');
$phpcsFile->fixer->endChangeset();
} else {
$phpcsFile->fixer->replaceToken(($typeToken + 1), ' ');
}
}
}
}
}
if ($propertyInfo['scope_specified'] === false && $propertyInfo['set_scope'] === false) {
$error = 'Visibility must be declared on property "%s"';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
}
/*
* Note: per PSR-PER section 4.6 v 2.1/3.0, the order should be:
* - Inheritance modifier: `abstract` or `final`.
* - Visibility modifier: `public`, `protected`, or `private`.
* - Set-visibility modifier: `public(set)`, `protected(set)`, or `private(set)`
* - Scope modifier: `static`.
* - Mutation modifier: `readonly`.
* - Type declaration.
* - Name.
*
* Ref:
* - https://www.php-fig.org/per/coding-style/#46-modifier-keywords
* - https://github.com/php-fig/per-coding-style/pull/99
*
* The `static` and `readonly` modifiers are mutually exclusive and cannot be used together.
*
* Based on that, the below modifier keyword order checks are sufficient (for now).
*/
$hasVisibilityModifier = ($propertyInfo['scope_specified'] === true || $propertyInfo['set_scope'] !== false);
$lastVisibilityModifier = $phpcsFile->findPrevious(Tokens::SCOPE_MODIFIERS, ($stackPtr - 1));
$firstVisibilityModifier = $lastVisibilityModifier;
if ($propertyInfo['scope_specified'] === true && $propertyInfo['set_scope'] !== false) {
$scopePtr = $phpcsFile->findPrevious([T_PUBLIC, T_PROTECTED, T_PRIVATE], ($stackPtr - 1));
$setScopePtr = $phpcsFile->findPrevious([T_PUBLIC_SET, T_PROTECTED_SET, T_PRIVATE_SET], ($stackPtr - 1));
if ($scopePtr > $setScopePtr) {
$error = 'The "read"-visibility must come before the "write"-visibility';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'AvizKeywordOrder');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($scopePtr + 1); $scopePtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($scopePtr, '');
$phpcsFile->fixer->addContentBefore($setScopePtr, $tokens[$scopePtr]['content'] . ' ');
$phpcsFile->fixer->endChangeset();
}
}
$firstVisibilityModifier = min($scopePtr, $setScopePtr);
}
if ($hasVisibilityModifier === true && $propertyInfo['is_final'] === true) {
$scopePtr = $firstVisibilityModifier;
$finalPtr = $phpcsFile->findPrevious(T_FINAL, ($stackPtr - 1));
if ($finalPtr > $scopePtr) {
$error = 'The final declaration must come before the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'FinalAfterVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($finalPtr + 1); $finalPtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($finalPtr, '');
$phpcsFile->fixer->addContentBefore($scopePtr, $tokens[$finalPtr]['content'] . ' ');
$phpcsFile->fixer->endChangeset();
}
}
}
if ($hasVisibilityModifier === true && $propertyInfo['is_abstract'] === true) {
$scopePtr = $firstVisibilityModifier;
$abstractPtr = $phpcsFile->findPrevious(T_ABSTRACT, ($stackPtr - 1));
if ($abstractPtr > $scopePtr) {
$error = 'The abstract declaration must come before the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'AbstractAfterVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($abstractPtr + 1); $abstractPtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($abstractPtr, '');
$phpcsFile->fixer->addContentBefore($scopePtr, $tokens[$abstractPtr]['content'] . ' ');
$phpcsFile->fixer->endChangeset();
}
}
}
if ($hasVisibilityModifier === true && $propertyInfo['is_static'] === true) {
$scopePtr = $lastVisibilityModifier;
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));
if ($scopePtr > $staticPtr) {
$error = 'The static declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'StaticBeforeVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($staticPtr + 1); $staticPtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($staticPtr, '');
$phpcsFile->fixer->addContent($scopePtr, ' ' . $tokens[$staticPtr]['content']);
$phpcsFile->fixer->endChangeset();
}
}
}
if ($hasVisibilityModifier === true && $propertyInfo['is_readonly'] === true) {
$scopePtr = $lastVisibilityModifier;
$readonlyPtr = $phpcsFile->findPrevious(T_READONLY, ($stackPtr - 1));
if ($scopePtr > $readonlyPtr) {
$error = 'The readonly declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ReadonlyBeforeVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($readonlyPtr + 1); $readonlyPtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($readonlyPtr, '');
$phpcsFile->fixer->addContent($scopePtr, ' ' . $tokens[$readonlyPtr]['content']);
$phpcsFile->fixer->endChangeset();
}
}
}
}
/**
* Processes normal variables.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariable(File $phpcsFile, int $stackPtr)
{
// We don't care about normal variables.
}
/**
* Processes variables in double quoted strings.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariableInString(File $phpcsFile, int $stackPtr)
{
// We don't care about normal variables.
}
}