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
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const VERSION = '3.0.0'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* An array of settings that PHPCS and PHPCBF accept. |
27
|
|
|
* |
28
|
|
|
* This array is not meant to be accessed directly. Instead, use the settings |
29
|
|
|
* as if they are class member vars so the __get() and __set() magic methods |
30
|
|
|
* can be used to validate the values. For example, to set the verbosity level to |
31
|
|
|
* level 2, use $this->verbosity = 2; insteas of accessing this property directly. |
32
|
|
|
* |
33
|
|
|
* The list of settings are: |
34
|
|
|
* |
35
|
|
|
* string[] files The files and directories to check. |
36
|
|
|
* string[] standards The standards being used for checking. |
37
|
|
|
* int verbosity How verbose the output should be. |
38
|
|
|
* 0: no unnecessary output |
39
|
|
|
* 1: basic output for files being checked |
40
|
|
|
* 2: ruleset and file parsing output |
41
|
|
|
* 3: sniff execution output |
42
|
|
|
* bool interactive Enable interactive checking mode. |
43
|
|
|
* bool cache Enable the use of the file cache. |
44
|
|
|
* bool explain Explain the coding standards. |
45
|
|
|
* bool local Process local files in directories only (no recursion). |
46
|
|
|
* bool showSources Show sniff source codes in report output. |
47
|
|
|
* bool showProgress Show basic progress information while running. |
48
|
|
|
* int tabWidth How many spaces each tab is worth. |
49
|
|
|
* string[] sniffs The sniffs that should be used for checking. |
50
|
|
|
* If empty, all sniffs in the supplied standards will be used. |
51
|
|
|
* string[] ignored Regular expressions used to ignore files and folders during checking. |
52
|
|
|
* string reportFile A file where the report output should be written. |
53
|
|
|
* string filter The filter to use for the run. |
54
|
|
|
* string[] bootstrap One of more files to include before the run begins. |
55
|
|
|
* int reportWidth The maximum number of columns that reports should use for output. |
56
|
|
|
* Set to "auto" for have this value changed to the width of the terminal. |
57
|
|
|
* int errorSeverity The minimum severity an error must have to be displayed. |
58
|
|
|
* int warningSeverity The minimum severity a warning must have to be displayed. |
59
|
|
|
* bool recordErrors Record the content of error messages as well as error counts. |
60
|
|
|
* string suffix A suffix to add to fixed files. |
61
|
|
|
* string basepath A file system location to strip from the paths of files shown in reports. |
62
|
|
|
* bool stdin Read content from STDIN instead of supplied files. |
63
|
|
|
* string stdinContent Content passed directly to PHPCS on STDIN. |
64
|
|
|
* string stdinPath The path to use for content passed on STDIN. |
65
|
|
|
* |
66
|
|
|
* array<string, string> extensions File extensions that should be checked, and what tokenizer to use. |
67
|
|
|
* E.g., array('inc' => 'PHP'); |
68
|
|
|
* array<string, string|null> reports The reports to use for printing output after the run. |
69
|
|
|
* The format of the array is: |
70
|
|
|
* array( |
71
|
|
|
* 'reportName1' => 'outputFile', |
72
|
|
|
* 'reportName2' => null, |
73
|
|
|
* ); |
74
|
|
|
* If the array value is NULL, the report will be written to the screen. |
75
|
|
|
* |
76
|
|
|
* @var array<string, mixed> |
77
|
|
|
*/ |
78
|
|
|
private $settings = array( |
79
|
|
|
'files' => null, |
80
|
|
|
'standards' => null, |
81
|
|
|
'verbosity' => null, |
82
|
|
|
'interactive' => null, |
83
|
|
|
'explain' => null, |
84
|
|
|
'local' => null, |
85
|
|
|
'showSources' => null, |
86
|
|
|
'showProgress' => null, |
87
|
|
|
'tabWidth' => null, |
88
|
|
|
'extensions' => null, |
89
|
|
|
'sniffs' => null, |
90
|
|
|
'ignored' => null, |
91
|
|
|
'reportFile' => null, |
92
|
|
|
'filter' => null, |
93
|
|
|
'bootstrap' => null, |
94
|
|
|
'reports' => null, |
95
|
|
|
'basepath' => null, |
96
|
|
|
'reportWidth' => null, |
97
|
|
|
'errorSeverity' => null, |
98
|
|
|
'warningSeverity' => null, |
99
|
|
|
'recordErrors' => null, |
100
|
|
|
'suffix' => null, |
101
|
|
|
'stdin' => null, |
102
|
|
|
'stdinContent' => null, |
103
|
|
|
'stdinPath' => null, |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The current command line arguments we are processing. |
108
|
|
|
* |
109
|
|
|
* @var string[] |
110
|
|
|
*/ |
111
|
|
|
private $cliArgs = array(); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Command line values that the user has supplied directly. |
115
|
|
|
* |
116
|
|
|
* @var array<string, TRUE> |
117
|
|
|
*/ |
118
|
|
|
private $overriddenDefaults = array(); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Unknown arguments |
122
|
|
|
* |
123
|
|
|
* @var array<mixed> |
124
|
|
|
*/ |
125
|
|
|
private $values = array(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Config file data that has been loaded for the run. |
129
|
|
|
* |
130
|
|
|
* @var array<string, string> |
131
|
|
|
*/ |
132
|
|
|
private static $configData = null; |
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Automatically discovered executable utility paths. |
136
|
|
|
* |
137
|
|
|
* @var array<string, string> |
138
|
|
|
*/ |
139
|
|
|
private static $executablePaths = array(); |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the value of an inaccessible property. |
144
|
|
|
* |
145
|
|
|
* @param string $name The name of the property. |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
* @throws RuntimeException If the setting name is invalid. |
149
|
|
|
*/ |
150
|
|
|
public function __get($name) |
151
|
|
|
{ |
152
|
|
|
if (array_key_exists($name, $this->settings) === false) { |
153
|
|
|
throw new RuntimeException("ERROR: unable to get value of property \"$name\""); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->settings[$name]; |
157
|
|
|
|
158
|
|
|
}//end __get() |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the value of an inaccessible property. |
163
|
|
|
* |
164
|
|
|
* @param string $name The name of the property. |
165
|
|
|
* @param mixed $value The value of the property. |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
* @throws RuntimeException If the setting name is invalid. |
169
|
|
|
*/ |
170
|
12 |
|
public function __set($name, $value) |
171
|
|
|
{ |
172
|
12 |
|
if (array_key_exists($name, $this->settings) === false) { |
173
|
|
|
throw new RuntimeException("Can't __set() $name; setting doesn't exist"); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
switch ($name) { |
177
|
12 |
|
case 'reportWidth' : |
|
|
|
|
178
|
|
|
// Support auto terminal width. |
179
|
|
|
if ($value === 'auto' && preg_match('|\d+ (\d+)|', shell_exec('stty size 2>&1'), $matches) === 1) { |
180
|
|
|
$value = (int) $matches[1]; |
181
|
|
|
} else { |
182
|
|
|
$value = (int) $value; |
183
|
|
|
} |
184
|
|
|
break; |
185
|
12 |
|
case 'standards' : |
|
|
|
|
186
|
12 |
|
$cleaned = array(); |
187
|
|
|
|
188
|
|
|
// Check if the standard name is valid, or if the case is invalid. |
189
|
12 |
|
$installedStandards = Util\Standards::getInstalledStandards(); |
190
|
|
|
foreach ($value as $standard) { |
191
|
|
|
foreach ($installedStandards as $validStandard) { |
192
|
|
|
if (strtolower($standard) === strtolower($validStandard)) { |
193
|
|
|
$standard = $validStandard; |
194
|
|
|
break; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$cleaned[] = $standard; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$value = $cleaned; |
202
|
|
|
break; |
203
|
|
|
default : |
|
|
|
|
204
|
|
|
// No validation required. |
205
|
12 |
|
break; |
206
|
|
|
}//end switch |
207
|
|
|
|
208
|
12 |
|
$this->settings[$name] = $value; |
209
|
|
|
|
210
|
12 |
|
}//end __set() |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Check if the value of an inaccessible property is set. |
215
|
|
|
* |
216
|
|
|
* @param string $name The name of the property. |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
public function __isset($name) |
221
|
|
|
{ |
222
|
|
|
return isset($this->settings[$name]); |
223
|
|
|
|
224
|
|
|
}//end __isset() |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Unset the value of an inaccessible property. |
229
|
|
|
* |
230
|
|
|
* @param string $name The name of the property. |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function __unset($name) |
235
|
|
|
{ |
236
|
|
|
$this->settings[$name] = null; |
237
|
|
|
|
238
|
|
|
}//end __unset() |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Creates a Config object and populates it with command line values. |
243
|
|
|
* |
244
|
|
|
* @param array $cliArgs An array of values gathered from CLI args. |
245
|
|
|
* |
246
|
|
|
* @return void |
|
|
|
|
247
|
|
|
*/ |
248
|
12 |
|
public function __construct(array $cliArgs=array()) |
|
|
|
|
249
|
|
|
{ |
250
|
12 |
|
$checkStdin = false; |
251
|
12 |
|
if (empty($cliArgs) === true) { |
252
|
12 |
|
$cliArgs = $_SERVER['argv']; |
253
|
12 |
|
array_shift($cliArgs); |
254
|
12 |
|
$checkStdin = true; |
255
|
|
|
} |
256
|
|
|
|
257
|
12 |
|
$this->restoreDefaults(); |
258
|
|
|
$this->setCommandLineValues($cliArgs); |
259
|
|
|
|
260
|
|
|
if (isset($this->overriddenDefaults['standards']) === false |
261
|
|
|
&& Config::getConfigData('default_standard') === null |
262
|
|
|
) { |
263
|
|
|
// They did not supply a standard to use. |
264
|
|
|
// Look for a default ruleset in the current directory or higher. |
265
|
|
|
$currentDir = getcwd(); |
266
|
|
|
|
267
|
|
|
do { |
268
|
|
|
$default = $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml'; |
269
|
|
|
if (is_file($default) === true) { |
270
|
|
|
$this->standards = array($default); |
|
|
|
|
271
|
|
|
} else { |
272
|
|
|
$default = $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml.dist'; |
273
|
|
|
if (is_file($default) === true) { |
274
|
|
|
$this->standards = array($default); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$lastDir = $currentDir; |
279
|
|
|
$currentDir = dirname($currentDir); |
280
|
|
|
} while ($currentDir !== '.' && $currentDir !== $lastDir); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// Check for content on STDIN. |
284
|
|
|
if ($checkStdin === true) { |
285
|
|
|
$handle = fopen('php://stdin', 'r'); |
286
|
|
|
if (stream_set_blocking($handle, false) === true) { |
287
|
|
|
$fileContents = ''; |
288
|
|
|
while (($line = fgets(STDIN)) !== false) { |
289
|
|
|
$fileContents .= $line; |
290
|
|
|
usleep(10); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
stream_set_blocking($handle, true); |
294
|
|
|
fclose($handle); |
295
|
|
|
if (trim($fileContents) !== '') { |
296
|
|
|
$this->stdin = true; |
|
|
|
|
297
|
|
|
$this->stdinContent = $fileContents; |
|
|
|
|
298
|
|
|
$this->overriddenDefaults['stdin'] = true; |
299
|
|
|
$this->overriddenDefaults['stdinContent'] = true; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
}//end __construct() |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Set the command line values. |
309
|
|
|
* |
310
|
|
|
* @param array $args An array of command line arguments to set. |
311
|
|
|
* |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
public function setCommandLineValues($args) |
315
|
|
|
{ |
316
|
|
|
$this->cliArgs = $args; |
317
|
|
|
$numArgs = count($args); |
318
|
|
|
|
319
|
|
|
for ($i = 0; $i < $numArgs; $i++) { |
320
|
|
|
$arg = $this->cliArgs[$i]; |
321
|
|
|
if ($arg === '') { |
322
|
|
|
continue; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if ($arg{0} === '-') { |
326
|
|
|
if ($arg === '-') { |
327
|
|
|
// Asking to read from STDIN. |
328
|
|
|
$this->stdin = true; |
|
|
|
|
329
|
|
|
$this->overriddenDefaults['stdin'] = true; |
330
|
|
|
continue; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if ($arg === '--') { |
334
|
|
|
// Empty argument, ignore it. |
335
|
|
|
continue; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
if ($arg{1} === '-') { |
339
|
|
|
$this->processLongArgument(substr($arg, 2), $i); |
340
|
|
|
} else { |
341
|
|
|
$switches = str_split($arg); |
342
|
|
|
foreach ($switches as $switch) { |
343
|
|
|
if ($switch === '-') { |
344
|
|
|
continue; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$this->processShortArgument($switch, $i); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
} else { |
351
|
|
|
$this->processUnknownArgument($arg, $i); |
352
|
|
|
}//end if |
353
|
|
|
}//end for |
354
|
|
|
|
355
|
|
|
}//end setCommandLineValues() |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Restore default values for all possible command line arguments. |
360
|
|
|
* |
361
|
|
|
* @return array |
362
|
|
|
*/ |
363
|
12 |
|
public function restoreDefaults() |
364
|
|
|
{ |
365
|
12 |
|
$this->files = array(); |
|
|
|
|
366
|
12 |
|
$this->standards = array('PEAR'); |
|
|
|
|
367
|
|
|
$this->verbosity = 0; |
|
|
|
|
368
|
|
|
$this->colors = true; |
|
|
|
|
369
|
|
|
$this->explain = false; |
|
|
|
|
370
|
|
|
$this->local = false; |
|
|
|
|
371
|
|
|
$this->showSources = false; |
|
|
|
|
372
|
|
|
$this->showProgress = false; |
|
|
|
|
373
|
|
|
$this->tabWidth = 0; |
|
|
|
|
374
|
|
|
$this->extensions = array( |
|
|
|
|
375
|
|
|
'php' => 'PHP', |
376
|
|
|
'inc' => 'PHP', |
377
|
|
|
'js' => 'JS', |
378
|
|
|
'css' => 'CSS', |
379
|
|
|
); |
380
|
|
|
$this->sniffs = array(); |
|
|
|
|
381
|
|
|
$this->ignored = array(); |
|
|
|
|
382
|
|
|
$this->filter = null; |
|
|
|
|
383
|
|
|
$this->reports = array('full' => null); |
|
|
|
|
384
|
|
|
$this->errorSeverity = 5; |
|
|
|
|
385
|
|
|
$this->warningSeverity = 5; |
|
|
|
|
386
|
|
|
$this->recordErrors = true; |
|
|
|
|
387
|
|
|
$this->suffix = ''; |
|
|
|
|
388
|
|
|
$this->stdin = false; |
|
|
|
|
389
|
|
|
$this->stdinContent = null; |
|
|
|
|
390
|
|
|
$this->stdinPath = null; |
|
|
|
|
391
|
|
|
|
392
|
|
|
$standard = self::getConfigData('default_standard'); |
393
|
|
|
if ($standard !== null) { |
394
|
|
|
$this->standards = explode(',', $standard); |
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$tabWidth = self::getConfigData('tab_width'); |
398
|
|
|
if ($tabWidth !== null) { |
399
|
|
|
$this->tabWidth = (int) $tabWidth; |
|
|
|
|
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
$severity = self::getConfigData('severity'); |
403
|
|
|
if ($severity !== null) { |
404
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
405
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
$severity = self::getConfigData('error_severity'); |
409
|
|
|
if ($severity !== null) { |
410
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$severity = self::getConfigData('warning_severity'); |
414
|
|
|
if ($severity !== null) { |
415
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
$showWarnings = self::getConfigData('show_warnings'); |
419
|
|
|
if ($showWarnings !== null) { |
420
|
|
|
$showWarnings = (bool) $showWarnings; |
421
|
|
|
if ($showWarnings === false) { |
422
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
$showProgress = self::getConfigData('show_progress'); |
427
|
|
|
if ($showProgress !== null) { |
428
|
|
|
$this->showProgress = (bool) $showProgress; |
|
|
|
|
429
|
|
|
} |
430
|
|
|
}//end restoreDefaults() |
431
|
|
|
|
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Processes a short (-e) command line argument. |
435
|
|
|
* |
436
|
|
|
* @param string $arg The command line argument. |
437
|
|
|
* @param int $pos The position of the argument on the command line. |
438
|
|
|
* |
439
|
|
|
* @return void |
440
|
|
|
*/ |
441
|
|
|
public function processShortArgument($arg, $pos) |
442
|
|
|
{ |
443
|
|
|
switch ($arg) { |
444
|
|
|
case 'h': |
445
|
|
|
case '?': |
446
|
|
|
$this->printUsage(); |
447
|
|
|
exit(0); |
|
|
|
|
448
|
|
|
case 'i' : |
|
|
|
|
449
|
|
|
Util\Standards::printInstalledStandards(); |
450
|
|
|
exit(0); |
|
|
|
|
451
|
|
|
case 'v' : |
|
|
|
|
452
|
|
|
$this->verbosity++; |
|
|
|
|
453
|
|
|
$this->overriddenDefaults['verbosity'] = true; |
454
|
|
|
break; |
455
|
|
|
case 'l' : |
|
|
|
|
456
|
|
|
$this->local = true; |
|
|
|
|
457
|
|
|
$this->overriddenDefaults['local'] = true; |
458
|
|
|
break; |
459
|
|
|
case 's' : |
|
|
|
|
460
|
|
|
$this->showSources = true; |
|
|
|
|
461
|
|
|
$this->overriddenDefaults['showSources'] = true; |
462
|
|
|
break; |
463
|
|
|
case 'e': |
464
|
|
|
$this->explain = true; |
|
|
|
|
465
|
|
|
$this->overriddenDefaults['explain'] = true; |
466
|
|
|
break; |
467
|
|
|
case 'p' : |
|
|
|
|
468
|
|
|
$this->showProgress = true; |
|
|
|
|
469
|
|
|
$this->overriddenDefaults['showProgress'] = true; |
470
|
|
|
break; |
471
|
|
|
case 'm' : |
|
|
|
|
472
|
|
|
$this->recordErrors = false; |
|
|
|
|
473
|
|
|
$this->overriddenDefaults['recordErrors'] = true; |
474
|
|
|
break; |
475
|
|
|
case 'd' : |
|
|
|
|
476
|
|
|
$ini = explode('=', $this->cliArgs[($pos + 1)]); |
477
|
|
|
$this->cliArgs[($pos + 1)] = ''; |
478
|
|
|
if (isset($ini[1]) === true) { |
479
|
|
|
ini_set($ini[0], $ini[1]); |
480
|
|
|
} else { |
481
|
|
|
ini_set($ini[0], true); |
482
|
|
|
} |
483
|
|
|
break; |
484
|
|
View Code Duplication |
case 'n' : |
|
|
|
|
485
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
486
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
487
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
488
|
|
|
} |
489
|
|
|
break; |
490
|
|
View Code Duplication |
case 'w' : |
|
|
|
|
491
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
492
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
493
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
494
|
|
|
} |
495
|
|
|
break; |
496
|
|
|
default: |
497
|
|
|
$this->processUnknownArgument('-'.$arg, $pos); |
498
|
|
|
}//end switch |
499
|
|
|
|
500
|
|
|
}//end processShortArgument() |
501
|
|
|
|
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Processes a long (--example) command line argument. |
505
|
|
|
* |
506
|
|
|
* @param string $arg The command line argument. |
507
|
|
|
* @param int $pos The position of the argument on the command line. |
508
|
|
|
* |
509
|
|
|
* @return void |
510
|
|
|
*/ |
511
|
|
|
public function processLongArgument($arg, $pos) |
512
|
|
|
{ |
513
|
|
|
switch ($arg) { |
514
|
|
|
case 'help': |
515
|
|
|
$this->printUsage(); |
516
|
|
|
exit(0); |
|
|
|
|
517
|
|
|
case 'version': |
518
|
|
|
echo 'Symplify\PHP7_CodeSniffer version '.self::VERSION; |
519
|
|
|
exit(0); |
|
|
|
|
520
|
|
|
case 'cache': |
521
|
|
|
$this->cache = true; |
|
|
|
|
522
|
|
|
$this->overriddenDefaults['cache'] = true; |
523
|
|
|
break; |
524
|
|
|
case 'no-cache': |
525
|
|
|
$this->cache = false; |
|
|
|
|
526
|
|
|
$this->overriddenDefaults['cache'] = true; |
527
|
|
|
break; |
528
|
|
|
default: |
529
|
|
|
if (substr($arg, 0, 7) === 'sniffs=') { |
530
|
|
|
$sniffs = explode(',', substr($arg, 7)); |
531
|
|
|
foreach ($sniffs as $sniff) { |
532
|
|
|
if (substr_count($sniff, '.') !== 2) { |
533
|
|
|
echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL; |
534
|
|
|
$this->printUsage(); |
535
|
|
|
exit(2); |
|
|
|
|
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
$this->sniffs = $sniffs; |
|
|
|
|
540
|
|
|
$this->overriddenDefaults['sniffs'] = true; |
541
|
|
|
} else if (substr($arg, 0, 11) === 'stdin-path=') { |
542
|
|
|
$this->stdinPath = Util\Common::realpath(substr($arg, 11)); |
|
|
|
|
543
|
|
|
|
544
|
|
|
// It may not exist and return false instead, so use whatever they gave us. |
545
|
|
|
if ($this->stdinPath === false) { |
|
|
|
|
546
|
|
|
$this->stdinPath = trim(substr($arg, 11)); |
|
|
|
|
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
$this->overriddenDefaults['stdinPath'] = true; |
550
|
|
|
} else if (substr($arg, 0, 9) === 'basepath=') { |
551
|
|
|
if (isset($this->overriddenDefaults['basepath']) === true) { |
552
|
|
|
break; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
$this->basepath = Util\Common::realpath(substr($arg, 9)); |
|
|
|
|
556
|
|
|
|
557
|
|
|
// It may not exist and return false instead. |
558
|
|
|
if ($this->basepath === false) { |
|
|
|
|
559
|
|
|
$this->basepath = substr($arg, 9); |
|
|
|
|
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
$this->overriddenDefaults['basepath'] = true; |
563
|
|
|
|
564
|
|
|
if (is_dir($this->basepath) === false) { |
|
|
|
|
565
|
|
|
echo 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
566
|
|
|
$this->printUsage(); |
567
|
|
|
exit(2); |
|
|
|
|
568
|
|
|
} |
569
|
|
|
} else if (substr($arg, 0, 7) === 'filter=') { |
570
|
|
|
if (isset($this->overriddenDefaults['filter']) === true) { |
571
|
|
|
break; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
$this->filter = substr($arg, 7); |
|
|
|
|
575
|
|
|
$this->overriddenDefaults['filter'] = true; |
576
|
|
|
} else if (substr($arg, 0, 9) === 'standard=') { |
577
|
|
|
$standards = trim(substr($arg, 9)); |
578
|
|
|
if ($standards !== '') { |
579
|
|
|
$this->standards = explode(',', $standards); |
|
|
|
|
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
$this->overriddenDefaults['standards'] = true; |
583
|
|
|
} else if (substr($arg, 0, 11) === 'extensions=') { |
584
|
|
|
$extensions = explode(',', substr($arg, 11)); |
585
|
|
|
$newExtensions = array(); |
586
|
|
|
foreach ($extensions as $ext) { |
587
|
|
|
$slash = strpos($ext, '/'); |
588
|
|
|
if ($slash !== false) { |
589
|
|
|
// They specified the tokenizer too. |
590
|
|
|
list($ext, $tokenizer) = explode('/', $ext); |
591
|
|
|
$newExtensions[$ext] = strtoupper($tokenizer); |
592
|
|
|
continue; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
if (isset($this->extensions[$ext]) === true) { |
|
|
|
|
596
|
|
|
$newExtensions[$ext] = $this->extensions[$ext]; |
|
|
|
|
597
|
|
|
} else { |
598
|
|
|
$newExtensions[$ext] = 'PHP'; |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
$this->extensions = $newExtensions; |
|
|
|
|
603
|
|
|
$this->overriddenDefaults['extensions'] = true; |
604
|
|
|
} else if (substr($arg, 0, 7) === 'suffix=') { |
605
|
|
|
$this->suffix = explode(',', substr($arg, 7)); |
|
|
|
|
606
|
|
|
$this->overriddenDefaults['suffix'] = true; |
607
|
|
|
} else if (substr($arg, 0, 9) === 'severity=') { |
608
|
|
|
$this->errorSeverity = (int) substr($arg, 9); |
|
|
|
|
609
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
610
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
611
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
612
|
|
|
} else if (substr($arg, 0, 15) === 'error-severity=') { |
613
|
|
|
$this->errorSeverity = (int) substr($arg, 15); |
|
|
|
|
614
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
615
|
|
|
} else if (substr($arg, 0, 17) === 'warning-severity=') { |
616
|
|
|
$this->warningSeverity = (int) substr($arg, 17); |
|
|
|
|
617
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
618
|
|
|
} else if (substr($arg, 0, 7) === 'ignore=') { |
619
|
|
|
// Split the ignore string on commas, unless the comma is escaped |
620
|
|
|
// using 1 or 3 slashes (\, or \\\,). |
|
|
|
|
621
|
|
|
$patterns = preg_split( |
622
|
|
|
'/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', |
623
|
|
|
substr($arg, 7) |
624
|
|
|
); |
625
|
|
|
|
626
|
|
|
$ignored = array(); |
627
|
|
|
foreach ($patterns as $pattern) { |
628
|
|
|
$pattern = trim($pattern); |
629
|
|
|
if ($pattern === '') { |
630
|
|
|
continue; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
$ignored[$pattern] = 'absolute'; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
$this->ignored = $ignored; |
|
|
|
|
637
|
|
|
$this->overriddenDefaults['ignored'] = true; |
638
|
|
|
} else if (substr($arg, 0, 10) === 'tab-width=') { |
639
|
|
|
$this->tabWidth = (int) substr($arg, 10); |
|
|
|
|
640
|
|
|
$this->overriddenDefaults['tabWidth'] = true; |
641
|
|
|
} else { |
642
|
|
|
$this->processUnknownArgument('--'.$arg, $pos); |
643
|
|
|
}//end if |
644
|
|
|
|
645
|
|
|
break; |
646
|
|
|
}//end switch |
647
|
|
|
|
648
|
|
|
}//end processLongArgument() |
649
|
|
|
|
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Processes an unknown command line argument. |
653
|
|
|
* |
654
|
|
|
* Assumes all unknown arguments are files and folders to check. |
655
|
|
|
* |
656
|
|
|
* @param string $arg The command line argument. |
657
|
|
|
* @param int $pos The position of the argument on the command line. |
658
|
|
|
* |
659
|
|
|
* @return void |
660
|
|
|
*/ |
661
|
|
|
public function processUnknownArgument($arg, $pos) |
|
|
|
|
662
|
|
|
{ |
663
|
|
|
// If we are processing STDIN, don't record any files to check. |
664
|
|
|
if ($this->stdin === true) { |
|
|
|
|
665
|
|
|
return; |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
// We don't know about any additional switches; just files. |
669
|
|
|
if ($arg{0} === '-') { |
670
|
|
|
echo "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL; |
671
|
|
|
$this->printUsage(); |
672
|
|
|
exit(2); |
|
|
|
|
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
$file = Util\Common::realpath($arg); |
676
|
|
|
if (file_exists($file) === false) { |
677
|
|
|
echo 'ERROR: The file "'.$arg.'" does not exist.'.PHP_EOL.PHP_EOL; |
678
|
|
|
$this->printUsage(); |
679
|
|
|
exit(2); |
|
|
|
|
680
|
|
|
} else { |
681
|
|
|
$files = $this->files; |
|
|
|
|
682
|
|
|
$files[] = $file; |
683
|
|
|
$this->files = $files; |
|
|
|
|
684
|
|
|
$this->overriddenDefaults['files'] = true; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
}//end processUnknownArgument() |
688
|
|
|
|
689
|
|
|
|
690
|
|
|
/** |
691
|
|
|
* Prints out the usage information for this script. |
692
|
|
|
* |
693
|
|
|
* @return void |
694
|
|
|
*/ |
695
|
|
|
public function printUsage() |
696
|
|
|
{ |
697
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
698
|
|
|
$this->printPHPCBFUsage(); |
699
|
|
|
} else { |
700
|
|
|
$this->printPHPCSUsage(); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
}//end printUsage() |
704
|
|
|
|
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* Prints out the usage information for PHPCS. |
708
|
|
|
* |
709
|
|
|
* @return void |
710
|
|
|
*/ |
711
|
|
|
public function printPHPCSUsage() |
712
|
|
|
{ |
713
|
|
|
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]]'.PHP_EOL; |
714
|
|
|
echo ' [--basepath=<basepath>] [--tab-width=<tabWidth>]'.PHP_EOL; |
715
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
716
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>]'.PHP_EOL; |
717
|
|
|
echo ' [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
718
|
|
|
echo ' - Check STDIN instead of local files and directories'.PHP_EOL; |
719
|
|
|
echo ' -n Do not print warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
720
|
|
|
echo ' -w Print both warnings and errors (this is the default)'.PHP_EOL; |
721
|
|
|
echo ' -s Show sniff codes in all reports'.PHP_EOL; |
722
|
|
|
echo ' -a Run interactively'.PHP_EOL; |
723
|
|
|
echo ' -e Explain a standard by showing the sniffs it includes'.PHP_EOL; |
724
|
|
|
echo ' -p Show progress of the run'.PHP_EOL; |
725
|
|
|
echo ' -m Stop error messages from being recorded'.PHP_EOL; |
726
|
|
|
echo ' (saves a lot of memory, but stops many reports from being used)'.PHP_EOL; |
727
|
|
|
echo ' -v[v][v] Print verbose output'.PHP_EOL; |
728
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
729
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
730
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
731
|
|
|
echo ' --version Print version information'.PHP_EOL; |
732
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
733
|
|
|
echo ' <file> One or more files and/or directories to check'.PHP_EOL; |
734
|
|
|
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL; |
735
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
736
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
737
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
738
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
739
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the check to'.PHP_EOL; |
740
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
741
|
|
|
echo ' <severity> The minimum severity required to display an error or warning'.PHP_EOL; |
742
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
743
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
744
|
|
|
|
745
|
|
|
}//end printPHPCSUsage() |
746
|
|
|
|
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* Prints out the usage information for PHPCBF. |
750
|
|
|
* |
751
|
|
|
* @return void |
752
|
|
|
*/ |
753
|
|
|
public function printPHPCBFUsage() |
754
|
|
|
{ |
755
|
|
|
echo 'Usage: phpcbf [-nwli] [-d key[=value]]'.PHP_EOL; |
756
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--suffix=<suffix>]'.PHP_EOL; |
757
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
758
|
|
|
echo ' [--tab-width=<tabWidth>]'.PHP_EOL; |
759
|
|
|
echo ' [--basepath=<basepath>] [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
760
|
|
|
echo ' - Fix STDIN instead of local files and directories'.PHP_EOL; |
761
|
|
|
echo ' -n Do not fix warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
762
|
|
|
echo ' -w Fix both warnings and errors (on by default)'.PHP_EOL; |
763
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
764
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
765
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
766
|
|
|
echo ' --version Print version information'.PHP_EOL; |
767
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
768
|
|
|
echo ' <file> One or more files and/or directories to fix'.PHP_EOL; |
769
|
|
|
echo ' <extensions> A comma separated list of file extensions to fix'.PHP_EOL; |
770
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
771
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
772
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
773
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
774
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the fixes to'.PHP_EOL; |
775
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
776
|
|
|
echo ' <severity> The minimum severity required to fix an error or warning'.PHP_EOL; |
777
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
778
|
|
|
echo ' <suffix> Write modified files to a filename using this suffix'.PHP_EOL; |
779
|
|
|
echo ' ("diff" and "patch" are not used in this mode)'.PHP_EOL; |
780
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
781
|
|
|
|
782
|
|
|
}//end printPHPCBFUsage() |
783
|
|
|
|
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Get a single config value. |
787
|
|
|
* |
788
|
|
|
* @param string $key The name of the config value. |
789
|
|
|
* |
790
|
|
|
* @return string|null |
791
|
|
|
* @see setConfigData() |
792
|
|
|
* @see getAllConfigData() |
793
|
|
|
*/ |
794
|
12 |
|
public static function getConfigData($key) |
795
|
|
|
{ |
796
|
12 |
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
|
|
|
|
797
|
|
|
|
798
|
|
|
if ($phpCodeSnifferConfig === null) { |
799
|
|
|
return null; |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
if (isset($phpCodeSnifferConfig[$key]) === false) { |
803
|
|
|
return null; |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
return $phpCodeSnifferConfig[$key]; |
807
|
|
|
|
808
|
|
|
}//end getConfigData() |
809
|
|
|
|
810
|
|
|
|
811
|
|
|
/** |
812
|
|
|
* Get the path to an executable utility. |
813
|
|
|
* |
814
|
|
|
* @param string $name The name of the executable utility. |
815
|
|
|
* |
816
|
|
|
* @return string|null |
817
|
|
|
* @see getConfigData() |
818
|
|
|
*/ |
819
|
|
|
public static function getExecutablePath($name) |
820
|
|
|
{ |
821
|
|
|
$data = self::getConfigData($name.'_path'); |
822
|
|
|
if ($data !== null) { |
823
|
|
|
return $data; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
if (array_key_exists($name, self::$executablePaths) === true) { |
827
|
|
|
return self::$executablePaths[$name]; |
828
|
|
|
} |
829
|
|
|
|
830
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
831
|
|
|
$cmd = 'where '.escapeshellarg($name).' 2> nul'; |
832
|
|
|
} else { |
833
|
|
|
$cmd = 'which '.escapeshellarg($name); |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
$result = exec($cmd, $output, $retVal); |
837
|
|
|
if ($retVal !== 0) { |
838
|
|
|
$result = null; |
839
|
|
|
} |
840
|
|
|
|
841
|
|
|
self::$executablePaths[$name] = $result; |
842
|
|
|
return $result; |
843
|
|
|
|
844
|
|
|
}//end getExecutablePath() |
845
|
|
|
|
846
|
|
|
|
847
|
|
|
}//end class |
848
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.