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 explain Explain the coding standards. |
55
|
|
|
* bool local Process local files in directories only (no recursion). |
56
|
|
|
* bool showSources Show sniff source codes in report output. |
57
|
|
|
* bool showProgress Show basic progress information while running. |
58
|
|
|
* int tabWidth How many spaces each tab is worth. |
59
|
|
|
* string[] sniffs The sniffs that should be used for checking. |
60
|
|
|
* If empty, all sniffs in the supplied standards will be used. |
61
|
|
|
* string[] ignored Regular expressions used to ignore files and folders during checking. |
62
|
|
|
* string reportFile A file where the report output should be written. |
63
|
|
|
* string generator The documentation generator to use. |
64
|
|
|
* string filter The filter to use for the run. |
65
|
|
|
* string[] bootstrap One of more files to include before the run begins. |
66
|
|
|
* int reportWidth The maximum number of columns that reports should use for output. |
67
|
|
|
* Set to "auto" for have this value changed to the width of the terminal. |
68
|
|
|
* int errorSeverity The minimum severity an error must have to be displayed. |
69
|
|
|
* int warningSeverity The minimum severity a warning must have to be displayed. |
70
|
|
|
* bool recordErrors Record the content of error messages as well as error counts. |
71
|
|
|
* string suffix A suffix to add to fixed files. |
72
|
|
|
* string basepath A file system location to strip from the paths of files shown in reports. |
73
|
|
|
* bool stdin Read content from STDIN instead of supplied files. |
74
|
|
|
* string stdinContent Content passed directly to PHPCS on STDIN. |
75
|
|
|
* string stdinPath The path to use for content passed on STDIN. |
76
|
|
|
* |
77
|
|
|
* array<string, string> extensions File extensions that should be checked, and what tokenizer to use. |
78
|
|
|
* E.g., array('inc' => 'PHP'); |
79
|
|
|
* array<string, string|null> reports The reports to use for printing output after the run. |
80
|
|
|
* The format of the array is: |
81
|
|
|
* array( |
82
|
|
|
* 'reportName1' => 'outputFile', |
83
|
|
|
* 'reportName2' => null, |
84
|
|
|
* ); |
85
|
|
|
* If the array value is NULL, the report will be written to the screen. |
86
|
|
|
* |
87
|
|
|
* @var array<string, mixed> |
88
|
|
|
*/ |
89
|
|
|
private $settings = array( |
90
|
|
|
'files' => null, |
91
|
|
|
'standards' => null, |
92
|
|
|
'verbosity' => null, |
93
|
|
|
'interactive' => null, |
94
|
|
|
'parallel' => null, |
95
|
|
|
'explain' => null, |
96
|
|
|
'local' => null, |
97
|
|
|
'showSources' => null, |
98
|
|
|
'showProgress' => null, |
99
|
|
|
'tabWidth' => null, |
100
|
|
|
'extensions' => null, |
101
|
|
|
'sniffs' => null, |
102
|
|
|
'ignored' => null, |
103
|
|
|
'reportFile' => null, |
104
|
|
|
'generator' => null, |
105
|
|
|
'filter' => null, |
106
|
|
|
'bootstrap' => null, |
107
|
|
|
'reports' => null, |
108
|
|
|
'basepath' => null, |
109
|
|
|
'reportWidth' => null, |
110
|
|
|
'errorSeverity' => null, |
111
|
|
|
'warningSeverity' => null, |
112
|
|
|
'recordErrors' => null, |
113
|
|
|
'suffix' => null, |
114
|
|
|
'stdin' => null, |
115
|
|
|
'stdinContent' => null, |
116
|
|
|
'stdinPath' => null, |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Whether or not to kill the process when an unknown command line arg is found. |
121
|
|
|
* |
122
|
|
|
* If FALSE, arguments that are not command line options or file/directory paths |
123
|
|
|
* will be ignored and execution will continue. |
124
|
|
|
* |
125
|
|
|
* @var boolean |
126
|
|
|
*/ |
127
|
|
|
public $dieOnUnknownArg; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* The current command line arguments we are processing. |
131
|
|
|
* |
132
|
|
|
* @var string[] |
133
|
|
|
*/ |
134
|
|
|
private $cliArgs = array(); |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Command line values that the user has supplied directly. |
138
|
|
|
* |
139
|
|
|
* @var array<string, TRUE> |
140
|
|
|
*/ |
141
|
|
|
private $overriddenDefaults = array(); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Unknown arguments |
145
|
|
|
* |
146
|
|
|
* @var array<mixed> |
147
|
|
|
*/ |
148
|
|
|
private $values = array(); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Config file data that has been loaded for the run. |
152
|
|
|
* |
153
|
|
|
* @var array<string, string> |
154
|
|
|
*/ |
155
|
|
|
private static $configData = null; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Automatically discovered executable utility paths. |
159
|
|
|
* |
160
|
|
|
* @var array<string, string> |
161
|
|
|
*/ |
162
|
|
|
private static $executablePaths = array(); |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the value of an inaccessible property. |
167
|
|
|
* |
168
|
|
|
* @param string $name The name of the property. |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
* @throws RuntimeException If the setting name is invalid. |
172
|
|
|
*/ |
173
|
|
|
public function __get($name) |
174
|
|
|
{ |
175
|
|
|
if (array_key_exists($name, $this->settings) === false) { |
176
|
|
|
throw new RuntimeException("ERROR: unable to get value of property \"$name\""); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->settings[$name]; |
180
|
|
|
|
181
|
|
|
}//end __get() |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set the value of an inaccessible property. |
186
|
|
|
* |
187
|
|
|
* @param string $name The name of the property. |
188
|
|
|
* @param mixed $value The value of the property. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
* @throws RuntimeException If the setting name is invalid. |
192
|
|
|
*/ |
193
|
12 |
|
public function __set($name, $value) |
194
|
|
|
{ |
195
|
12 |
|
if (array_key_exists($name, $this->settings) === false) { |
196
|
12 |
|
throw new RuntimeException("Can't __set() $name; setting doesn't exist"); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
switch ($name) { |
200
|
12 |
|
case 'reportWidth' : |
|
|
|
|
201
|
|
|
// Support auto terminal width. |
202
|
|
|
if ($value === 'auto' && preg_match('|\d+ (\d+)|', shell_exec('stty size 2>&1'), $matches) === 1) { |
203
|
|
|
$value = (int) $matches[1]; |
204
|
|
|
} else { |
205
|
|
|
$value = (int) $value; |
206
|
|
|
} |
207
|
|
|
break; |
208
|
12 |
|
case 'standards' : |
|
|
|
|
209
|
12 |
|
$cleaned = array(); |
210
|
|
|
|
211
|
|
|
// Check if the standard name is valid, or if the case is invalid. |
212
|
12 |
|
$installedStandards = Util\Standards::getInstalledStandards(); |
213
|
12 |
|
foreach ($value as $standard) { |
214
|
12 |
|
foreach ($installedStandards as $validStandard) { |
215
|
|
|
if (strtolower($standard) === strtolower($validStandard)) { |
216
|
|
|
$standard = $validStandard; |
217
|
|
|
break; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
12 |
|
$cleaned[] = $standard; |
222
|
|
|
} |
223
|
|
|
|
224
|
12 |
|
$value = $cleaned; |
225
|
12 |
|
break; |
226
|
|
|
default : |
|
|
|
|
227
|
|
|
// No validation required. |
228
|
12 |
|
break; |
229
|
|
|
}//end switch |
230
|
|
|
|
231
|
12 |
|
$this->settings[$name] = $value; |
232
|
|
|
|
233
|
12 |
|
}//end __set() |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Check if the value of an inaccessible property is set. |
238
|
|
|
* |
239
|
|
|
* @param string $name The name of the property. |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public function __isset($name) |
244
|
|
|
{ |
245
|
|
|
return isset($this->settings[$name]); |
246
|
|
|
|
247
|
|
|
}//end __isset() |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Unset the value of an inaccessible property. |
252
|
|
|
* |
253
|
|
|
* @param string $name The name of the property. |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function __unset($name) |
258
|
|
|
{ |
259
|
|
|
$this->settings[$name] = null; |
260
|
|
|
|
261
|
|
|
}//end __unset() |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Creates a Config object and populates it with command line values. |
266
|
|
|
* |
267
|
|
|
* @param array $cliArgs An array of values gathered from CLI args. |
268
|
|
|
* @param bool $dieOnUnknownArg Whether or not to kill the process when an |
269
|
|
|
* unknown command line arg is found. |
270
|
|
|
* |
271
|
|
|
* @return void |
|
|
|
|
272
|
|
|
*/ |
273
|
12 |
|
public function __construct(array $cliArgs=array(), $dieOnUnknownArg=true) |
|
|
|
|
274
|
|
|
{ |
275
|
12 |
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === true) { |
276
|
|
|
// Let everything through during testing so that we can |
277
|
|
|
// make use of PHPUnit command line arguments as well. |
278
|
|
|
$this->dieOnUnknownArg = false; |
279
|
|
|
} else { |
280
|
12 |
|
$this->dieOnUnknownArg = $dieOnUnknownArg; |
281
|
|
|
} |
282
|
|
|
|
283
|
12 |
|
$checkStdin = false; |
284
|
12 |
|
if (empty($cliArgs) === true) { |
285
|
12 |
|
$cliArgs = $_SERVER['argv']; |
286
|
12 |
|
array_shift($cliArgs); |
287
|
12 |
|
$checkStdin = true; |
288
|
|
|
} |
289
|
|
|
|
290
|
12 |
|
$this->restoreDefaults(); |
291
|
|
|
$this->setCommandLineValues($cliArgs); |
292
|
|
|
|
293
|
|
|
if (isset($this->overriddenDefaults['standards']) === false |
294
|
|
|
&& Config::getConfigData('default_standard') === null |
295
|
|
|
) { |
296
|
|
|
// They did not supply a standard to use. |
297
|
|
|
// Look for a default ruleset in the current directory or higher. |
298
|
|
|
$currentDir = getcwd(); |
299
|
|
|
|
300
|
|
|
do { |
301
|
|
|
$default = $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml'; |
302
|
|
|
if (is_file($default) === true) { |
303
|
|
|
$this->standards = array($default); |
|
|
|
|
304
|
|
|
} else { |
305
|
|
|
$default = $currentDir.DIRECTORY_SEPARATOR.'phpcs.xml.dist'; |
306
|
|
|
if (is_file($default) === true) { |
307
|
|
|
$this->standards = array($default); |
|
|
|
|
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$lastDir = $currentDir; |
312
|
|
|
$currentDir = dirname($currentDir); |
313
|
|
|
} while ($currentDir !== '.' && $currentDir !== $lastDir); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
// Check for content on STDIN. |
317
|
|
|
if ($checkStdin === true) { |
318
|
|
|
$handle = fopen('php://stdin', 'r'); |
319
|
|
|
if (stream_set_blocking($handle, false) === true) { |
320
|
|
|
$fileContents = ''; |
321
|
|
|
while (($line = fgets(STDIN)) !== false) { |
322
|
|
|
$fileContents .= $line; |
323
|
|
|
usleep(10); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
stream_set_blocking($handle, true); |
327
|
|
|
fclose($handle); |
328
|
|
|
if (trim($fileContents) !== '') { |
329
|
|
|
$this->stdin = true; |
|
|
|
|
330
|
|
|
$this->stdinContent = $fileContents; |
|
|
|
|
331
|
|
|
$this->overriddenDefaults['stdin'] = true; |
332
|
|
|
$this->overriddenDefaults['stdinContent'] = true; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
}//end __construct() |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Set the command line values. |
342
|
|
|
* |
343
|
|
|
* @param array $args An array of command line arguments to set. |
344
|
|
|
* |
345
|
|
|
* @return void |
346
|
|
|
*/ |
347
|
|
|
public function setCommandLineValues($args) |
348
|
|
|
{ |
349
|
|
|
$this->cliArgs = $args; |
350
|
|
|
$numArgs = count($args); |
351
|
|
|
|
352
|
|
|
for ($i = 0; $i < $numArgs; $i++) { |
353
|
|
|
$arg = $this->cliArgs[$i]; |
354
|
|
|
if ($arg === '') { |
355
|
|
|
continue; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if ($arg{0} === '-') { |
359
|
|
|
if ($arg === '-') { |
360
|
|
|
// Asking to read from STDIN. |
361
|
|
|
$this->stdin = true; |
|
|
|
|
362
|
|
|
$this->overriddenDefaults['stdin'] = true; |
363
|
|
|
continue; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if ($arg === '--') { |
367
|
|
|
// Empty argument, ignore it. |
368
|
|
|
continue; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if ($arg{1} === '-') { |
372
|
|
|
$this->processLongArgument(substr($arg, 2), $i); |
373
|
|
|
} else { |
374
|
|
|
$switches = str_split($arg); |
375
|
|
|
foreach ($switches as $switch) { |
376
|
|
|
if ($switch === '-') { |
377
|
|
|
continue; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$this->processShortArgument($switch, $i); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
} else { |
384
|
|
|
$this->processUnknownArgument($arg, $i); |
385
|
|
|
}//end if |
386
|
|
|
}//end for |
387
|
|
|
|
388
|
|
|
}//end setCommandLineValues() |
389
|
|
|
|
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Restore default values for all possible command line arguments. |
393
|
|
|
* |
394
|
|
|
* @return array |
395
|
|
|
*/ |
396
|
12 |
|
public function restoreDefaults() |
397
|
|
|
{ |
398
|
12 |
|
$this->files = array(); |
|
|
|
|
399
|
12 |
|
$this->standards = array('PEAR'); |
|
|
|
|
400
|
12 |
|
$this->verbosity = 0; |
|
|
|
|
401
|
12 |
|
$this->interactive = false; |
|
|
|
|
402
|
12 |
|
$this->colors = true; |
|
|
|
|
403
|
|
|
$this->explain = false; |
|
|
|
|
404
|
|
|
$this->local = false; |
|
|
|
|
405
|
|
|
$this->showSources = false; |
|
|
|
|
406
|
|
|
$this->showProgress = false; |
|
|
|
|
407
|
|
|
$this->parallel = 1; |
|
|
|
|
408
|
|
|
$this->tabWidth = 0; |
|
|
|
|
409
|
|
|
$this->extensions = array( |
|
|
|
|
410
|
|
|
'php' => 'PHP', |
411
|
|
|
'inc' => 'PHP', |
412
|
|
|
'js' => 'JS', |
413
|
|
|
'css' => 'CSS', |
414
|
|
|
); |
415
|
|
|
$this->sniffs = array(); |
|
|
|
|
416
|
|
|
$this->ignored = array(); |
|
|
|
|
417
|
|
|
$this->filter = null; |
|
|
|
|
418
|
|
|
$this->bootstrap = array(); |
|
|
|
|
419
|
|
|
$this->reports = array('full' => null); |
|
|
|
|
420
|
|
|
$this->errorSeverity = 5; |
|
|
|
|
421
|
|
|
$this->warningSeverity = 5; |
|
|
|
|
422
|
|
|
$this->recordErrors = true; |
|
|
|
|
423
|
|
|
$this->suffix = ''; |
|
|
|
|
424
|
|
|
$this->stdin = false; |
|
|
|
|
425
|
|
|
$this->stdinContent = null; |
|
|
|
|
426
|
|
|
$this->stdinPath = null; |
|
|
|
|
427
|
|
|
|
428
|
|
|
$standard = self::getConfigData('default_standard'); |
429
|
|
|
if ($standard !== null) { |
430
|
|
|
$this->standards = explode(',', $standard); |
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
$tabWidth = self::getConfigData('tab_width'); |
434
|
|
|
if ($tabWidth !== null) { |
435
|
|
|
$this->tabWidth = (int) $tabWidth; |
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$severity = self::getConfigData('severity'); |
439
|
|
|
if ($severity !== null) { |
440
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
441
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$severity = self::getConfigData('error_severity'); |
445
|
|
|
if ($severity !== null) { |
446
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
$severity = self::getConfigData('warning_severity'); |
450
|
|
|
if ($severity !== null) { |
451
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
$showWarnings = self::getConfigData('show_warnings'); |
455
|
|
|
if ($showWarnings !== null) { |
456
|
|
|
$showWarnings = (bool) $showWarnings; |
457
|
|
|
if ($showWarnings === false) { |
458
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
$showProgress = self::getConfigData('show_progress'); |
463
|
|
|
if ($showProgress !== null) { |
464
|
|
|
$this->showProgress = (bool) $showProgress; |
|
|
|
|
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
468
|
|
|
$cache = self::getConfigData('cache'); |
469
|
|
|
if ($cache !== null) { |
470
|
|
|
$this->cache = (bool) $cache; |
|
|
|
|
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$parallel = self::getConfigData('parallel'); |
474
|
|
|
if ($parallel !== null) { |
475
|
|
|
$this->parallel = max((int) $parallel, 1); |
|
|
|
|
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
}//end restoreDefaults() |
480
|
|
|
|
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Processes a short (-e) command line argument. |
484
|
|
|
* |
485
|
|
|
* @param string $arg The command line argument. |
486
|
|
|
* @param int $pos The position of the argument on the command line. |
487
|
|
|
* |
488
|
|
|
* @return void |
489
|
|
|
*/ |
490
|
|
|
public function processShortArgument($arg, $pos) |
491
|
|
|
{ |
492
|
|
|
switch ($arg) { |
493
|
|
|
case 'h': |
494
|
|
|
case '?': |
495
|
|
|
$this->printUsage(); |
496
|
|
|
exit(0); |
|
|
|
|
497
|
|
|
case 'i' : |
|
|
|
|
498
|
|
|
Util\Standards::printInstalledStandards(); |
499
|
|
|
exit(0); |
|
|
|
|
500
|
|
|
case 'v' : |
|
|
|
|
501
|
|
|
$this->verbosity++; |
|
|
|
|
502
|
|
|
$this->overriddenDefaults['verbosity'] = true; |
503
|
|
|
break; |
504
|
|
|
case 'l' : |
|
|
|
|
505
|
|
|
$this->local = true; |
|
|
|
|
506
|
|
|
$this->overriddenDefaults['local'] = true; |
507
|
|
|
break; |
508
|
|
|
case 's' : |
|
|
|
|
509
|
|
|
$this->showSources = true; |
|
|
|
|
510
|
|
|
$this->overriddenDefaults['showSources'] = true; |
511
|
|
|
break; |
512
|
|
|
case 'a' : |
|
|
|
|
513
|
|
|
$this->interactive = true; |
|
|
|
|
514
|
|
|
$this->overriddenDefaults['interactive'] = true; |
515
|
|
|
break; |
516
|
|
|
case 'e': |
517
|
|
|
$this->explain = true; |
|
|
|
|
518
|
|
|
$this->overriddenDefaults['explain'] = true; |
519
|
|
|
break; |
520
|
|
|
case 'p' : |
|
|
|
|
521
|
|
|
$this->showProgress = true; |
|
|
|
|
522
|
|
|
$this->overriddenDefaults['showProgress'] = true; |
523
|
|
|
break; |
524
|
|
|
case 'm' : |
|
|
|
|
525
|
|
|
$this->recordErrors = false; |
|
|
|
|
526
|
|
|
$this->overriddenDefaults['recordErrors'] = true; |
527
|
|
|
break; |
528
|
|
|
case 'd' : |
|
|
|
|
529
|
|
|
$ini = explode('=', $this->cliArgs[($pos + 1)]); |
530
|
|
|
$this->cliArgs[($pos + 1)] = ''; |
531
|
|
|
if (isset($ini[1]) === true) { |
532
|
|
|
ini_set($ini[0], $ini[1]); |
533
|
|
|
} else { |
534
|
|
|
ini_set($ini[0], true); |
535
|
|
|
} |
536
|
|
|
break; |
537
|
|
View Code Duplication |
case 'n' : |
|
|
|
|
538
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
539
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
540
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
541
|
|
|
} |
542
|
|
|
break; |
543
|
|
View Code Duplication |
case 'w' : |
|
|
|
|
544
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
545
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
546
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
547
|
|
|
} |
548
|
|
|
break; |
549
|
|
|
default: |
550
|
|
|
if ($this->dieOnUnknownArg === false) { |
551
|
|
|
$this->values[$arg] = $arg; |
552
|
|
|
} else { |
553
|
|
|
$this->processUnknownArgument('-'.$arg, $pos); |
554
|
|
|
} |
555
|
|
|
}//end switch |
556
|
|
|
|
557
|
|
|
}//end processShortArgument() |
558
|
|
|
|
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Processes a long (--example) command line argument. |
562
|
|
|
* |
563
|
|
|
* @param string $arg The command line argument. |
564
|
|
|
* @param int $pos The position of the argument on the command line. |
565
|
|
|
* |
566
|
|
|
* @return void |
567
|
|
|
*/ |
568
|
|
|
public function processLongArgument($arg, $pos) |
569
|
|
|
{ |
570
|
|
|
switch ($arg) { |
571
|
|
|
case 'help': |
572
|
|
|
$this->printUsage(); |
573
|
|
|
exit(0); |
|
|
|
|
574
|
|
|
case 'version': |
575
|
|
|
echo 'Symplify\PHP7_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') '; |
576
|
|
|
echo 'by Squiz (http://www.squiz.net)'.PHP_EOL; |
577
|
|
|
exit(0); |
|
|
|
|
578
|
|
|
case 'cache': |
579
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
580
|
|
|
$this->cache = true; |
|
|
|
|
581
|
|
|
$this->overriddenDefaults['cache'] = true; |
582
|
|
|
} |
583
|
|
|
break; |
584
|
|
|
case 'no-cache': |
585
|
|
|
$this->cache = false; |
|
|
|
|
586
|
|
|
$this->overriddenDefaults['cache'] = true; |
587
|
|
|
break; |
588
|
|
|
default: |
589
|
|
|
if (substr($arg, 0, 7) === 'sniffs=') { |
590
|
|
|
$sniffs = explode(',', substr($arg, 7)); |
591
|
|
|
foreach ($sniffs as $sniff) { |
592
|
|
|
if (substr_count($sniff, '.') !== 2) { |
593
|
|
|
echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL; |
594
|
|
|
$this->printUsage(); |
595
|
|
|
exit(2); |
|
|
|
|
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
$this->sniffs = $sniffs; |
|
|
|
|
600
|
|
|
$this->overriddenDefaults['sniffs'] = true; |
601
|
|
|
} else if (substr($arg, 0, 10) === 'bootstrap=') { |
602
|
|
|
$files = explode(',', substr($arg, 10)); |
603
|
|
|
$bootstrap = array(); |
604
|
|
|
foreach ($files as $file) { |
605
|
|
|
$path = Util\Common::realpath($file); |
606
|
|
|
if ($path === false) { |
607
|
|
|
echo 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL; |
608
|
|
|
$this->printUsage(); |
609
|
|
|
exit(2); |
|
|
|
|
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
$bootstrap[] = $path; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
$this->bootstrap = array_merge($this->bootstrap, $bootstrap); |
|
|
|
|
616
|
|
|
$this->overriddenDefaults['bootstrap'] = true; |
617
|
|
|
} else if (substr($arg, 0, 11) === 'stdin-path=') { |
618
|
|
|
$this->stdinPath = Util\Common::realpath(substr($arg, 11)); |
|
|
|
|
619
|
|
|
|
620
|
|
|
// It may not exist and return false instead, so use whatever they gave us. |
621
|
|
|
if ($this->stdinPath === false) { |
|
|
|
|
622
|
|
|
$this->stdinPath = trim(substr($arg, 11)); |
|
|
|
|
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
$this->overriddenDefaults['stdinPath'] = true; |
626
|
|
|
} else if (substr($arg, 0, 9) === 'basepath=') { |
627
|
|
|
if (isset($this->overriddenDefaults['basepath']) === true) { |
628
|
|
|
break; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
$this->basepath = Util\Common::realpath(substr($arg, 9)); |
|
|
|
|
632
|
|
|
|
633
|
|
|
// It may not exist and return false instead. |
634
|
|
|
if ($this->basepath === false) { |
|
|
|
|
635
|
|
|
$this->basepath = substr($arg, 9); |
|
|
|
|
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
$this->overriddenDefaults['basepath'] = true; |
639
|
|
|
|
640
|
|
|
if (is_dir($this->basepath) === false) { |
|
|
|
|
641
|
|
|
echo 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
642
|
|
|
$this->printUsage(); |
643
|
|
|
exit(2); |
|
|
|
|
644
|
|
|
} |
645
|
|
|
} else if (substr($arg, 0, 7) === 'filter=') { |
646
|
|
|
if (isset($this->overriddenDefaults['filter']) === true) { |
647
|
|
|
break; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
$this->filter = substr($arg, 7); |
|
|
|
|
651
|
|
|
$this->overriddenDefaults['filter'] = true; |
652
|
|
|
} else if (substr($arg, 0, 9) === 'standard=') { |
653
|
|
|
$standards = trim(substr($arg, 9)); |
654
|
|
|
if ($standards !== '') { |
655
|
|
|
$this->standards = explode(',', $standards); |
|
|
|
|
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
$this->overriddenDefaults['standards'] = true; |
659
|
|
|
} else if (substr($arg, 0, 11) === 'extensions=') { |
660
|
|
|
$extensions = explode(',', substr($arg, 11)); |
661
|
|
|
$newExtensions = array(); |
662
|
|
|
foreach ($extensions as $ext) { |
663
|
|
|
$slash = strpos($ext, '/'); |
664
|
|
|
if ($slash !== false) { |
665
|
|
|
// They specified the tokenizer too. |
666
|
|
|
list($ext, $tokenizer) = explode('/', $ext); |
667
|
|
|
$newExtensions[$ext] = strtoupper($tokenizer); |
668
|
|
|
continue; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
if (isset($this->extensions[$ext]) === true) { |
|
|
|
|
672
|
|
|
$newExtensions[$ext] = $this->extensions[$ext]; |
|
|
|
|
673
|
|
|
} else { |
674
|
|
|
$newExtensions[$ext] = 'PHP'; |
675
|
|
|
} |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
$this->extensions = $newExtensions; |
|
|
|
|
679
|
|
|
$this->overriddenDefaults['extensions'] = true; |
680
|
|
|
} else if (substr($arg, 0, 7) === 'suffix=') { |
681
|
|
|
$this->suffix = explode(',', substr($arg, 7)); |
|
|
|
|
682
|
|
|
$this->overriddenDefaults['suffix'] = true; |
683
|
|
|
} else if (substr($arg, 0, 9) === 'parallel=') { |
684
|
|
|
$this->parallel = max((int) substr($arg, 9), 1); |
|
|
|
|
685
|
|
|
$this->overriddenDefaults['parallel'] = true; |
686
|
|
|
} else if (substr($arg, 0, 9) === 'severity=') { |
687
|
|
|
$this->errorSeverity = (int) substr($arg, 9); |
|
|
|
|
688
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
689
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
690
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
691
|
|
|
} else if (substr($arg, 0, 15) === 'error-severity=') { |
692
|
|
|
$this->errorSeverity = (int) substr($arg, 15); |
|
|
|
|
693
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
694
|
|
|
} else if (substr($arg, 0, 17) === 'warning-severity=') { |
695
|
|
|
$this->warningSeverity = (int) substr($arg, 17); |
|
|
|
|
696
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
697
|
|
|
} else if (substr($arg, 0, 7) === 'ignore=') { |
698
|
|
|
// Split the ignore string on commas, unless the comma is escaped |
699
|
|
|
// using 1 or 3 slashes (\, or \\\,). |
|
|
|
|
700
|
|
|
$patterns = preg_split( |
701
|
|
|
'/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', |
702
|
|
|
substr($arg, 7) |
703
|
|
|
); |
704
|
|
|
|
705
|
|
|
$ignored = array(); |
706
|
|
|
foreach ($patterns as $pattern) { |
707
|
|
|
$pattern = trim($pattern); |
708
|
|
|
if ($pattern === '') { |
709
|
|
|
continue; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
$ignored[$pattern] = 'absolute'; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
$this->ignored = $ignored; |
|
|
|
|
716
|
|
|
$this->overriddenDefaults['ignored'] = true; |
717
|
|
|
} else if (substr($arg, 0, 10) === 'generator=' |
718
|
|
|
&& PHP_CodeSniffer_CBF === false |
719
|
|
|
) { |
720
|
|
|
$this->generator = substr($arg, 10); |
|
|
|
|
721
|
|
|
$this->overriddenDefaults['generator'] = true; |
722
|
|
|
} else if (substr($arg, 0, 10) === 'tab-width=') { |
723
|
|
|
$this->tabWidth = (int) substr($arg, 10); |
|
|
|
|
724
|
|
|
$this->overriddenDefaults['tabWidth'] = true; |
725
|
|
|
} else { |
726
|
|
|
if ($this->dieOnUnknownArg === false) { |
727
|
|
|
$eqPos = strpos($arg, '='); |
728
|
|
|
if ($eqPos === false) { |
729
|
|
|
$this->values[$arg] = $arg; |
730
|
|
|
} else { |
731
|
|
|
$value = substr($arg, ($eqPos + 1)); |
732
|
|
|
$arg = substr($arg, 0, $eqPos); |
733
|
|
|
$this->values[$arg] = $value; |
734
|
|
|
} |
735
|
|
|
} else { |
736
|
|
|
$this->processUnknownArgument('--'.$arg, $pos); |
737
|
|
|
} |
738
|
|
|
}//end if |
739
|
|
|
|
740
|
|
|
break; |
741
|
|
|
}//end switch |
742
|
|
|
|
743
|
|
|
}//end processLongArgument() |
744
|
|
|
|
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* Processes an unknown command line argument. |
748
|
|
|
* |
749
|
|
|
* Assumes all unknown arguments are files and folders to check. |
750
|
|
|
* |
751
|
|
|
* @param string $arg The command line argument. |
752
|
|
|
* @param int $pos The position of the argument on the command line. |
753
|
|
|
* |
754
|
|
|
* @return void |
755
|
|
|
*/ |
756
|
|
|
public function processUnknownArgument($arg, $pos) |
|
|
|
|
757
|
|
|
{ |
758
|
|
|
// If we are processing STDIN, don't record any files to check. |
759
|
|
|
if ($this->stdin === true) { |
|
|
|
|
760
|
|
|
return; |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
// We don't know about any additional switches; just files. |
764
|
|
|
if ($arg{0} === '-') { |
765
|
|
|
if ($this->dieOnUnknownArg === false) { |
766
|
|
|
return; |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
echo "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL; |
770
|
|
|
$this->printUsage(); |
771
|
|
|
exit(2); |
|
|
|
|
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
$file = Util\Common::realpath($arg); |
775
|
|
|
if (file_exists($file) === false) { |
776
|
|
|
if ($this->dieOnUnknownArg === false) { |
777
|
|
|
return; |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
echo 'ERROR: The file "'.$arg.'" does not exist.'.PHP_EOL.PHP_EOL; |
781
|
|
|
$this->printUsage(); |
782
|
|
|
exit(2); |
|
|
|
|
783
|
|
|
} else { |
784
|
|
|
$files = $this->files; |
|
|
|
|
785
|
|
|
$files[] = $file; |
786
|
|
|
$this->files = $files; |
|
|
|
|
787
|
|
|
$this->overriddenDefaults['files'] = true; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
}//end processUnknownArgument() |
791
|
|
|
|
792
|
|
|
|
793
|
|
|
/** |
794
|
|
|
* Prints out the usage information for this script. |
795
|
|
|
* |
796
|
|
|
* @return void |
797
|
|
|
*/ |
798
|
|
|
public function printUsage() |
799
|
|
|
{ |
800
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
801
|
|
|
$this->printPHPCBFUsage(); |
802
|
|
|
} else { |
803
|
|
|
$this->printPHPCSUsage(); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
}//end printUsage() |
807
|
|
|
|
808
|
|
|
|
809
|
|
|
/** |
810
|
|
|
* Prints out the usage information for PHPCS. |
811
|
|
|
* |
812
|
|
|
* @return void |
813
|
|
|
*/ |
814
|
|
|
public function printPHPCSUsage() |
815
|
|
|
{ |
816
|
|
|
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]]'.PHP_EOL; |
817
|
|
|
echo ' [--basepath=<basepath>] [--tab-width=<tabWidth>]'.PHP_EOL; |
818
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
819
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--parallel=<processes>]'.PHP_EOL; |
820
|
|
|
echo ' [--extensions=<extensions>] [--generator=<generator>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
821
|
|
|
echo ' - Check STDIN instead of local files and directories'.PHP_EOL; |
822
|
|
|
echo ' -n Do not print warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
823
|
|
|
echo ' -w Print both warnings and errors (this is the default)'.PHP_EOL; |
824
|
|
|
echo ' -l Local directory only, no recursion'.PHP_EOL; |
825
|
|
|
echo ' -s Show sniff codes in all reports'.PHP_EOL; |
826
|
|
|
echo ' -a Run interactively'.PHP_EOL; |
827
|
|
|
echo ' -e Explain a standard by showing the sniffs it includes'.PHP_EOL; |
828
|
|
|
echo ' -p Show progress of the run'.PHP_EOL; |
829
|
|
|
echo ' -m Stop error messages from being recorded'.PHP_EOL; |
830
|
|
|
echo ' (saves a lot of memory, but stops many reports from being used)'.PHP_EOL; |
831
|
|
|
echo ' -v[v][v] Print verbose output'.PHP_EOL; |
832
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
833
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
834
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
835
|
|
|
echo ' --version Print version information'.PHP_EOL; |
836
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
837
|
|
|
echo ' <file> One or more files and/or directories to check'.PHP_EOL; |
838
|
|
|
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL; |
839
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
840
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
841
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
842
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
843
|
|
|
echo ' <processes> How many files should be checked simultaneously (default is 1)'.PHP_EOL; |
844
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the check to'.PHP_EOL; |
845
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
846
|
|
|
echo ' <severity> The minimum severity required to display an error or warning'.PHP_EOL; |
847
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
848
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
849
|
|
|
|
850
|
|
|
}//end printPHPCSUsage() |
851
|
|
|
|
852
|
|
|
|
853
|
|
|
/** |
854
|
|
|
* Prints out the usage information for PHPCBF. |
855
|
|
|
* |
856
|
|
|
* @return void |
857
|
|
|
*/ |
858
|
|
|
public function printPHPCBFUsage() |
859
|
|
|
{ |
860
|
|
|
echo 'Usage: phpcbf [-nwli] [-d key[=value]]'.PHP_EOL; |
861
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--suffix=<suffix>]'.PHP_EOL; |
862
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
863
|
|
|
echo ' [--tab-width=<tabWidth>] [--parallel=<processes>]'.PHP_EOL; |
864
|
|
|
echo ' [--basepath=<basepath>] [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
865
|
|
|
echo ' - Fix STDIN instead of local files and directories'.PHP_EOL; |
866
|
|
|
echo ' -n Do not fix warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
867
|
|
|
echo ' -w Fix both warnings and errors (on by default)'.PHP_EOL; |
868
|
|
|
echo ' -l Local directory only, no recursion'.PHP_EOL; |
869
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
870
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
871
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
872
|
|
|
echo ' --version Print version information'.PHP_EOL; |
873
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
874
|
|
|
echo ' <file> One or more files and/or directories to fix'.PHP_EOL; |
875
|
|
|
echo ' <extensions> A comma separated list of file extensions to fix'.PHP_EOL; |
876
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
877
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
878
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
879
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
880
|
|
|
echo ' <processes> How many files should be fixed simultaneously (default is 1)'.PHP_EOL; |
881
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the fixes to'.PHP_EOL; |
882
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
883
|
|
|
echo ' <severity> The minimum severity required to fix an error or warning'.PHP_EOL; |
884
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
885
|
|
|
echo ' <suffix> Write modified files to a filename using this suffix'.PHP_EOL; |
886
|
|
|
echo ' ("diff" and "patch" are not used in this mode)'.PHP_EOL; |
887
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
888
|
|
|
|
889
|
|
|
}//end printPHPCBFUsage() |
890
|
|
|
|
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* Get a single config value. |
894
|
|
|
* |
895
|
|
|
* @param string $key The name of the config value. |
896
|
|
|
* |
897
|
|
|
* @return string|null |
898
|
|
|
* @see setConfigData() |
899
|
|
|
* @see getAllConfigData() |
900
|
|
|
*/ |
901
|
12 |
|
public static function getConfigData($key) |
902
|
|
|
{ |
903
|
12 |
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
904
|
|
|
|
905
|
12 |
|
if ($phpCodeSnifferConfig === null) { |
906
|
|
|
return null; |
907
|
|
|
} |
908
|
|
|
|
909
|
12 |
|
if (isset($phpCodeSnifferConfig[$key]) === false) { |
910
|
12 |
|
return null; |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
return $phpCodeSnifferConfig[$key]; |
914
|
|
|
|
915
|
|
|
}//end getConfigData() |
916
|
|
|
|
917
|
|
|
|
918
|
|
|
/** |
919
|
|
|
* Get the path to an executable utility. |
920
|
|
|
* |
921
|
|
|
* @param string $name The name of the executable utility. |
922
|
|
|
* |
923
|
|
|
* @return string|null |
924
|
|
|
* @see getConfigData() |
925
|
|
|
*/ |
926
|
|
|
public static function getExecutablePath($name) |
927
|
|
|
{ |
928
|
|
|
$data = self::getConfigData($name.'_path'); |
929
|
|
|
if ($data !== null) { |
930
|
|
|
return $data; |
931
|
|
|
} |
932
|
|
|
|
933
|
|
|
if (array_key_exists($name, self::$executablePaths) === true) { |
934
|
|
|
return self::$executablePaths[$name]; |
935
|
|
|
} |
936
|
|
|
|
937
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
938
|
|
|
$cmd = 'where '.escapeshellarg($name).' 2> nul'; |
939
|
|
|
} else { |
940
|
|
|
$cmd = 'which '.escapeshellarg($name); |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
$result = exec($cmd, $output, $retVal); |
944
|
|
|
if ($retVal !== 0) { |
945
|
|
|
$result = null; |
946
|
|
|
} |
947
|
|
|
|
948
|
|
|
self::$executablePaths[$name] = $result; |
949
|
|
|
return $result; |
950
|
|
|
|
951
|
|
|
}//end getExecutablePath() |
952
|
|
|
|
953
|
|
|
|
954
|
|
|
/** |
955
|
|
|
* Set a single config value. |
956
|
|
|
* |
957
|
|
|
* @param string $key The name of the config value. |
958
|
|
|
* @param string|null $value The value to set. If null, the config |
959
|
|
|
* entry is deleted, reverting it to the |
960
|
|
|
* default value. |
961
|
|
|
* @param boolean $temp Set this config data temporarily for this |
962
|
|
|
* script run. This will not write the config |
963
|
|
|
* data to the config file. |
964
|
|
|
* |
965
|
|
|
* @return bool |
966
|
|
|
* @see getConfigData() |
967
|
|
|
* @throws RuntimeException If the config file can not be written. |
968
|
|
|
*/ |
969
|
|
|
public static function setConfigData($key, $value, $temp=false) |
970
|
|
|
{ |
971
|
|
|
if ($temp === false) { |
972
|
|
|
$configFile = dirname(__FILE__).'/../CodeSniffer.conf'; |
973
|
|
|
if (is_file($configFile) === false |
974
|
|
|
&& strpos('@data_dir@', '@data_dir') === false |
975
|
|
|
) { |
976
|
|
|
// If data_dir was replaced, this is a PEAR install and we can |
977
|
|
|
// use the PEAR data dir to store the conf file. |
978
|
|
|
$configFile = '@data_dir@/Symplify\PHP7_CodeSniffer/CodeSniffer.conf'; |
979
|
|
|
} |
980
|
|
|
|
981
|
|
|
if (is_file($configFile) === true |
982
|
|
|
&& is_writable($configFile) === false |
983
|
|
|
) { |
984
|
|
|
$error = 'Config file '.$configFile.' is not writable'; |
985
|
|
|
throw new RuntimeException($error); |
986
|
|
|
} |
987
|
|
|
} |
988
|
|
|
|
989
|
|
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
990
|
|
|
|
991
|
|
|
if ($value === null) { |
992
|
|
|
if (isset($phpCodeSnifferConfig[$key]) === true) { |
993
|
|
|
unset($phpCodeSnifferConfig[$key]); |
994
|
|
|
} |
995
|
|
|
} else { |
996
|
|
|
$phpCodeSnifferConfig[$key] = $value; |
997
|
|
|
} |
998
|
|
|
|
999
|
|
|
if ($temp === false) { |
1000
|
|
|
$output = '<'.'?php'."\n".' $phpCodeSnifferConfig = '; |
1001
|
|
|
$output .= var_export($phpCodeSnifferConfig, true); |
1002
|
|
|
$output .= "\n?".'>'; |
1003
|
|
|
|
1004
|
|
|
if (file_put_contents($configFile, $output) === false) { |
|
|
|
|
1005
|
|
|
return false; |
1006
|
|
|
} |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
self::$configData = $phpCodeSnifferConfig; |
1010
|
|
|
|
1011
|
|
|
return true; |
1012
|
|
|
|
1013
|
|
|
}//end setConfigData() |
1014
|
|
|
|
1015
|
|
|
|
1016
|
|
|
/** |
1017
|
|
|
* Get all config data. |
1018
|
|
|
* |
1019
|
|
|
* @return array<string, string> |
|
|
|
|
1020
|
|
|
* @see getConfigData() |
1021
|
|
|
*/ |
1022
|
12 |
|
public static function getAllConfigData() |
1023
|
|
|
{ |
1024
|
12 |
|
if (self::$configData !== null) { |
1025
|
11 |
|
return self::$configData; |
1026
|
|
|
} |
1027
|
|
|
|
1028
|
1 |
|
$configFile = dirname(__FILE__).'/../CodeSniffer.conf'; |
1029
|
1 |
|
if (is_file($configFile) === false) { |
1030
|
|
|
$configFile = '@data_dir@/Symplify\PHP7_CodeSniffer/CodeSniffer.conf'; |
1031
|
|
|
} |
1032
|
|
|
|
1033
|
1 |
|
if (is_file($configFile) === false) { |
1034
|
|
|
self::$configData = array(); |
1035
|
|
|
return array(); |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
1 |
|
include $configFile; |
1039
|
1 |
|
self::$configData = $phpCodeSnifferConfig; |
|
|
|
|
1040
|
1 |
|
return self::$configData; |
1041
|
|
|
|
1042
|
|
|
}//end getAllConfigData() |
1043
|
|
|
|
1044
|
|
|
|
1045
|
|
|
/** |
1046
|
|
|
* Prints out the gathered config data. |
1047
|
|
|
* |
1048
|
|
|
* @param array $data The config data to print. |
1049
|
|
|
* |
1050
|
|
|
* @return void |
1051
|
|
|
*/ |
1052
|
|
|
public function printConfigData($data) |
1053
|
|
|
{ |
1054
|
|
|
$max = 0; |
1055
|
|
|
$keys = array_keys($data); |
1056
|
|
|
foreach ($keys as $key) { |
1057
|
|
|
$len = strlen($key); |
1058
|
|
|
if (strlen($key) > $max) { |
1059
|
|
|
$max = $len; |
1060
|
|
|
} |
1061
|
|
|
} |
1062
|
|
|
|
1063
|
|
|
if ($max === 0) { |
1064
|
|
|
return; |
1065
|
|
|
} |
1066
|
|
|
|
1067
|
|
|
$max += 2; |
1068
|
|
|
ksort($data); |
1069
|
|
|
foreach ($data as $name => $value) { |
1070
|
|
|
echo str_pad($name.': ', $max).$value.PHP_EOL; |
1071
|
|
|
} |
1072
|
|
|
|
1073
|
|
|
}//end printConfigData() |
1074
|
|
|
|
1075
|
|
|
|
1076
|
|
|
}//end class |
1077
|
|
|
|
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.