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