1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Stores the configuration used to run PHPCS and PHPCBF. |
4
|
|
|
* |
5
|
|
|
* Parses the command line to determine user supplied values |
6
|
|
|
* and provides functions to access data stored in config files. |
7
|
|
|
* |
8
|
|
|
* @author Greg Sherwood <[email protected]> |
9
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
10
|
|
|
* @license https://github.com/squizlabs/Symplify\PHP7_CodeSniffer/blob/master/licence.txt BSD Licence |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Symplify\PHP7_CodeSniffer; |
14
|
|
|
|
15
|
|
|
use Symplify\PHP7_CodeSniffer\Exceptions\RuntimeException; |
16
|
|
|
|
17
|
|
|
class Config |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The current version. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const VERSION = '3.0.0'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Package stability; either stable, beta or alpha. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const STABILITY = 'alpha'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* An array of settings that PHPCS and PHPCBF accept. |
36
|
|
|
* |
37
|
|
|
* This array is not meant to be accessed directly. Instead, use the settings |
38
|
|
|
* as if they are class member vars so the __get() and __set() magic methods |
39
|
|
|
* can be used to validate the values. For example, to set the verbosity level to |
40
|
|
|
* level 2, use $this->verbosity = 2; insteas of accessing this property directly. |
41
|
|
|
* |
42
|
|
|
* The list of settings are: |
43
|
|
|
* |
44
|
|
|
* string[] files The files and directories to check. |
45
|
|
|
* string[] standards The standards being used for checking. |
46
|
|
|
* int verbosity How verbose the output should be. |
47
|
|
|
* 0: no unnecessary output |
48
|
|
|
* 1: basic output for files being checked |
49
|
|
|
* 2: ruleset and file parsing output |
50
|
|
|
* 3: sniff execution output |
51
|
|
|
* bool interactive Enable interactive checking mode. |
52
|
|
|
* bool parallel Check files in parallel. |
53
|
|
|
* bool cache Enable the use of the file cache. |
54
|
|
|
* bool cacheFile A file where the cache data should be written |
55
|
|
|
* bool colors Display colours in output. |
56
|
|
|
* bool explain Explain the coding standards. |
57
|
|
|
* bool local Process local files in directories only (no recursion). |
58
|
|
|
* bool showSources Show sniff source codes in report output. |
59
|
|
|
* bool showProgress Show basic progress information while running. |
60
|
|
|
* int tabWidth How many spaces each tab is worth. |
61
|
|
|
* string encoding The encoding of the files being checked. |
62
|
|
|
* string[] sniffs The sniffs that should be used for checking. |
63
|
|
|
* If empty, all sniffs in the supplied standards will be used. |
64
|
|
|
* string[] ignored Regular expressions used to ignore files and folders during checking. |
65
|
|
|
* string reportFile A file where the report output should be written. |
66
|
|
|
* string generator The documentation generator to use. |
67
|
|
|
* string filter The filter to use for the run. |
68
|
|
|
* string[] bootstrap One of more files to include before the run begins. |
69
|
|
|
* int reportWidth The maximum number of columns that reports should use for output. |
70
|
|
|
* Set to "auto" for have this value changed to the width of the terminal. |
71
|
|
|
* int errorSeverity The minimum severity an error must have to be displayed. |
72
|
|
|
* int warningSeverity The minimum severity a warning must have to be displayed. |
73
|
|
|
* bool recordErrors Record the content of error messages as well as error counts. |
74
|
|
|
* string suffix A suffix to add to fixed files. |
75
|
|
|
* string basepath A file system location to strip from the paths of files shown in reports. |
76
|
|
|
* bool stdin Read content from STDIN instead of supplied files. |
77
|
|
|
* string stdinContent Content passed directly to PHPCS on STDIN. |
78
|
|
|
* string stdinPath The path to use for content passed on STDIN. |
79
|
|
|
* |
80
|
|
|
* array<string, string> extensions File extensions that should be checked, and what tokenizer to use. |
81
|
|
|
* E.g., array('inc' => 'PHP'); |
82
|
|
|
* array<string, string|null> reports The reports to use for printing output after the run. |
83
|
|
|
* The format of the array is: |
84
|
|
|
* array( |
85
|
|
|
* 'reportName1' => 'outputFile', |
86
|
|
|
* 'reportName2' => null, |
87
|
|
|
* ); |
88
|
|
|
* If the array value is NULL, the report will be written to the screen. |
89
|
|
|
* |
90
|
|
|
* @var array<string, mixed> |
91
|
|
|
*/ |
92
|
|
|
private $settings = array( |
93
|
|
|
'files' => null, |
94
|
|
|
'standards' => null, |
95
|
|
|
'verbosity' => null, |
96
|
|
|
'interactive' => null, |
97
|
|
|
'parallel' => null, |
98
|
|
|
'cache' => null, |
99
|
|
|
'cacheFile' => null, |
100
|
|
|
'colors' => null, |
101
|
|
|
'explain' => null, |
102
|
|
|
'local' => null, |
103
|
|
|
'showSources' => null, |
104
|
|
|
'showProgress' => null, |
105
|
|
|
'tabWidth' => null, |
106
|
|
|
'encoding' => null, |
107
|
|
|
'extensions' => null, |
108
|
|
|
'sniffs' => null, |
109
|
|
|
'ignored' => null, |
110
|
|
|
'reportFile' => null, |
111
|
|
|
'generator' => null, |
112
|
|
|
'filter' => null, |
113
|
|
|
'bootstrap' => null, |
114
|
|
|
'reports' => null, |
115
|
|
|
'basepath' => null, |
116
|
|
|
'reportWidth' => null, |
117
|
|
|
'errorSeverity' => null, |
118
|
|
|
'warningSeverity' => null, |
119
|
|
|
'recordErrors' => null, |
120
|
|
|
'suffix' => null, |
121
|
|
|
'stdin' => null, |
122
|
|
|
'stdinContent' => null, |
123
|
|
|
'stdinPath' => null, |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Whether or not to kill the process when an unknown command line arg is found. |
128
|
|
|
* |
129
|
|
|
* If FALSE, arguments that are not command line options or file/directory paths |
130
|
|
|
* will be ignored and execution will continue. |
131
|
|
|
* |
132
|
|
|
* @var boolean |
133
|
|
|
*/ |
134
|
|
|
public $dieOnUnknownArg; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* The current command line arguments we are processing. |
138
|
|
|
* |
139
|
|
|
* @var string[] |
140
|
|
|
*/ |
141
|
|
|
private $cliArgs = array(); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Command line values that the user has supplied directly. |
145
|
|
|
* |
146
|
|
|
* @var array<string, TRUE> |
147
|
|
|
*/ |
148
|
|
|
private $overriddenDefaults = array(); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Unknown arguments |
152
|
|
|
* |
153
|
|
|
* @var array<mixed> |
154
|
|
|
*/ |
155
|
|
|
private $values = array(); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Config file data that has been loaded for the run. |
159
|
|
|
* |
160
|
|
|
* @var array<string, string> |
161
|
|
|
*/ |
162
|
|
|
private static $configData = null; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Automatically discovered executable utility paths. |
166
|
|
|
* |
167
|
|
|
* @var array<string, string> |
168
|
|
|
*/ |
169
|
|
|
private static $executablePaths = array(); |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the value of an inaccessible property. |
174
|
|
|
* |
175
|
|
|
* @param string $name The name of the property. |
176
|
|
|
* |
177
|
|
|
* @return mixed |
178
|
|
|
* @throws RuntimeException If the setting name is invalid. |
179
|
|
|
*/ |
180
|
|
|
public function __get($name) |
181
|
|
|
{ |
182
|
|
|
if (array_key_exists($name, $this->settings) === false) { |
183
|
|
|
throw new RuntimeException("ERROR: unable to get value of property \"$name\""); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->settings[$name]; |
187
|
|
|
|
188
|
|
|
}//end __get() |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Set the value of an inaccessible property. |
193
|
|
|
* |
194
|
|
|
* @param string $name The name of the property. |
195
|
|
|
* @param mixed $value The value of the property. |
196
|
|
|
* |
197
|
|
|
* @return void |
198
|
|
|
* @throws RuntimeException If the setting name is invalid. |
199
|
|
|
*/ |
200
|
|
|
public function __set($name, $value) |
201
|
|
|
{ |
202
|
|
|
if (array_key_exists($name, $this->settings) === false) { |
203
|
|
|
throw new RuntimeException("Can't __set() $name; setting doesn't exist"); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
switch ($name) { |
207
|
|
|
case 'reportWidth' : |
|
|
|
|
208
|
|
|
// Support auto terminal width. |
209
|
|
|
if ($value === 'auto' && preg_match('|\d+ (\d+)|', shell_exec('stty size 2>&1'), $matches) === 1) { |
210
|
|
|
$value = (int) $matches[1]; |
211
|
|
|
} else { |
212
|
|
|
$value = (int) $value; |
213
|
|
|
} |
214
|
|
|
break; |
215
|
|
|
case 'standards' : |
|
|
|
|
216
|
|
|
$cleaned = array(); |
217
|
|
|
|
218
|
|
|
// Check if the standard name is valid, or if the case is invalid. |
219
|
|
|
$installedStandards = Util\Standards::getInstalledStandards(); |
220
|
|
|
foreach ($value as $standard) { |
221
|
|
|
foreach ($installedStandards as $validStandard) { |
222
|
|
|
if (strtolower($standard) === strtolower($validStandard)) { |
223
|
|
|
$standard = $validStandard; |
224
|
|
|
break; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$cleaned[] = $standard; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$value = $cleaned; |
232
|
|
|
break; |
233
|
|
|
default : |
|
|
|
|
234
|
|
|
// No validation required. |
235
|
|
|
break; |
236
|
|
|
}//end switch |
237
|
|
|
|
238
|
|
|
$this->settings[$name] = $value; |
239
|
|
|
|
240
|
|
|
}//end __set() |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Check if the value of an inaccessible property is set. |
245
|
|
|
* |
246
|
|
|
* @param string $name The name of the property. |
247
|
|
|
* |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function __isset($name) |
251
|
|
|
{ |
252
|
|
|
return isset($this->settings[$name]); |
253
|
|
|
|
254
|
|
|
}//end __isset() |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Unset the value of an inaccessible property. |
259
|
|
|
* |
260
|
|
|
* @param string $name The name of the property. |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
public function __unset($name) |
265
|
|
|
{ |
266
|
|
|
$this->settings[$name] = null; |
267
|
|
|
|
268
|
|
|
}//end __unset() |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Creates a Config object and populates it with command line values. |
273
|
|
|
* |
274
|
|
|
* @param array $cliArgs An array of values gathered from CLI args. |
275
|
|
|
* @param bool $dieOnUnknownArg Whether or not to kill the process when an |
276
|
|
|
* unknown command line arg is found. |
277
|
|
|
* |
278
|
|
|
* @return void |
|
|
|
|
279
|
|
|
*/ |
280
|
|
|
public function __construct(array $cliArgs=array(), $dieOnUnknownArg=true) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === true) { |
283
|
|
|
// Let everything through during testing so that we can |
284
|
|
|
// make use of PHPUnit command line arguments as well. |
285
|
|
|
$this->dieOnUnknownArg = false; |
286
|
|
|
} else { |
287
|
|
|
$this->dieOnUnknownArg = $dieOnUnknownArg; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$checkStdin = false; |
291
|
|
|
if (empty($cliArgs) === true) { |
292
|
|
|
$cliArgs = $_SERVER['argv']; |
293
|
|
|
array_shift($cliArgs); |
294
|
|
|
$checkStdin = true; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$this->restoreDefaults(); |
298
|
|
|
$this->setCommandLineValues($cliArgs); |
299
|
|
|
|
300
|
|
|
if (isset($this->overriddenDefaults['standards']) === false |
301
|
|
|
&& Config::getConfigData('default_standard') === null |
302
|
|
|
) { |
303
|
|
|
// They did not supply a standard to use. |
304
|
|
|
// Look for a default ruleset in the current directory or higher. |
305
|
|
|
$currentDir = getcwd(); |
306
|
|
|
|
307
|
|
|
do { |
308
|
|
|
$default = $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml'; |
309
|
|
|
if (is_file($default) === true) { |
310
|
|
|
$this->standards = array($default); |
|
|
|
|
311
|
|
|
} else { |
312
|
|
|
$default = $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml.dist'; |
313
|
|
|
if (is_file($default) === true) { |
314
|
|
|
$this->standards = array($default); |
|
|
|
|
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
$lastDir = $currentDir; |
319
|
|
|
$currentDir = dirname($currentDir); |
320
|
|
|
} while ($currentDir !== '.' && $currentDir !== $lastDir); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
// Check for content on STDIN. |
324
|
|
|
if ($checkStdin === true) { |
325
|
|
|
$handle = fopen('php://stdin', 'r'); |
326
|
|
|
if (stream_set_blocking($handle, false) === true) { |
327
|
|
|
$fileContents = ''; |
328
|
|
|
while (($line = fgets(STDIN)) !== false) { |
329
|
|
|
$fileContents .= $line; |
330
|
|
|
usleep(10); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
stream_set_blocking($handle, true); |
334
|
|
|
fclose($handle); |
335
|
|
|
if (trim($fileContents) !== '') { |
336
|
|
|
$this->stdin = true; |
|
|
|
|
337
|
|
|
$this->stdinContent = $fileContents; |
|
|
|
|
338
|
|
|
$this->overriddenDefaults['stdin'] = true; |
339
|
|
|
$this->overriddenDefaults['stdinContent'] = true; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
}//end __construct() |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Set the command line values. |
349
|
|
|
* |
350
|
|
|
* @param array $args An array of command line arguments to set. |
351
|
|
|
* |
352
|
|
|
* @return void |
353
|
|
|
*/ |
354
|
|
|
public function setCommandLineValues($args) |
355
|
|
|
{ |
356
|
|
|
$this->cliArgs = $args; |
357
|
|
|
$numArgs = count($args); |
358
|
|
|
|
359
|
|
|
for ($i = 0; $i < $numArgs; $i++) { |
360
|
|
|
$arg = $this->cliArgs[$i]; |
361
|
|
|
if ($arg === '') { |
362
|
|
|
continue; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if ($arg{0} === '-') { |
366
|
|
|
if ($arg === '-') { |
367
|
|
|
// Asking to read from STDIN. |
368
|
|
|
$this->stdin = true; |
|
|
|
|
369
|
|
|
$this->overriddenDefaults['stdin'] = true; |
370
|
|
|
continue; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if ($arg === '--') { |
374
|
|
|
// Empty argument, ignore it. |
375
|
|
|
continue; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
if ($arg{1} === '-') { |
379
|
|
|
$this->processLongArgument(substr($arg, 2), $i); |
380
|
|
|
} else { |
381
|
|
|
$switches = str_split($arg); |
382
|
|
|
foreach ($switches as $switch) { |
383
|
|
|
if ($switch === '-') { |
384
|
|
|
continue; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$this->processShortArgument($switch, $i); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} else { |
391
|
|
|
$this->processUnknownArgument($arg, $i); |
392
|
|
|
}//end if |
393
|
|
|
}//end for |
394
|
|
|
|
395
|
|
|
}//end setCommandLineValues() |
396
|
|
|
|
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Restore default values for all possible command line arguments. |
400
|
|
|
* |
401
|
|
|
* @return array |
402
|
|
|
*/ |
403
|
|
|
public function restoreDefaults() |
404
|
|
|
{ |
405
|
|
|
$this->files = array(); |
|
|
|
|
406
|
|
|
$this->standards = array('PEAR'); |
|
|
|
|
407
|
|
|
$this->verbosity = 0; |
|
|
|
|
408
|
|
|
$this->interactive = false; |
|
|
|
|
409
|
|
|
$this->cache = false; |
|
|
|
|
410
|
|
|
$this->cacheFile = null; |
|
|
|
|
411
|
|
|
$this->colors = false; |
|
|
|
|
412
|
|
|
$this->explain = false; |
|
|
|
|
413
|
|
|
$this->local = false; |
|
|
|
|
414
|
|
|
$this->showSources = false; |
|
|
|
|
415
|
|
|
$this->showProgress = false; |
|
|
|
|
416
|
|
|
$this->parallel = 1; |
|
|
|
|
417
|
|
|
$this->tabWidth = 0; |
|
|
|
|
418
|
|
|
$this->encoding = 'utf-8'; |
|
|
|
|
419
|
|
|
$this->extensions = array( |
|
|
|
|
420
|
|
|
'php' => 'PHP', |
421
|
|
|
'inc' => 'PHP', |
422
|
|
|
'js' => 'JS', |
423
|
|
|
'css' => 'CSS', |
424
|
|
|
); |
425
|
|
|
$this->sniffs = array(); |
|
|
|
|
426
|
|
|
$this->ignored = array(); |
|
|
|
|
427
|
|
|
$this->reportFile = null; |
|
|
|
|
428
|
|
|
$this->generator = null; |
|
|
|
|
429
|
|
|
$this->filter = null; |
|
|
|
|
430
|
|
|
$this->bootstrap = array(); |
|
|
|
|
431
|
|
|
$this->reports = array('full' => null); |
|
|
|
|
432
|
|
|
$this->reportWidth = 'auto'; |
|
|
|
|
433
|
|
|
$this->errorSeverity = 5; |
|
|
|
|
434
|
|
|
$this->warningSeverity = 5; |
|
|
|
|
435
|
|
|
$this->recordErrors = true; |
|
|
|
|
436
|
|
|
$this->suffix = ''; |
|
|
|
|
437
|
|
|
$this->stdin = false; |
|
|
|
|
438
|
|
|
$this->stdinContent = null; |
|
|
|
|
439
|
|
|
$this->stdinPath = null; |
|
|
|
|
440
|
|
|
|
441
|
|
|
$standard = self::getConfigData('default_standard'); |
442
|
|
|
if ($standard !== null) { |
443
|
|
|
$this->standards = explode(',', $standard); |
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
$reportFormat = self::getConfigData('report_format'); |
447
|
|
|
if ($reportFormat !== null) { |
448
|
|
|
$this->reports = array($reportFormat => null); |
|
|
|
|
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$tabWidth = self::getConfigData('tab_width'); |
452
|
|
|
if ($tabWidth !== null) { |
453
|
|
|
$this->tabWidth = (int) $tabWidth; |
|
|
|
|
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
$encoding = self::getConfigData('encoding'); |
457
|
|
|
if ($encoding !== null) { |
458
|
|
|
$this->encoding = strtolower($encoding); |
|
|
|
|
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
$severity = self::getConfigData('severity'); |
462
|
|
|
if ($severity !== null) { |
463
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
464
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
$severity = self::getConfigData('error_severity'); |
468
|
|
|
if ($severity !== null) { |
469
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
$severity = self::getConfigData('warning_severity'); |
473
|
|
|
if ($severity !== null) { |
474
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
$showWarnings = self::getConfigData('show_warnings'); |
478
|
|
|
if ($showWarnings !== null) { |
479
|
|
|
$showWarnings = (bool) $showWarnings; |
480
|
|
|
if ($showWarnings === false) { |
481
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
$reportWidth = self::getConfigData('report_width'); |
486
|
|
|
if ($reportWidth !== null) { |
487
|
|
|
$this->reportWidth = $reportWidth; |
|
|
|
|
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$showProgress = self::getConfigData('show_progress'); |
491
|
|
|
if ($showProgress !== null) { |
492
|
|
|
$this->showProgress = (bool) $showProgress; |
|
|
|
|
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
$colors = self::getConfigData('colors'); |
496
|
|
|
if ($colors !== null) { |
497
|
|
|
$this->colors = (bool) $colors; |
|
|
|
|
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
501
|
|
|
$cache = self::getConfigData('cache'); |
502
|
|
|
if ($cache !== null) { |
503
|
|
|
$this->cache = (bool) $cache; |
|
|
|
|
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$parallel = self::getConfigData('parallel'); |
507
|
|
|
if ($parallel !== null) { |
508
|
|
|
$this->parallel = max((int) $parallel, 1); |
|
|
|
|
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
}//end restoreDefaults() |
513
|
|
|
|
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Processes a short (-e) command line argument. |
517
|
|
|
* |
518
|
|
|
* @param string $arg The command line argument. |
519
|
|
|
* @param int $pos The position of the argument on the command line. |
520
|
|
|
* |
521
|
|
|
* @return void |
522
|
|
|
*/ |
523
|
|
|
public function processShortArgument($arg, $pos) |
524
|
|
|
{ |
525
|
|
|
switch ($arg) { |
526
|
|
|
case 'h': |
527
|
|
|
case '?': |
528
|
|
|
$this->printUsage(); |
529
|
|
|
exit(0); |
|
|
|
|
530
|
|
|
case 'i' : |
|
|
|
|
531
|
|
|
Util\Standards::printInstalledStandards(); |
532
|
|
|
exit(0); |
|
|
|
|
533
|
|
|
case 'v' : |
|
|
|
|
534
|
|
|
$this->verbosity++; |
|
|
|
|
535
|
|
|
$this->overriddenDefaults['verbosity'] = true; |
536
|
|
|
break; |
537
|
|
|
case 'l' : |
|
|
|
|
538
|
|
|
$this->local = true; |
|
|
|
|
539
|
|
|
$this->overriddenDefaults['local'] = true; |
540
|
|
|
break; |
541
|
|
|
case 's' : |
|
|
|
|
542
|
|
|
$this->showSources = true; |
|
|
|
|
543
|
|
|
$this->overriddenDefaults['showSources'] = true; |
544
|
|
|
break; |
545
|
|
|
case 'a' : |
|
|
|
|
546
|
|
|
$this->interactive = true; |
|
|
|
|
547
|
|
|
$this->overriddenDefaults['interactive'] = true; |
548
|
|
|
break; |
549
|
|
|
case 'e': |
550
|
|
|
$this->explain = true; |
|
|
|
|
551
|
|
|
$this->overriddenDefaults['explain'] = true; |
552
|
|
|
break; |
553
|
|
|
case 'p' : |
|
|
|
|
554
|
|
|
$this->showProgress = true; |
|
|
|
|
555
|
|
|
$this->overriddenDefaults['showProgress'] = true; |
556
|
|
|
break; |
557
|
|
|
case 'm' : |
|
|
|
|
558
|
|
|
$this->recordErrors = false; |
|
|
|
|
559
|
|
|
$this->overriddenDefaults['recordErrors'] = true; |
560
|
|
|
break; |
561
|
|
|
case 'd' : |
|
|
|
|
562
|
|
|
$ini = explode('=', $this->cliArgs[($pos + 1)]); |
563
|
|
|
$this->cliArgs[($pos + 1)] = ''; |
564
|
|
|
if (isset($ini[1]) === true) { |
565
|
|
|
ini_set($ini[0], $ini[1]); |
566
|
|
|
} else { |
567
|
|
|
ini_set($ini[0], true); |
568
|
|
|
} |
569
|
|
|
break; |
570
|
|
View Code Duplication |
case 'n' : |
|
|
|
|
571
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
572
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
573
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
574
|
|
|
} |
575
|
|
|
break; |
576
|
|
View Code Duplication |
case 'w' : |
|
|
|
|
577
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
578
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
579
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
580
|
|
|
} |
581
|
|
|
break; |
582
|
|
|
default: |
583
|
|
|
if ($this->dieOnUnknownArg === false) { |
584
|
|
|
$this->values[$arg] = $arg; |
585
|
|
|
} else { |
586
|
|
|
$this->processUnknownArgument('-'.$arg, $pos); |
587
|
|
|
} |
588
|
|
|
}//end switch |
589
|
|
|
|
590
|
|
|
}//end processShortArgument() |
591
|
|
|
|
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Processes a long (--example) command line argument. |
595
|
|
|
* |
596
|
|
|
* @param string $arg The command line argument. |
597
|
|
|
* @param int $pos The position of the argument on the command line. |
598
|
|
|
* |
599
|
|
|
* @return void |
600
|
|
|
*/ |
601
|
|
|
public function processLongArgument($arg, $pos) |
602
|
|
|
{ |
603
|
|
|
switch ($arg) { |
604
|
|
|
case 'help': |
605
|
|
|
$this->printUsage(); |
606
|
|
|
exit(0); |
|
|
|
|
607
|
|
|
case 'version': |
608
|
|
|
echo 'Symplify\PHP7_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') '; |
609
|
|
|
echo 'by Squiz (http://www.squiz.net)'.PHP_EOL; |
610
|
|
|
exit(0); |
|
|
|
|
611
|
|
|
case 'colors': |
612
|
|
|
$this->colors = true; |
|
|
|
|
613
|
|
|
$this->overriddenDefaults['colors'] = true; |
614
|
|
|
break; |
615
|
|
|
case 'no-colors': |
616
|
|
|
$this->colors = false; |
|
|
|
|
617
|
|
|
$this->overriddenDefaults['colors'] = true; |
618
|
|
|
break; |
619
|
|
|
case 'cache': |
620
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
621
|
|
|
$this->cache = true; |
|
|
|
|
622
|
|
|
$this->overriddenDefaults['cache'] = true; |
623
|
|
|
} |
624
|
|
|
break; |
625
|
|
|
case 'no-cache': |
626
|
|
|
$this->cache = false; |
|
|
|
|
627
|
|
|
$this->overriddenDefaults['cache'] = true; |
628
|
|
|
break; |
629
|
|
|
case 'config-set': |
630
|
|
View Code Duplication |
if (isset($this->cliArgs[($pos + 1)]) === false |
|
|
|
|
631
|
|
|
|| isset($this->cliArgs[($pos + 2)]) === false |
632
|
|
|
) { |
633
|
|
|
echo 'ERROR: Setting a config option requires a name and value'.PHP_EOL.PHP_EOL; |
634
|
|
|
$this->printUsage(); |
635
|
|
|
exit(0); |
|
|
|
|
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
$key = $this->cliArgs[($pos + 1)]; |
639
|
|
|
$value = $this->cliArgs[($pos + 2)]; |
640
|
|
|
$current = self::getConfigData($key); |
641
|
|
|
|
642
|
|
|
try { |
643
|
|
|
$this->setConfigData($key, $value); |
644
|
|
|
} catch (Exception $e) { |
|
|
|
|
645
|
|
|
echo $e->getMessage().PHP_EOL; |
646
|
|
|
exit(2); |
|
|
|
|
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
if ($current === null) { |
650
|
|
|
echo "Config value \"$key\" added successfully".PHP_EOL; |
651
|
|
|
} else { |
652
|
|
|
echo "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL; |
653
|
|
|
} |
654
|
|
|
exit(0); |
|
|
|
|
655
|
|
|
case 'config-delete': |
656
|
|
|
if (isset($this->cliArgs[($pos + 1)]) === false) { |
657
|
|
|
echo 'ERROR: Deleting a config option requires the name of the option'.PHP_EOL.PHP_EOL; |
658
|
|
|
$this->printUsage(); |
659
|
|
|
exit(0); |
|
|
|
|
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
$key = $this->cliArgs[($pos + 1)]; |
663
|
|
|
$current = self::getConfigData($key); |
664
|
|
|
if ($current === null) { |
665
|
|
|
echo "Config value \"$key\" has not been set".PHP_EOL; |
666
|
|
|
} else { |
667
|
|
|
try { |
668
|
|
|
$this->setConfigData($key, null); |
669
|
|
|
} catch (Exception $e) { |
|
|
|
|
670
|
|
|
echo $e->getMessage().PHP_EOL; |
671
|
|
|
exit(2); |
|
|
|
|
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
echo "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL; |
675
|
|
|
} |
676
|
|
|
exit(0); |
|
|
|
|
677
|
|
|
case 'config-show': |
678
|
|
|
$data = self::getAllConfigData(); |
679
|
|
|
$this->printConfigData($data); |
680
|
|
|
exit(0); |
|
|
|
|
681
|
|
|
case 'runtime-set': |
682
|
|
View Code Duplication |
if (isset($this->cliArgs[($pos + 1)]) === false |
|
|
|
|
683
|
|
|
|| isset($this->cliArgs[($pos + 2)]) === false |
684
|
|
|
) { |
685
|
|
|
echo 'ERROR: Setting a runtime config option requires a name and value'.PHP_EOL.PHP_EOL; |
686
|
|
|
$this->printUsage(); |
687
|
|
|
exit(0); |
|
|
|
|
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
$key = $this->cliArgs[($pos + 1)]; |
691
|
|
|
$value = $this->cliArgs[($pos + 2)]; |
692
|
|
|
$this->cliArgs[($pos + 1)] = ''; |
693
|
|
|
$this->cliArgs[($pos + 2)] = ''; |
694
|
|
|
self::setConfigData($key, $value, true); |
695
|
|
|
break; |
696
|
|
|
default: |
697
|
|
|
if (substr($arg, 0, 7) === 'sniffs=') { |
698
|
|
|
$sniffs = explode(',', substr($arg, 7)); |
699
|
|
|
foreach ($sniffs as $sniff) { |
700
|
|
|
if (substr_count($sniff, '.') !== 2) { |
701
|
|
|
echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL; |
702
|
|
|
$this->printUsage(); |
703
|
|
|
exit(2); |
|
|
|
|
704
|
|
|
} |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
$this->sniffs = $sniffs; |
|
|
|
|
708
|
|
|
$this->overriddenDefaults['sniffs'] = true; |
709
|
|
|
} else if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false |
710
|
|
|
&& substr($arg, 0, 6) === 'cache=' |
711
|
|
|
) { |
712
|
|
|
// Turn caching on. |
713
|
|
|
$this->cache = true; |
|
|
|
|
714
|
|
|
$this->overriddenDefaults['cache'] = true; |
715
|
|
|
|
716
|
|
|
$this->cacheFile = Util\Common::realpath(substr($arg, 6)); |
|
|
|
|
717
|
|
|
|
718
|
|
|
// It may not exist and return false instead. |
719
|
|
View Code Duplication |
if ($this->cacheFile === false) { |
|
|
|
|
720
|
|
|
$this->cacheFile = substr($arg, 6); |
|
|
|
|
721
|
|
|
|
722
|
|
|
$dir = dirname($this->cacheFile); |
|
|
|
|
723
|
|
|
if (is_dir($dir) === false) { |
724
|
|
|
echo 'ERROR: The specified cache file path "'.$this->cacheFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
725
|
|
|
$this->printUsage(); |
726
|
|
|
exit(2); |
|
|
|
|
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
if ($dir === '.') { |
730
|
|
|
// Passed report file is a file in the current directory. |
731
|
|
|
$this->cacheFile = getcwd().'/'.basename($this->cacheFile); |
|
|
|
|
732
|
|
|
} else { |
733
|
|
|
$dir = Util\Common::realpath(getcwd().'/'.$dir); |
734
|
|
|
if ($dir !== false) { |
735
|
|
|
// Report file path is relative. |
736
|
|
|
$this->cacheFile = $dir.'/'.basename($this->cacheFile); |
|
|
|
|
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
}//end if |
740
|
|
|
|
741
|
|
|
$this->overriddenDefaults['cacheFile'] = true; |
742
|
|
|
|
743
|
|
|
if (is_dir($this->cacheFile) === true) { |
|
|
|
|
744
|
|
|
echo 'ERROR: The specified cache file path "'.$this->cacheFile.'" is a directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
745
|
|
|
$this->printUsage(); |
746
|
|
|
exit(2); |
|
|
|
|
747
|
|
|
} |
748
|
|
|
} else if (substr($arg, 0, 10) === 'bootstrap=') { |
749
|
|
|
$files = explode(',', substr($arg, 10)); |
750
|
|
|
$bootstrap = array(); |
751
|
|
|
foreach ($files as $file) { |
752
|
|
|
$path = Util\Common::realpath($file); |
753
|
|
|
if ($path === false) { |
754
|
|
|
echo 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL; |
755
|
|
|
$this->printUsage(); |
756
|
|
|
exit(2); |
|
|
|
|
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
$bootstrap[] = $path; |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
$this->bootstrap = array_merge($this->bootstrap, $bootstrap); |
|
|
|
|
763
|
|
|
$this->overriddenDefaults['bootstrap'] = true; |
764
|
|
|
} else if (substr($arg, 0, 11) === 'stdin-path=') { |
765
|
|
|
$this->stdinPath = Util\Common::realpath(substr($arg, 11)); |
|
|
|
|
766
|
|
|
|
767
|
|
|
// It may not exist and return false instead, so use whatever they gave us. |
768
|
|
|
if ($this->stdinPath === false) { |
|
|
|
|
769
|
|
|
$this->stdinPath = trim(substr($arg, 11)); |
|
|
|
|
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
$this->overriddenDefaults['stdinPath'] = true; |
773
|
|
|
} else if (PHP_CodeSniffer_CBF === false && substr($arg, 0, 12) === 'report-file=') { |
774
|
|
|
$this->reportFile = Util\Common::realpath(substr($arg, 12)); |
|
|
|
|
775
|
|
|
|
776
|
|
|
// It may not exist and return false instead. |
777
|
|
View Code Duplication |
if ($this->reportFile === false) { |
|
|
|
|
778
|
|
|
$this->reportFile = substr($arg, 12); |
|
|
|
|
779
|
|
|
|
780
|
|
|
$dir = dirname($this->reportFile); |
|
|
|
|
781
|
|
|
if (is_dir($dir) === false) { |
782
|
|
|
echo 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
783
|
|
|
$this->printUsage(); |
784
|
|
|
exit(2); |
|
|
|
|
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
if ($dir === '.') { |
788
|
|
|
// Passed report file is a file in the current directory. |
789
|
|
|
$this->reportFile = getcwd().'/'.basename($this->reportFile); |
|
|
|
|
790
|
|
|
} else { |
791
|
|
|
$dir = Util\Common::realpath(getcwd().'/'.$dir); |
792
|
|
|
if ($dir !== false) { |
793
|
|
|
// Report file path is relative. |
794
|
|
|
$this->reportFile = $dir.'/'.basename($this->reportFile); |
|
|
|
|
795
|
|
|
} |
796
|
|
|
} |
797
|
|
|
}//end if |
798
|
|
|
|
799
|
|
|
$this->overriddenDefaults['reportFile'] = true; |
800
|
|
|
|
801
|
|
|
if (is_dir($this->reportFile) === true) { |
|
|
|
|
802
|
|
|
echo 'ERROR: The specified report file path "'.$this->reportFile.'" is a directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
803
|
|
|
$this->printUsage(); |
804
|
|
|
exit(2); |
|
|
|
|
805
|
|
|
} |
806
|
|
|
} else if (substr($arg, 0, 13) === 'report-width=') { |
807
|
|
|
if (isset($this->overriddenDefaults['reportWidth']) === true) { |
808
|
|
|
break; |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
$this->reportWidth = substr($arg, 13); |
|
|
|
|
812
|
|
|
$this->overriddenDefaults['reportWidth'] = true; |
813
|
|
|
} else if (substr($arg, 0, 9) === 'basepath=') { |
814
|
|
|
if (isset($this->overriddenDefaults['basepath']) === true) { |
815
|
|
|
break; |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
$this->basepath = Util\Common::realpath(substr($arg, 9)); |
|
|
|
|
819
|
|
|
|
820
|
|
|
// It may not exist and return false instead. |
821
|
|
|
if ($this->basepath === false) { |
|
|
|
|
822
|
|
|
$this->basepath = substr($arg, 9); |
|
|
|
|
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
$this->overriddenDefaults['basepath'] = true; |
826
|
|
|
|
827
|
|
|
if (is_dir($this->basepath) === false) { |
|
|
|
|
828
|
|
|
echo 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
829
|
|
|
$this->printUsage(); |
830
|
|
|
exit(2); |
|
|
|
|
831
|
|
|
} |
832
|
|
|
} else if ((substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-')) { |
833
|
|
|
$reports = array(); |
834
|
|
|
|
835
|
|
|
if ($arg[6] === '-') { |
836
|
|
|
// This is a report with file output. |
837
|
|
|
$split = strpos($arg, '='); |
838
|
|
|
if ($split === false) { |
839
|
|
|
$report = substr($arg, 7); |
840
|
|
|
$output = null; |
841
|
|
|
} else { |
842
|
|
|
$report = substr($arg, 7, ($split - 7)); |
843
|
|
|
$output = substr($arg, ($split + 1)); |
844
|
|
|
if ($output === false) { |
845
|
|
|
$output = null; |
846
|
|
|
} else { |
847
|
|
|
$dir = dirname($output); |
848
|
|
|
if ($dir === '.') { |
849
|
|
|
// Passed report file is a filename in the current directory. |
850
|
|
|
$output = getcwd().'/'.basename($output); |
851
|
|
|
} else { |
852
|
|
|
$dir = Util\Common::realpath(getcwd().'/'.$dir); |
853
|
|
|
if ($dir !== false) { |
854
|
|
|
// Report file path is relative. |
855
|
|
|
$output = $dir.'/'.basename($output); |
856
|
|
|
} |
857
|
|
|
} |
858
|
|
|
}//end if |
859
|
|
|
}//end if |
860
|
|
|
|
861
|
|
|
$reports[$report] = $output; |
862
|
|
|
} else { |
863
|
|
|
// This is a single report. |
864
|
|
|
if (isset($this->overriddenDefaults['reports']) === true) { |
865
|
|
|
break; |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
$reportNames = explode(',', substr($arg, 7)); |
869
|
|
|
foreach ($reportNames as $report) { |
870
|
|
|
$reports[$report] = null; |
871
|
|
|
} |
872
|
|
|
}//end if |
873
|
|
|
|
874
|
|
|
// Remove the default value so the CLI value overrides it. |
875
|
|
|
if (isset($this->overriddenDefaults['reports']) === false) { |
876
|
|
|
$this->reports = $reports; |
|
|
|
|
877
|
|
|
} else { |
878
|
|
|
$this->reports = array_merge($this->reports, $reports); |
|
|
|
|
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
$this->overriddenDefaults['reports'] = true; |
882
|
|
|
} else if (substr($arg, 0, 7) === 'filter=') { |
883
|
|
|
if (isset($this->overriddenDefaults['filter']) === true) { |
884
|
|
|
break; |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
$this->filter = substr($arg, 7); |
|
|
|
|
888
|
|
|
$this->overriddenDefaults['filter'] = true; |
889
|
|
|
} else if (substr($arg, 0, 9) === 'standard=') { |
890
|
|
|
$standards = trim(substr($arg, 9)); |
891
|
|
|
if ($standards !== '') { |
892
|
|
|
$this->standards = explode(',', $standards); |
|
|
|
|
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
$this->overriddenDefaults['standards'] = true; |
896
|
|
|
} else if (substr($arg, 0, 11) === 'extensions=') { |
897
|
|
|
$extensions = explode(',', substr($arg, 11)); |
898
|
|
|
$newExtensions = array(); |
899
|
|
|
foreach ($extensions as $ext) { |
900
|
|
|
$slash = strpos($ext, '/'); |
901
|
|
|
if ($slash !== false) { |
902
|
|
|
// They specified the tokenizer too. |
903
|
|
|
list($ext, $tokenizer) = explode('/', $ext); |
904
|
|
|
$newExtensions[$ext] = strtoupper($tokenizer); |
905
|
|
|
continue; |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
if (isset($this->extensions[$ext]) === true) { |
|
|
|
|
909
|
|
|
$newExtensions[$ext] = $this->extensions[$ext]; |
|
|
|
|
910
|
|
|
} else { |
911
|
|
|
$newExtensions[$ext] = 'PHP'; |
912
|
|
|
} |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
$this->extensions = $newExtensions; |
|
|
|
|
916
|
|
|
$this->overriddenDefaults['extensions'] = true; |
917
|
|
|
} else if (substr($arg, 0, 7) === 'suffix=') { |
918
|
|
|
$this->suffix = explode(',', substr($arg, 7)); |
|
|
|
|
919
|
|
|
$this->overriddenDefaults['suffix'] = true; |
920
|
|
|
} else if (substr($arg, 0, 9) === 'parallel=') { |
921
|
|
|
$this->parallel = max((int) substr($arg, 9), 1); |
|
|
|
|
922
|
|
|
$this->overriddenDefaults['parallel'] = true; |
923
|
|
|
} else if (substr($arg, 0, 9) === 'severity=') { |
924
|
|
|
$this->errorSeverity = (int) substr($arg, 9); |
|
|
|
|
925
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
926
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
927
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
928
|
|
|
} else if (substr($arg, 0, 15) === 'error-severity=') { |
929
|
|
|
$this->errorSeverity = (int) substr($arg, 15); |
|
|
|
|
930
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
931
|
|
|
} else if (substr($arg, 0, 17) === 'warning-severity=') { |
932
|
|
|
$this->warningSeverity = (int) substr($arg, 17); |
|
|
|
|
933
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
934
|
|
|
} else if (substr($arg, 0, 7) === 'ignore=') { |
935
|
|
|
// Split the ignore string on commas, unless the comma is escaped |
936
|
|
|
// using 1 or 3 slashes (\, or \\\,). |
|
|
|
|
937
|
|
|
$patterns = preg_split( |
938
|
|
|
'/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', |
939
|
|
|
substr($arg, 7) |
940
|
|
|
); |
941
|
|
|
|
942
|
|
|
$ignored = array(); |
943
|
|
|
foreach ($patterns as $pattern) { |
944
|
|
|
$pattern = trim($pattern); |
945
|
|
|
if ($pattern === '') { |
946
|
|
|
continue; |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
$ignored[$pattern] = 'absolute'; |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
$this->ignored = $ignored; |
|
|
|
|
953
|
|
|
$this->overriddenDefaults['ignored'] = true; |
954
|
|
|
} else if (substr($arg, 0, 10) === 'generator=' |
955
|
|
|
&& PHP_CodeSniffer_CBF === false |
956
|
|
|
) { |
957
|
|
|
$this->generator = substr($arg, 10); |
|
|
|
|
958
|
|
|
$this->overriddenDefaults['generator'] = true; |
959
|
|
|
} else if (substr($arg, 0, 9) === 'encoding=') { |
960
|
|
|
$this->encoding = strtolower(substr($arg, 9)); |
|
|
|
|
961
|
|
|
$this->overriddenDefaults['encoding'] = true; |
962
|
|
|
} else if (substr($arg, 0, 10) === 'tab-width=') { |
963
|
|
|
$this->tabWidth = (int) substr($arg, 10); |
|
|
|
|
964
|
|
|
$this->overriddenDefaults['tabWidth'] = true; |
965
|
|
|
} else { |
966
|
|
|
if ($this->dieOnUnknownArg === false) { |
967
|
|
|
$eqPos = strpos($arg, '='); |
968
|
|
|
if ($eqPos === false) { |
969
|
|
|
$this->values[$arg] = $arg; |
970
|
|
|
} else { |
971
|
|
|
$value = substr($arg, ($eqPos + 1)); |
972
|
|
|
$arg = substr($arg, 0, $eqPos); |
973
|
|
|
$this->values[$arg] = $value; |
974
|
|
|
} |
975
|
|
|
} else { |
976
|
|
|
$this->processUnknownArgument('--'.$arg, $pos); |
977
|
|
|
} |
978
|
|
|
}//end if |
979
|
|
|
|
980
|
|
|
break; |
981
|
|
|
}//end switch |
982
|
|
|
|
983
|
|
|
}//end processLongArgument() |
984
|
|
|
|
985
|
|
|
|
986
|
|
|
/** |
987
|
|
|
* Processes an unknown command line argument. |
988
|
|
|
* |
989
|
|
|
* Assumes all unknown arguments are files and folders to check. |
990
|
|
|
* |
991
|
|
|
* @param string $arg The command line argument. |
992
|
|
|
* @param int $pos The position of the argument on the command line. |
993
|
|
|
* |
994
|
|
|
* @return void |
995
|
|
|
*/ |
996
|
|
|
public function processUnknownArgument($arg, $pos) |
|
|
|
|
997
|
|
|
{ |
998
|
|
|
// If we are processing STDIN, don't record any files to check. |
999
|
|
|
if ($this->stdin === true) { |
|
|
|
|
1000
|
|
|
return; |
1001
|
|
|
} |
1002
|
|
|
|
1003
|
|
|
// We don't know about any additional switches; just files. |
1004
|
|
|
if ($arg{0} === '-') { |
1005
|
|
|
if ($this->dieOnUnknownArg === false) { |
1006
|
|
|
return; |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
echo "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL; |
1010
|
|
|
$this->printUsage(); |
1011
|
|
|
exit(2); |
|
|
|
|
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
$file = Util\Common::realpath($arg); |
1015
|
|
|
if (file_exists($file) === false) { |
1016
|
|
|
if ($this->dieOnUnknownArg === false) { |
1017
|
|
|
return; |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
|
|
echo 'ERROR: The file "'.$arg.'" does not exist.'.PHP_EOL.PHP_EOL; |
1021
|
|
|
$this->printUsage(); |
1022
|
|
|
exit(2); |
|
|
|
|
1023
|
|
|
} else { |
1024
|
|
|
$files = $this->files; |
|
|
|
|
1025
|
|
|
$files[] = $file; |
1026
|
|
|
$this->files = $files; |
|
|
|
|
1027
|
|
|
$this->overriddenDefaults['files'] = true; |
1028
|
|
|
} |
1029
|
|
|
|
1030
|
|
|
}//end processUnknownArgument() |
1031
|
|
|
|
1032
|
|
|
|
1033
|
|
|
/** |
1034
|
|
|
* Prints out the usage information for this script. |
1035
|
|
|
* |
1036
|
|
|
* @return void |
1037
|
|
|
*/ |
1038
|
|
|
public function printUsage() |
1039
|
|
|
{ |
1040
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
1041
|
|
|
$this->printPHPCBFUsage(); |
1042
|
|
|
} else { |
1043
|
|
|
$this->printPHPCSUsage(); |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
}//end printUsage() |
1047
|
|
|
|
1048
|
|
|
|
1049
|
|
|
/** |
1050
|
|
|
* Prints out the usage information for PHPCS. |
1051
|
|
|
* |
1052
|
|
|
* @return void |
1053
|
|
|
*/ |
1054
|
|
|
public function printPHPCSUsage() |
1055
|
|
|
{ |
1056
|
|
|
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--cache[=<cacheFile>]] [--no-cache] [--colors] [--no-colors]'.PHP_EOL; |
1057
|
|
|
echo ' [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...'.PHP_EOL; |
1058
|
|
|
echo ' [--report-width=<reportWidth>] [--basepath=<basepath>] [--tab-width=<tabWidth>]'.PHP_EOL; |
1059
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
1060
|
|
|
echo ' [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]'.PHP_EOL; |
1061
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--encoding=<encoding>] [--parallel=<processes>]'.PHP_EOL; |
1062
|
|
|
echo ' [--extensions=<extensions>] [--generator=<generator>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
1063
|
|
|
echo ' - Check STDIN instead of local files and directories'.PHP_EOL; |
1064
|
|
|
echo ' -n Do not print warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
1065
|
|
|
echo ' -w Print both warnings and errors (this is the default)'.PHP_EOL; |
1066
|
|
|
echo ' -l Local directory only, no recursion'.PHP_EOL; |
1067
|
|
|
echo ' -s Show sniff codes in all reports'.PHP_EOL; |
1068
|
|
|
echo ' -a Run interactively'.PHP_EOL; |
1069
|
|
|
echo ' -e Explain a standard by showing the sniffs it includes'.PHP_EOL; |
1070
|
|
|
echo ' -p Show progress of the run'.PHP_EOL; |
1071
|
|
|
echo ' -m Stop error messages from being recorded'.PHP_EOL; |
1072
|
|
|
echo ' (saves a lot of memory, but stops many reports from being used)'.PHP_EOL; |
1073
|
|
|
echo ' -v[v][v] Print verbose output'.PHP_EOL; |
1074
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
1075
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
1076
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
1077
|
|
|
echo ' --version Print version information'.PHP_EOL; |
1078
|
|
|
echo ' --colors Use colors in output'.PHP_EOL; |
1079
|
|
|
echo ' --no-colors Do not use colors in output (this is the default)'.PHP_EOL; |
1080
|
|
|
echo ' --cache Cache results between runs'.PHP_EOL; |
1081
|
|
|
echo ' --no-cache Do not cache results between runs (this is the default)'.PHP_EOL; |
1082
|
|
|
echo ' <cacheFile> Use a specific file for caching (uses a temporary file by default)'.PHP_EOL; |
1083
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
1084
|
|
|
echo ' <file> One or more files and/or directories to check'.PHP_EOL; |
1085
|
|
|
echo ' <encoding> The encoding of the files being checked (default is iso-8859-1)'.PHP_EOL; |
1086
|
|
|
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL; |
1087
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
1088
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
1089
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
1090
|
|
|
echo ' <generator> Uses either the "HTML", "Markdown" or "Text" generator'.PHP_EOL; |
1091
|
|
|
echo ' (forces documentation generation instead of checking)'.PHP_EOL; |
1092
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
1093
|
|
|
echo ' <processes> How many files should be checked simultaneously (default is 1)'.PHP_EOL; |
1094
|
|
|
echo ' <report> Print either the "full", "xml", "checkstyle", "csv"'.PHP_EOL; |
1095
|
|
|
echo ' "json", "junit", "emacs", "source", "summary", "diff"'.PHP_EOL; |
1096
|
|
|
echo ' "svnblame", "gitblame", "hgblame" or "notifysend" report'.PHP_EOL; |
1097
|
|
|
echo ' (the "full" report is printed by default)'.PHP_EOL; |
1098
|
|
|
echo ' <reportFile> Write the report to the specified file path'.PHP_EOL; |
1099
|
|
|
echo ' <reportWidth> How many columns wide screen reports should be printed'.PHP_EOL; |
1100
|
|
|
echo ' or set to "auto" to use current screen width, where supported'.PHP_EOL; |
1101
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the check to'.PHP_EOL; |
1102
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
1103
|
|
|
echo ' <severity> The minimum severity required to display an error or warning'.PHP_EOL; |
1104
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
1105
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
1106
|
|
|
|
1107
|
|
|
}//end printPHPCSUsage() |
1108
|
|
|
|
1109
|
|
|
|
1110
|
|
|
/** |
1111
|
|
|
* Prints out the usage information for PHPCBF. |
1112
|
|
|
* |
1113
|
|
|
* @return void |
1114
|
|
|
*/ |
1115
|
|
|
public function printPHPCBFUsage() |
1116
|
|
|
{ |
1117
|
|
|
echo 'Usage: phpcbf [-nwli] [-d key[=value]]'.PHP_EOL; |
1118
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--suffix=<suffix>]'.PHP_EOL; |
1119
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
1120
|
|
|
echo ' [--tab-width=<tabWidth>] [--encoding=<encoding>] [--parallel=<processes>]'.PHP_EOL; |
1121
|
|
|
echo ' [--basepath=<basepath>] [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
1122
|
|
|
echo ' - Fix STDIN instead of local files and directories'.PHP_EOL; |
1123
|
|
|
echo ' -n Do not fix warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
1124
|
|
|
echo ' -w Fix both warnings and errors (on by default)'.PHP_EOL; |
1125
|
|
|
echo ' -l Local directory only, no recursion'.PHP_EOL; |
1126
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
1127
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
1128
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
1129
|
|
|
echo ' --version Print version information'.PHP_EOL; |
1130
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
1131
|
|
|
echo ' <file> One or more files and/or directories to fix'.PHP_EOL; |
1132
|
|
|
echo ' <encoding> The encoding of the files being fixed (default is iso-8859-1)'.PHP_EOL; |
1133
|
|
|
echo ' <extensions> A comma separated list of file extensions to fix'.PHP_EOL; |
1134
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
1135
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
1136
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
1137
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
1138
|
|
|
echo ' <processes> How many files should be fixed simultaneously (default is 1)'.PHP_EOL; |
1139
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the fixes to'.PHP_EOL; |
1140
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
1141
|
|
|
echo ' <severity> The minimum severity required to fix an error or warning'.PHP_EOL; |
1142
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
1143
|
|
|
echo ' <suffix> Write modified files to a filename using this suffix'.PHP_EOL; |
1144
|
|
|
echo ' ("diff" and "patch" are not used in this mode)'.PHP_EOL; |
1145
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
1146
|
|
|
|
1147
|
|
|
}//end printPHPCBFUsage() |
1148
|
|
|
|
1149
|
|
|
|
1150
|
|
|
/** |
1151
|
|
|
* Get a single config value. |
1152
|
|
|
* |
1153
|
|
|
* @param string $key The name of the config value. |
1154
|
|
|
* |
1155
|
|
|
* @return string|null |
1156
|
|
|
* @see setConfigData() |
1157
|
|
|
* @see getAllConfigData() |
1158
|
|
|
*/ |
1159
|
|
|
public static function getConfigData($key) |
1160
|
|
|
{ |
1161
|
|
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
1162
|
|
|
|
1163
|
|
|
if ($phpCodeSnifferConfig === null) { |
1164
|
|
|
return null; |
1165
|
|
|
} |
1166
|
|
|
|
1167
|
|
|
if (isset($phpCodeSnifferConfig[$key]) === false) { |
1168
|
|
|
return null; |
1169
|
|
|
} |
1170
|
|
|
|
1171
|
|
|
return $phpCodeSnifferConfig[$key]; |
1172
|
|
|
|
1173
|
|
|
}//end getConfigData() |
1174
|
|
|
|
1175
|
|
|
|
1176
|
|
|
/** |
1177
|
|
|
* Get the path to an executable utility. |
1178
|
|
|
* |
1179
|
|
|
* @param string $name The name of the executable utility. |
1180
|
|
|
* |
1181
|
|
|
* @return string|null |
1182
|
|
|
* @see getConfigData() |
1183
|
|
|
*/ |
1184
|
|
|
public static function getExecutablePath($name) |
1185
|
|
|
{ |
1186
|
|
|
$data = self::getConfigData($name.'_path'); |
1187
|
|
|
if ($data !== null) { |
1188
|
|
|
return $data; |
1189
|
|
|
} |
1190
|
|
|
|
1191
|
|
|
if (array_key_exists($name, self::$executablePaths) === true) { |
1192
|
|
|
return self::$executablePaths[$name]; |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
1196
|
|
|
$cmd = 'where '.escapeshellarg($name).' 2> nul'; |
1197
|
|
|
} else { |
1198
|
|
|
$cmd = 'which '.escapeshellarg($name); |
1199
|
|
|
} |
1200
|
|
|
|
1201
|
|
|
$result = exec($cmd, $output, $retVal); |
1202
|
|
|
if ($retVal !== 0) { |
1203
|
|
|
$result = null; |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
|
|
self::$executablePaths[$name] = $result; |
1207
|
|
|
return $result; |
1208
|
|
|
|
1209
|
|
|
}//end getExecutablePath() |
1210
|
|
|
|
1211
|
|
|
|
1212
|
|
|
/** |
1213
|
|
|
* Set a single config value. |
1214
|
|
|
* |
1215
|
|
|
* @param string $key The name of the config value. |
1216
|
|
|
* @param string|null $value The value to set. If null, the config |
1217
|
|
|
* entry is deleted, reverting it to the |
1218
|
|
|
* default value. |
1219
|
|
|
* @param boolean $temp Set this config data temporarily for this |
1220
|
|
|
* script run. This will not write the config |
1221
|
|
|
* data to the config file. |
1222
|
|
|
* |
1223
|
|
|
* @return bool |
1224
|
|
|
* @see getConfigData() |
1225
|
|
|
* @throws RuntimeException If the config file can not be written. |
1226
|
|
|
*/ |
1227
|
|
|
public static function setConfigData($key, $value, $temp=false) |
1228
|
|
|
{ |
1229
|
|
|
if ($temp === false) { |
1230
|
|
|
$configFile = dirname(__FILE__).'/../CodeSniffer.conf'; |
1231
|
|
|
if (is_file($configFile) === false |
1232
|
|
|
&& strpos('@data_dir@', '@data_dir') === false |
1233
|
|
|
) { |
1234
|
|
|
// If data_dir was replaced, this is a PEAR install and we can |
1235
|
|
|
// use the PEAR data dir to store the conf file. |
1236
|
|
|
$configFile = '@data_dir@/Symplify\PHP7_CodeSniffer/CodeSniffer.conf'; |
1237
|
|
|
} |
1238
|
|
|
|
1239
|
|
|
if (is_file($configFile) === true |
1240
|
|
|
&& is_writable($configFile) === false |
1241
|
|
|
) { |
1242
|
|
|
$error = 'Config file '.$configFile.' is not writable'; |
1243
|
|
|
throw new RuntimeException($error); |
1244
|
|
|
} |
1245
|
|
|
} |
1246
|
|
|
|
1247
|
|
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
1248
|
|
|
|
1249
|
|
|
if ($value === null) { |
1250
|
|
|
if (isset($phpCodeSnifferConfig[$key]) === true) { |
1251
|
|
|
unset($phpCodeSnifferConfig[$key]); |
1252
|
|
|
} |
1253
|
|
|
} else { |
1254
|
|
|
$phpCodeSnifferConfig[$key] = $value; |
1255
|
|
|
} |
1256
|
|
|
|
1257
|
|
|
if ($temp === false) { |
1258
|
|
|
$output = '<'.'?php'."\n".' $phpCodeSnifferConfig = '; |
1259
|
|
|
$output .= var_export($phpCodeSnifferConfig, true); |
1260
|
|
|
$output .= "\n?".'>'; |
1261
|
|
|
|
1262
|
|
|
if (file_put_contents($configFile, $output) === false) { |
|
|
|
|
1263
|
|
|
return false; |
1264
|
|
|
} |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
self::$configData = $phpCodeSnifferConfig; |
1268
|
|
|
|
1269
|
|
|
return true; |
1270
|
|
|
|
1271
|
|
|
}//end setConfigData() |
1272
|
|
|
|
1273
|
|
|
|
1274
|
|
|
/** |
1275
|
|
|
* Get all config data. |
1276
|
|
|
* |
1277
|
|
|
* @return array<string, string> |
|
|
|
|
1278
|
|
|
* @see getConfigData() |
1279
|
|
|
*/ |
1280
|
|
|
public static function getAllConfigData() |
1281
|
|
|
{ |
1282
|
|
|
if (self::$configData !== null) { |
1283
|
|
|
return self::$configData; |
1284
|
|
|
} |
1285
|
|
|
|
1286
|
|
|
$configFile = dirname(__FILE__).'/../CodeSniffer.conf'; |
1287
|
|
|
if (is_file($configFile) === false) { |
1288
|
|
|
$configFile = '@data_dir@/Symplify\PHP7_CodeSniffer/CodeSniffer.conf'; |
1289
|
|
|
} |
1290
|
|
|
|
1291
|
|
|
if (is_file($configFile) === false) { |
1292
|
|
|
self::$configData = array(); |
1293
|
|
|
return array(); |
1294
|
|
|
} |
1295
|
|
|
|
1296
|
|
|
include $configFile; |
1297
|
|
|
self::$configData = $phpCodeSnifferConfig; |
|
|
|
|
1298
|
|
|
return self::$configData; |
1299
|
|
|
|
1300
|
|
|
}//end getAllConfigData() |
1301
|
|
|
|
1302
|
|
|
|
1303
|
|
|
/** |
1304
|
|
|
* Prints out the gathered config data. |
1305
|
|
|
* |
1306
|
|
|
* @param array $data The config data to print. |
1307
|
|
|
* |
1308
|
|
|
* @return void |
1309
|
|
|
*/ |
1310
|
|
|
public function printConfigData($data) |
1311
|
|
|
{ |
1312
|
|
|
$max = 0; |
1313
|
|
|
$keys = array_keys($data); |
1314
|
|
|
foreach ($keys as $key) { |
1315
|
|
|
$len = strlen($key); |
1316
|
|
|
if (strlen($key) > $max) { |
1317
|
|
|
$max = $len; |
1318
|
|
|
} |
1319
|
|
|
} |
1320
|
|
|
|
1321
|
|
|
if ($max === 0) { |
1322
|
|
|
return; |
1323
|
|
|
} |
1324
|
|
|
|
1325
|
|
|
$max += 2; |
1326
|
|
|
ksort($data); |
1327
|
|
|
foreach ($data as $name => $value) { |
1328
|
|
|
echo str_pad($name.': ', $max).$value.PHP_EOL; |
1329
|
|
|
} |
1330
|
|
|
|
1331
|
|
|
}//end printConfigData() |
1332
|
|
|
|
1333
|
|
|
|
1334
|
|
|
}//end class |
1335
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.