1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A helper class for fixing errors. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Greg Sherwood <[email protected]> |
10
|
|
|
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) |
11
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A helper class for fixing errors. |
17
|
|
|
* |
18
|
|
|
* Provides helper functions that act upon a token array and modify the file |
19
|
|
|
* content. |
20
|
|
|
* |
21
|
|
|
* @category PHP |
22
|
|
|
* @package PHP_CodeSniffer |
23
|
|
|
* @author Greg Sherwood <[email protected]> |
24
|
|
|
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) |
25
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
26
|
|
|
* @version Release: @package_version@ |
27
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
28
|
|
|
*/ |
29
|
|
|
class PHP_CodeSniffer_Fixer |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Is the fixer enabled and fixing a file? |
34
|
|
|
* |
35
|
|
|
* Sniffs should check this value to ensure they are not |
36
|
|
|
* doing extra processing to prepare for a fix when fixing is |
37
|
|
|
* not required. |
38
|
|
|
* |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
public $enabled = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The number of times we have looped over a file. |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
public $loops = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The file being fixed. |
52
|
|
|
* |
53
|
|
|
* @var PHP_CodeSniffer_File |
54
|
|
|
*/ |
55
|
|
|
private $_currentFile = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The list of tokens that make up the file contents. |
59
|
|
|
* |
60
|
|
|
* This is a simplified list which just contains the token content and nothing |
61
|
|
|
* else. This is the array that is updated as fixes are made, not the file's |
62
|
|
|
* token array. Imploding this array will give you the file content back. |
63
|
|
|
* |
64
|
|
|
* @var array(int => string) |
65
|
|
|
*/ |
66
|
|
|
private $_tokens = array(); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* A list of tokens that have already been fixed. |
70
|
|
|
* |
71
|
|
|
* We don't allow the same token to be fixed more than once each time |
72
|
|
|
* through a file as this can easily cause conflicts between sniffs. |
73
|
|
|
* |
74
|
|
|
* @var array(int) |
75
|
|
|
*/ |
76
|
|
|
private $_fixedTokens = array(); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The last value of each fixed token. |
80
|
|
|
* |
81
|
|
|
* If a token is being "fixed" back to its last value, the fix is |
82
|
|
|
* probably conflicting with another. |
83
|
|
|
* |
84
|
|
|
* @var array(int => string) |
85
|
|
|
*/ |
86
|
|
|
private $_oldTokenValues = array(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* A list of tokens that have been fixed during a changeset. |
90
|
|
|
* |
91
|
|
|
* All changes in changeset must be able to be applied, or else |
92
|
|
|
* the entire changeset is rejected. |
93
|
|
|
* |
94
|
|
|
* @var array() |
95
|
|
|
*/ |
96
|
|
|
private $_changeset = array(); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Is there an open changeset. |
100
|
|
|
* |
101
|
|
|
* @var boolean |
102
|
|
|
*/ |
103
|
|
|
private $_inChangeset = false; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Is the current fixing loop in conflict? |
107
|
|
|
* |
108
|
|
|
* @var boolean |
109
|
|
|
*/ |
110
|
|
|
private $_inConflict = false; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* The number of fixes that have been performed. |
114
|
|
|
* |
115
|
|
|
* @var int |
116
|
|
|
*/ |
117
|
|
|
private $_numFixes = 0; |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Starts fixing a new file. |
122
|
|
|
* |
123
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being fixed. |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function startFile($phpcsFile) |
128
|
|
|
{ |
129
|
|
|
$this->_currentFile = $phpcsFile; |
130
|
|
|
$this->_numFixes = 0; |
131
|
|
|
$this->_fixedTokens = array(); |
132
|
|
|
|
133
|
|
|
$tokens = $phpcsFile->getTokens(); |
134
|
|
|
$this->_tokens = array(); |
135
|
|
|
foreach ($tokens as $index => $token) { |
136
|
|
|
if (isset($token['orig_content']) === true) { |
137
|
|
|
$this->_tokens[$index] = $token['orig_content']; |
138
|
|
|
} else { |
139
|
|
|
$this->_tokens[$index] = $token['content']; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
}//end startFile() |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Attempt to fix the file by processing it until no fixes are made. |
148
|
|
|
* |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
|
|
public function fixFile() |
152
|
|
|
{ |
153
|
|
|
$fixable = $this->_currentFile->getFixableCount(); |
154
|
|
|
if ($fixable === 0) { |
155
|
|
|
// Nothing to fix. |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$stdin = false; |
160
|
|
|
$cliValues = $this->_currentFile->phpcs->cli->getCommandLineValues(); |
161
|
|
|
if (empty($cliValues['files']) === true) { |
162
|
|
|
$stdin = true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->enabled = true; |
166
|
|
|
|
167
|
|
|
$this->loops = 0; |
168
|
|
|
while ($this->loops < 50) { |
169
|
|
|
ob_start(); |
170
|
|
|
|
171
|
|
|
// Only needed once file content has changed. |
172
|
|
|
$contents = $this->getContents(); |
173
|
|
|
|
174
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 2) { |
175
|
|
|
@ob_end_clean(); |
|
|
|
|
176
|
|
|
echo '---START FILE CONTENT---'.PHP_EOL; |
177
|
|
|
$lines = explode($this->_currentFile->eolChar, $contents); |
178
|
|
|
$max = strlen(count($lines)); |
179
|
|
|
foreach ($lines as $lineNum => $line) { |
180
|
|
|
$lineNum++; |
181
|
|
|
echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
echo '--- END FILE CONTENT ---'.PHP_EOL; |
185
|
|
|
ob_start(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$this->_inConflict = false; |
189
|
|
|
$this->_currentFile->refreshTokenListeners(); |
190
|
|
|
$this->_currentFile->start($contents); |
191
|
|
|
ob_end_clean(); |
192
|
|
|
|
193
|
|
|
$this->loops++; |
194
|
|
|
|
195
|
|
|
if (PHP_CODESNIFFER_CBF === true && $stdin === false) { |
196
|
|
|
echo "\r".str_repeat(' ', 80)."\r"; |
197
|
|
|
echo "\t=> Fixing file: $this->_numFixes/$fixable violations remaining [made $this->loops pass"; |
198
|
|
|
if ($this->loops > 1) { |
199
|
|
|
echo 'es'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
echo ']... '; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ($this->_numFixes === 0 && $this->_inConflict === false) { |
206
|
|
|
// Nothing left to do. |
207
|
|
|
break; |
208
|
|
|
} else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
209
|
|
|
echo "\t* fixed $this->_numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
210
|
|
|
} |
211
|
|
|
}//end while |
212
|
|
|
|
213
|
|
|
$this->enabled = false; |
214
|
|
|
|
215
|
|
View Code Duplication |
if ($this->_numFixes > 0) { |
|
|
|
|
216
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
217
|
|
|
@ob_end_clean(); |
|
|
|
|
218
|
|
|
echo "\t*** Reached maximum number of loops with $this->_numFixes violations left unfixed ***".PHP_EOL; |
219
|
|
|
ob_start(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return true; |
226
|
|
|
|
227
|
|
|
}//end fixFile() |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Generates a text diff of the original file and the new content. |
232
|
|
|
* |
233
|
|
|
* @param string $filePath Optional file path to diff the file against. |
234
|
|
|
* If not specified, the original version of the |
235
|
|
|
* file will be used. |
236
|
|
|
* @param boolean $colors Print colored output or not. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function generateDiff($filePath=null, $colors=true) |
241
|
|
|
{ |
242
|
|
|
if ($filePath === null) { |
243
|
|
|
$filePath = $this->_currentFile->getFilename(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$cwd = getcwd().DIRECTORY_SEPARATOR; |
247
|
|
|
$filename = str_replace($cwd, '', $filePath); |
248
|
|
|
$contents = $this->getContents(); |
249
|
|
|
|
250
|
|
|
if (function_exists('sys_get_temp_dir') === true) { |
251
|
|
|
// This is needed for HHVM support, but only available from 5.2.1. |
252
|
|
|
$tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
253
|
|
|
$fixedFile = fopen($tempName, 'w'); |
254
|
|
|
} else { |
255
|
|
|
$fixedFile = tmpfile(); |
256
|
|
|
$data = stream_get_meta_data($fixedFile); |
257
|
|
|
$tempName = $data['uri']; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
fwrite($fixedFile, $contents); |
261
|
|
|
|
262
|
|
|
// We must use something like shell_exec() because whitespace at the end |
263
|
|
|
// of lines is critical to diff files. |
264
|
|
|
$cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\""; |
265
|
|
|
$diff = shell_exec($cmd); |
266
|
|
|
|
267
|
|
|
fclose($fixedFile); |
268
|
|
|
if (is_file($tempName) === true) { |
269
|
|
|
unlink($tempName); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($colors === false) { |
273
|
|
|
return $diff; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$diffLines = explode(PHP_EOL, $diff); |
277
|
|
|
if (count($diffLines) === 1) { |
278
|
|
|
// Seems to be required for cygwin. |
279
|
|
|
$diffLines = explode("\n", $diff); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$diff = array(); |
283
|
|
|
foreach ($diffLines as $line) { |
284
|
|
|
if (isset($line[0]) === true) { |
285
|
|
|
switch ($line[0]) { |
286
|
|
|
case '-': |
287
|
|
|
$diff[] = "\033[31m$line\033[0m"; |
288
|
|
|
break; |
289
|
|
|
case '+': |
290
|
|
|
$diff[] = "\033[32m$line\033[0m"; |
291
|
|
|
break; |
292
|
|
|
default: |
293
|
|
|
$diff[] = $line; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$diff = implode(PHP_EOL, $diff); |
299
|
|
|
|
300
|
|
|
return $diff; |
301
|
|
|
|
302
|
|
|
}//end generateDiff() |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get a count of fixes that have been performed on the file. |
307
|
|
|
* |
308
|
|
|
* This value is reset every time a new file is started, or an existing |
309
|
|
|
* file is restarted. |
310
|
|
|
* |
311
|
|
|
* @return int |
312
|
|
|
*/ |
313
|
|
|
public function getFixCount() |
314
|
|
|
{ |
315
|
|
|
return $this->_numFixes; |
316
|
|
|
|
317
|
|
|
}//end getFixCount() |
318
|
|
|
|
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get the current content of the file, as a string. |
322
|
|
|
* |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
|
|
public function getContents() |
326
|
|
|
{ |
327
|
|
|
$contents = implode($this->_tokens); |
328
|
|
|
return $contents; |
329
|
|
|
|
330
|
|
|
}//end getContents() |
331
|
|
|
|
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the current fixed content of a token. |
335
|
|
|
* |
336
|
|
|
* This function takes changesets into account so should be used |
337
|
|
|
* instead of directly accessing the token array. |
338
|
|
|
* |
339
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
340
|
|
|
* |
341
|
|
|
* @return string |
342
|
|
|
*/ |
343
|
|
|
public function getTokenContent($stackPtr) |
344
|
|
|
{ |
345
|
|
|
if ($this->_inChangeset === true |
346
|
|
|
&& isset($this->_changeset[$stackPtr]) === true |
347
|
|
|
) { |
348
|
|
|
return $this->_changeset[$stackPtr]; |
349
|
|
|
} else { |
350
|
|
|
return $this->_tokens[$stackPtr]; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
}//end getTokenContent() |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Start recording actions for a changeset. |
358
|
|
|
* |
359
|
|
|
* @return void |
360
|
|
|
*/ |
361
|
|
|
public function beginChangeset() |
362
|
|
|
{ |
363
|
|
|
if ($this->_inConflict === true) { |
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
368
|
|
|
$bt = debug_backtrace(); |
369
|
|
|
$sniff = $bt[1]['class']; |
370
|
|
|
$line = $bt[0]['line']; |
371
|
|
|
|
372
|
|
|
@ob_end_clean(); |
|
|
|
|
373
|
|
|
echo "\t=> Changeset started by $sniff (line $line)".PHP_EOL; |
374
|
|
|
ob_start(); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$this->_changeset = array(); |
378
|
|
|
$this->_inChangeset = true; |
379
|
|
|
|
380
|
|
|
}//end beginChangeset() |
381
|
|
|
|
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Stop recording actions for a changeset, and apply logged changes. |
385
|
|
|
* |
386
|
|
|
* @return boolean |
387
|
|
|
*/ |
388
|
|
|
public function endChangeset() |
389
|
|
|
{ |
390
|
|
|
if ($this->_inConflict === true) { |
391
|
|
|
return false; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$this->_inChangeset = false; |
395
|
|
|
|
396
|
|
|
$success = true; |
397
|
|
|
$applied = array(); |
398
|
|
|
foreach ($this->_changeset as $stackPtr => $content) { |
399
|
|
|
$success = $this->replaceToken($stackPtr, $content); |
400
|
|
|
if ($success === false) { |
401
|
|
|
break; |
402
|
|
|
} else { |
403
|
|
|
$applied[] = $stackPtr; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
if ($success === false) { |
408
|
|
|
// Rolling back all changes. |
409
|
|
|
foreach ($applied as $stackPtr) { |
410
|
|
|
$this->revertToken($stackPtr); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
414
|
|
|
@ob_end_clean(); |
|
|
|
|
415
|
|
|
echo "\t=> Changeset failed to apply".PHP_EOL; |
416
|
|
|
ob_start(); |
417
|
|
|
} |
418
|
|
View Code Duplication |
} else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
|
|
|
419
|
|
|
$fixes = count($this->_changeset); |
420
|
|
|
@ob_end_clean(); |
|
|
|
|
421
|
|
|
echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
422
|
|
|
ob_start(); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$this->_changeset = array(); |
426
|
|
|
|
427
|
|
|
}//end endChangeset() |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Stop recording actions for a changeset, and discard logged changes. |
432
|
|
|
* |
433
|
|
|
* @return void |
434
|
|
|
*/ |
435
|
|
|
public function rollbackChangeset() |
436
|
|
|
{ |
437
|
|
|
$this->_inChangeset = false; |
438
|
|
|
$this->_inConflict = false; |
439
|
|
|
|
440
|
|
|
if (empty($this->_changeset) === false) { |
441
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
442
|
|
|
$bt = debug_backtrace(); |
443
|
|
|
if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
444
|
|
|
$sniff = $bt[2]['class']; |
445
|
|
|
$line = $bt[1]['line']; |
446
|
|
|
} else { |
447
|
|
|
$sniff = $bt[1]['class']; |
448
|
|
|
$line = $bt[0]['line']; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$numChanges = count($this->_changeset); |
452
|
|
|
|
453
|
|
|
@ob_end_clean(); |
|
|
|
|
454
|
|
|
echo "\t\tR: $sniff (line $line) rolled back the changeset ($numChanges changes)".PHP_EOL; |
455
|
|
|
echo "\t=> Changeset rolled back".PHP_EOL; |
456
|
|
|
ob_start(); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
$this->_changeset = array(); |
460
|
|
|
}//end if |
461
|
|
|
|
462
|
|
|
}//end rollbackChangeset() |
463
|
|
|
|
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Replace the entire contents of a token. |
467
|
|
|
* |
468
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
469
|
|
|
* @param string $content The new content of the token. |
470
|
|
|
* |
471
|
|
|
* @return bool If the change was accepted. |
472
|
|
|
*/ |
473
|
|
|
public function replaceToken($stackPtr, $content) |
474
|
|
|
{ |
475
|
|
|
if ($this->_inConflict === true) { |
476
|
|
|
return false; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
if ($this->_inChangeset === false |
480
|
|
|
&& isset($this->_fixedTokens[$stackPtr]) === true |
481
|
|
|
) { |
482
|
|
|
$indent = "\t"; |
483
|
|
|
if (empty($this->_changeset) === false) { |
484
|
|
|
$indent .= "\t"; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
488
|
|
|
@ob_end_clean(); |
|
|
|
|
489
|
|
|
echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
490
|
|
|
ob_start(); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
return false; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
View Code Duplication |
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
|
|
|
497
|
|
|
$bt = debug_backtrace(); |
498
|
|
|
if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
499
|
|
|
$sniff = $bt[2]['class']; |
500
|
|
|
$line = $bt[1]['line']; |
501
|
|
|
} else { |
502
|
|
|
$sniff = $bt[1]['class']; |
503
|
|
|
$line = $bt[0]['line']; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$tokens = $this->_currentFile->getTokens(); |
507
|
|
|
$type = $tokens[$stackPtr]['type']; |
508
|
|
|
$oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
509
|
|
|
$newContent = PHP_CodeSniffer::prepareForOutput($content); |
510
|
|
|
if (trim($this->_tokens[$stackPtr]) === '' && isset($this->_tokens[($stackPtr + 1)]) === true) { |
511
|
|
|
// Add some context for whitespace only changes. |
512
|
|
|
$append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
513
|
|
|
$oldContent .= $append; |
514
|
|
|
$newContent .= $append; |
515
|
|
|
} |
516
|
|
|
}//end if |
517
|
|
|
|
518
|
|
|
if ($this->_inChangeset === true) { |
519
|
|
|
$this->_changeset[$stackPtr] = $content; |
520
|
|
|
|
521
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
522
|
|
|
@ob_end_clean(); |
|
|
|
|
523
|
|
|
echo "\t\tQ: $sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
|
|
|
524
|
|
|
ob_start(); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
return true; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
if (isset($this->_oldTokenValues[$stackPtr]) === false) { |
531
|
|
|
$this->_oldTokenValues[$stackPtr] = array( |
532
|
|
|
'curr' => $content, |
533
|
|
|
'prev' => $this->_tokens[$stackPtr], |
534
|
|
|
'loop' => $this->loops, |
535
|
|
|
); |
536
|
|
|
} else { |
537
|
|
|
if ($this->_oldTokenValues[$stackPtr]['prev'] === $content |
538
|
|
|
&& $this->_oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
539
|
|
|
) { |
540
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
541
|
|
|
$indent = "\t"; |
542
|
|
|
if (empty($this->_changeset) === false) { |
543
|
|
|
$indent .= "\t"; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
$loop = $this->_oldTokenValues[$stackPtr]['loop']; |
547
|
|
|
|
548
|
|
|
@ob_end_clean(); |
|
|
|
|
549
|
|
|
echo "$indent**** $sniff (line $line) has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
550
|
|
|
echo "$indent**** replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
if ($this->_oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
554
|
|
|
$this->_inConflict = true; |
555
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
556
|
|
|
echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
|
|
|
557
|
|
|
} |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
561
|
|
|
ob_start(); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
return false; |
565
|
|
|
}//end if |
566
|
|
|
|
567
|
|
|
$this->_oldTokenValues[$stackPtr]['prev'] = $this->_oldTokenValues[$stackPtr]['curr']; |
568
|
|
|
$this->_oldTokenValues[$stackPtr]['curr'] = $content; |
569
|
|
|
$this->_oldTokenValues[$stackPtr]['loop'] = $this->loops; |
570
|
|
|
}//end if |
571
|
|
|
|
572
|
|
|
$this->_fixedTokens[$stackPtr] = $this->_tokens[$stackPtr]; |
573
|
|
|
$this->_tokens[$stackPtr] = $content; |
574
|
|
|
$this->_numFixes++; |
575
|
|
|
|
576
|
|
View Code Duplication |
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
|
|
|
577
|
|
|
$indent = "\t"; |
578
|
|
|
if (empty($this->_changeset) === false) { |
579
|
|
|
$indent .= "\tA: "; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
@ob_end_clean(); |
|
|
|
|
583
|
|
|
echo "$indent$sniff (line $line) replaced token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
584
|
|
|
ob_start(); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
return true; |
588
|
|
|
|
589
|
|
|
}//end replaceToken() |
590
|
|
|
|
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Reverts the previous fix made to a token. |
594
|
|
|
* |
595
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
596
|
|
|
* |
597
|
|
|
* @return bool If a change was reverted. |
598
|
|
|
*/ |
599
|
|
|
public function revertToken($stackPtr) |
600
|
|
|
{ |
601
|
|
|
if (isset($this->_fixedTokens[$stackPtr]) === false) { |
602
|
|
|
return false; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
View Code Duplication |
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
|
|
|
606
|
|
|
$bt = debug_backtrace(); |
607
|
|
|
if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') { |
608
|
|
|
$sniff = $bt[2]['class']; |
609
|
|
|
$line = $bt[1]['line']; |
610
|
|
|
} else { |
611
|
|
|
$sniff = $bt[1]['class']; |
612
|
|
|
$line = $bt[0]['line']; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
$tokens = $this->_currentFile->getTokens(); |
616
|
|
|
$type = $tokens[$stackPtr]['type']; |
617
|
|
|
$oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]); |
618
|
|
|
$newContent = PHP_CodeSniffer::prepareForOutput($this->_fixedTokens[$stackPtr]); |
619
|
|
|
if (trim($this->_tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
620
|
|
|
// Add some context for whitespace only changes. |
621
|
|
|
$append = PHP_CodeSniffer::prepareForOutput($this->_tokens[($stackPtr + 1)]); |
622
|
|
|
$oldContent .= $append; |
623
|
|
|
$newContent .= $append; |
624
|
|
|
} |
625
|
|
|
}//end if |
626
|
|
|
|
627
|
|
|
$this->_tokens[$stackPtr] = $this->_fixedTokens[$stackPtr]; |
628
|
|
|
unset($this->_fixedTokens[$stackPtr]); |
629
|
|
|
$this->_numFixes--; |
630
|
|
|
|
631
|
|
View Code Duplication |
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
|
|
|
632
|
|
|
$indent = "\t"; |
633
|
|
|
if (empty($this->_changeset) === false) { |
634
|
|
|
$indent .= "\tR: "; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
@ob_end_clean(); |
|
|
|
|
638
|
|
|
echo "$indent$sniff (line $line) reverted token $stackPtr ($type) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
|
|
|
639
|
|
|
ob_start(); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
return true; |
643
|
|
|
|
644
|
|
|
}//end revertToken() |
645
|
|
|
|
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Replace the content of a token with a part of its current content. |
649
|
|
|
* |
650
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
651
|
|
|
* @param int $start The first character to keep. |
652
|
|
|
* @param int $length The number of chacters to keep. If NULL, the content of |
653
|
|
|
* the token from $start to the end of the content is kept. |
654
|
|
|
* |
655
|
|
|
* @return bool If the change was accepted. |
656
|
|
|
*/ |
657
|
|
|
public function substrToken($stackPtr, $start, $length=null) |
658
|
|
|
{ |
659
|
|
|
$current = $this->getTokenContent($stackPtr); |
660
|
|
|
|
661
|
|
|
if ($length === null) { |
662
|
|
|
$newContent = substr($current, $start); |
663
|
|
|
} else { |
664
|
|
|
$newContent = substr($current, $start, $length); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
return $this->replaceToken($stackPtr, $newContent); |
668
|
|
|
|
669
|
|
|
}//end substrToken() |
670
|
|
|
|
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* Adds a newline to end of a token's content. |
674
|
|
|
* |
675
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
676
|
|
|
* |
677
|
|
|
* @return bool If the change was accepted. |
678
|
|
|
*/ |
679
|
|
|
public function addNewline($stackPtr) |
680
|
|
|
{ |
681
|
|
|
$current = $this->getTokenContent($stackPtr); |
682
|
|
|
return $this->replaceToken($stackPtr, $current.$this->_currentFile->eolChar); |
683
|
|
|
|
684
|
|
|
}//end addNewline() |
685
|
|
|
|
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Adds a newline to the start of a token's content. |
689
|
|
|
* |
690
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
691
|
|
|
* |
692
|
|
|
* @return bool If the change was accepted. |
693
|
|
|
*/ |
694
|
|
|
public function addNewlineBefore($stackPtr) |
695
|
|
|
{ |
696
|
|
|
$current = $this->getTokenContent($stackPtr); |
697
|
|
|
return $this->replaceToken($stackPtr, $this->_currentFile->eolChar.$current); |
698
|
|
|
|
699
|
|
|
}//end addNewlineBefore() |
700
|
|
|
|
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* Adds content to the end of a token's current content. |
704
|
|
|
* |
705
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
706
|
|
|
* @param string $content The content to add. |
707
|
|
|
* |
708
|
|
|
* @return bool If the change was accepted. |
709
|
|
|
*/ |
710
|
|
|
public function addContent($stackPtr, $content) |
711
|
|
|
{ |
712
|
|
|
$current = $this->getTokenContent($stackPtr); |
713
|
|
|
return $this->replaceToken($stackPtr, $current.$content); |
714
|
|
|
|
715
|
|
|
}//end addContent() |
716
|
|
|
|
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Adds content to the start of a token's current content. |
720
|
|
|
* |
721
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
722
|
|
|
* @param string $content The content to add. |
723
|
|
|
* |
724
|
|
|
* @return bool If the change was accepted. |
725
|
|
|
*/ |
726
|
|
|
public function addContentBefore($stackPtr, $content) |
727
|
|
|
{ |
728
|
|
|
$current = $this->getTokenContent($stackPtr); |
729
|
|
|
return $this->replaceToken($stackPtr, $content.$current); |
730
|
|
|
|
731
|
|
|
}//end addContentBefore() |
732
|
|
|
|
733
|
|
|
|
734
|
|
|
}//end class |
735
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.