|
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
|
12 |
|
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
|
12 |
|
foreach ($value as $standard) { |
|
201
|
12 |
|
foreach ($installedStandards as $validStandard) { |
|
202
|
|
|
if (strtolower($standard) === strtolower($validStandard)) { |
|
203
|
|
|
$standard = $validStandard; |
|
204
|
|
|
break; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
12 |
|
$cleaned[] = $standard; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
12 |
|
$value = $cleaned; |
|
212
|
12 |
|
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
|
12 |
|
$this->verbosity = 0; |
|
|
|
|
|
|
388
|
12 |
|
$this->interactive = false; |
|
|
|
|
|
|
389
|
12 |
|
$this->colors = true; |
|
|
|
|
|
|
390
|
|
|
$this->explain = false; |
|
|
|
|
|
|
391
|
|
|
$this->local = false; |
|
|
|
|
|
|
392
|
|
|
$this->showSources = false; |
|
|
|
|
|
|
393
|
|
|
$this->showProgress = false; |
|
|
|
|
|
|
394
|
|
|
$this->tabWidth = 0; |
|
|
|
|
|
|
395
|
|
|
$this->extensions = array( |
|
|
|
|
|
|
396
|
|
|
'php' => 'PHP', |
|
397
|
|
|
'inc' => 'PHP', |
|
398
|
|
|
'js' => 'JS', |
|
399
|
|
|
'css' => 'CSS', |
|
400
|
|
|
); |
|
401
|
|
|
$this->sniffs = array(); |
|
|
|
|
|
|
402
|
|
|
$this->ignored = array(); |
|
|
|
|
|
|
403
|
|
|
$this->filter = null; |
|
|
|
|
|
|
404
|
|
|
$this->bootstrap = array(); |
|
|
|
|
|
|
405
|
|
|
$this->reports = array('full' => null); |
|
|
|
|
|
|
406
|
|
|
$this->errorSeverity = 5; |
|
|
|
|
|
|
407
|
|
|
$this->warningSeverity = 5; |
|
|
|
|
|
|
408
|
|
|
$this->recordErrors = true; |
|
|
|
|
|
|
409
|
|
|
$this->suffix = ''; |
|
|
|
|
|
|
410
|
|
|
$this->stdin = false; |
|
|
|
|
|
|
411
|
|
|
$this->stdinContent = null; |
|
|
|
|
|
|
412
|
|
|
$this->stdinPath = null; |
|
|
|
|
|
|
413
|
|
|
|
|
414
|
|
|
$standard = self::getConfigData('default_standard'); |
|
415
|
|
|
if ($standard !== null) { |
|
416
|
|
|
$this->standards = explode(',', $standard); |
|
|
|
|
|
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
$tabWidth = self::getConfigData('tab_width'); |
|
420
|
|
|
if ($tabWidth !== null) { |
|
421
|
|
|
$this->tabWidth = (int) $tabWidth; |
|
|
|
|
|
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
$severity = self::getConfigData('severity'); |
|
425
|
|
|
if ($severity !== null) { |
|
426
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
|
|
427
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
|
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
$severity = self::getConfigData('error_severity'); |
|
431
|
|
|
if ($severity !== null) { |
|
432
|
|
|
$this->errorSeverity = (int) $severity; |
|
|
|
|
|
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
$severity = self::getConfigData('warning_severity'); |
|
436
|
|
|
if ($severity !== null) { |
|
437
|
|
|
$this->warningSeverity = (int) $severity; |
|
|
|
|
|
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
$showWarnings = self::getConfigData('show_warnings'); |
|
441
|
|
|
if ($showWarnings !== null) { |
|
442
|
|
|
$showWarnings = (bool) $showWarnings; |
|
443
|
|
|
if ($showWarnings === false) { |
|
444
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
|
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
$showProgress = self::getConfigData('show_progress'); |
|
449
|
|
|
if ($showProgress !== null) { |
|
450
|
|
|
$this->showProgress = (bool) $showProgress; |
|
|
|
|
|
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
|
454
|
|
|
$cache = self::getConfigData('cache'); |
|
455
|
|
|
if ($cache !== null) { |
|
456
|
|
|
$this->cache = (bool) $cache; |
|
|
|
|
|
|
457
|
|
|
} |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
}//end restoreDefaults() |
|
461
|
|
|
|
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Processes a short (-e) command line argument. |
|
465
|
|
|
* |
|
466
|
|
|
* @param string $arg The command line argument. |
|
467
|
|
|
* @param int $pos The position of the argument on the command line. |
|
468
|
|
|
* |
|
469
|
|
|
* @return void |
|
470
|
|
|
*/ |
|
471
|
|
|
public function processShortArgument($arg, $pos) |
|
472
|
|
|
{ |
|
473
|
|
|
switch ($arg) { |
|
474
|
|
|
case 'h': |
|
475
|
|
|
case '?': |
|
476
|
|
|
$this->printUsage(); |
|
477
|
|
|
exit(0); |
|
|
|
|
|
|
478
|
|
|
case 'i' : |
|
|
|
|
|
|
479
|
|
|
Util\Standards::printInstalledStandards(); |
|
480
|
|
|
exit(0); |
|
|
|
|
|
|
481
|
|
|
case 'v' : |
|
|
|
|
|
|
482
|
|
|
$this->verbosity++; |
|
|
|
|
|
|
483
|
|
|
$this->overriddenDefaults['verbosity'] = true; |
|
484
|
|
|
break; |
|
485
|
|
|
case 'l' : |
|
|
|
|
|
|
486
|
|
|
$this->local = true; |
|
|
|
|
|
|
487
|
|
|
$this->overriddenDefaults['local'] = true; |
|
488
|
|
|
break; |
|
489
|
|
|
case 's' : |
|
|
|
|
|
|
490
|
|
|
$this->showSources = true; |
|
|
|
|
|
|
491
|
|
|
$this->overriddenDefaults['showSources'] = true; |
|
492
|
|
|
break; |
|
493
|
|
|
case 'a' : |
|
|
|
|
|
|
494
|
|
|
$this->interactive = true; |
|
|
|
|
|
|
495
|
|
|
$this->overriddenDefaults['interactive'] = true; |
|
496
|
|
|
break; |
|
497
|
|
|
case 'e': |
|
498
|
|
|
$this->explain = true; |
|
|
|
|
|
|
499
|
|
|
$this->overriddenDefaults['explain'] = true; |
|
500
|
|
|
break; |
|
501
|
|
|
case 'p' : |
|
|
|
|
|
|
502
|
|
|
$this->showProgress = true; |
|
|
|
|
|
|
503
|
|
|
$this->overriddenDefaults['showProgress'] = true; |
|
504
|
|
|
break; |
|
505
|
|
|
case 'm' : |
|
|
|
|
|
|
506
|
|
|
$this->recordErrors = false; |
|
|
|
|
|
|
507
|
|
|
$this->overriddenDefaults['recordErrors'] = true; |
|
508
|
|
|
break; |
|
509
|
|
|
case 'd' : |
|
|
|
|
|
|
510
|
|
|
$ini = explode('=', $this->cliArgs[($pos + 1)]); |
|
511
|
|
|
$this->cliArgs[($pos + 1)] = ''; |
|
512
|
|
|
if (isset($ini[1]) === true) { |
|
513
|
|
|
ini_set($ini[0], $ini[1]); |
|
514
|
|
|
} else { |
|
515
|
|
|
ini_set($ini[0], true); |
|
516
|
|
|
} |
|
517
|
|
|
break; |
|
518
|
|
View Code Duplication |
case 'n' : |
|
|
|
|
|
|
519
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
|
520
|
|
|
$this->warningSeverity = 0; |
|
|
|
|
|
|
521
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
|
522
|
|
|
} |
|
523
|
|
|
break; |
|
524
|
|
View Code Duplication |
case 'w' : |
|
|
|
|
|
|
525
|
|
|
if (isset($this->overriddenDefaults['warningSeverity']) === false) { |
|
526
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
|
|
527
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
|
528
|
|
|
} |
|
529
|
|
|
break; |
|
530
|
|
|
default: |
|
531
|
|
|
if ($this->dieOnUnknownArg === false) { |
|
532
|
|
|
$this->values[$arg] = $arg; |
|
533
|
|
|
} else { |
|
534
|
|
|
$this->processUnknownArgument('-'.$arg, $pos); |
|
535
|
|
|
} |
|
536
|
|
|
}//end switch |
|
537
|
|
|
|
|
538
|
|
|
}//end processShortArgument() |
|
539
|
|
|
|
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Processes a long (--example) command line argument. |
|
543
|
|
|
* |
|
544
|
|
|
* @param string $arg The command line argument. |
|
545
|
|
|
* @param int $pos The position of the argument on the command line. |
|
546
|
|
|
* |
|
547
|
|
|
* @return void |
|
548
|
|
|
*/ |
|
549
|
|
|
public function processLongArgument($arg, $pos) |
|
550
|
|
|
{ |
|
551
|
|
|
switch ($arg) { |
|
552
|
|
|
case 'help': |
|
553
|
|
|
$this->printUsage(); |
|
554
|
|
|
exit(0); |
|
|
|
|
|
|
555
|
|
|
case 'version': |
|
556
|
|
|
echo 'Symplify\PHP7_CodeSniffer version '.self::VERSION; |
|
557
|
|
|
exit(0); |
|
|
|
|
|
|
558
|
|
|
case 'cache': |
|
559
|
|
|
if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
|
560
|
|
|
$this->cache = true; |
|
|
|
|
|
|
561
|
|
|
$this->overriddenDefaults['cache'] = true; |
|
562
|
|
|
} |
|
563
|
|
|
break; |
|
564
|
|
|
case 'no-cache': |
|
565
|
|
|
$this->cache = false; |
|
|
|
|
|
|
566
|
|
|
$this->overriddenDefaults['cache'] = true; |
|
567
|
|
|
break; |
|
568
|
|
|
default: |
|
569
|
|
|
if (substr($arg, 0, 7) === 'sniffs=') { |
|
570
|
|
|
$sniffs = explode(',', substr($arg, 7)); |
|
571
|
|
|
foreach ($sniffs as $sniff) { |
|
572
|
|
|
if (substr_count($sniff, '.') !== 2) { |
|
573
|
|
|
echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL; |
|
574
|
|
|
$this->printUsage(); |
|
575
|
|
|
exit(2); |
|
|
|
|
|
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
$this->sniffs = $sniffs; |
|
|
|
|
|
|
580
|
|
|
$this->overriddenDefaults['sniffs'] = true; |
|
581
|
|
|
} else if (substr($arg, 0, 10) === 'bootstrap=') { |
|
582
|
|
|
$files = explode(',', substr($arg, 10)); |
|
583
|
|
|
$bootstrap = array(); |
|
584
|
|
|
foreach ($files as $file) { |
|
585
|
|
|
$path = Util\Common::realpath($file); |
|
586
|
|
|
if ($path === false) { |
|
587
|
|
|
echo 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL; |
|
588
|
|
|
$this->printUsage(); |
|
589
|
|
|
exit(2); |
|
|
|
|
|
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
$bootstrap[] = $path; |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
$this->bootstrap = array_merge($this->bootstrap, $bootstrap); |
|
|
|
|
|
|
596
|
|
|
$this->overriddenDefaults['bootstrap'] = true; |
|
597
|
|
|
} else if (substr($arg, 0, 11) === 'stdin-path=') { |
|
598
|
|
|
$this->stdinPath = Util\Common::realpath(substr($arg, 11)); |
|
|
|
|
|
|
599
|
|
|
|
|
600
|
|
|
// It may not exist and return false instead, so use whatever they gave us. |
|
601
|
|
|
if ($this->stdinPath === false) { |
|
|
|
|
|
|
602
|
|
|
$this->stdinPath = trim(substr($arg, 11)); |
|
|
|
|
|
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
$this->overriddenDefaults['stdinPath'] = true; |
|
606
|
|
|
} else if (substr($arg, 0, 9) === 'basepath=') { |
|
607
|
|
|
if (isset($this->overriddenDefaults['basepath']) === true) { |
|
608
|
|
|
break; |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
$this->basepath = Util\Common::realpath(substr($arg, 9)); |
|
|
|
|
|
|
612
|
|
|
|
|
613
|
|
|
// It may not exist and return false instead. |
|
614
|
|
|
if ($this->basepath === false) { |
|
|
|
|
|
|
615
|
|
|
$this->basepath = substr($arg, 9); |
|
|
|
|
|
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
$this->overriddenDefaults['basepath'] = true; |
|
619
|
|
|
|
|
620
|
|
|
if (is_dir($this->basepath) === false) { |
|
|
|
|
|
|
621
|
|
|
echo 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
|
|
|
|
|
|
622
|
|
|
$this->printUsage(); |
|
623
|
|
|
exit(2); |
|
|
|
|
|
|
624
|
|
|
} |
|
625
|
|
|
} else if (substr($arg, 0, 7) === 'filter=') { |
|
626
|
|
|
if (isset($this->overriddenDefaults['filter']) === true) { |
|
627
|
|
|
break; |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
$this->filter = substr($arg, 7); |
|
|
|
|
|
|
631
|
|
|
$this->overriddenDefaults['filter'] = true; |
|
632
|
|
|
} else if (substr($arg, 0, 9) === 'standard=') { |
|
633
|
|
|
$standards = trim(substr($arg, 9)); |
|
634
|
|
|
if ($standards !== '') { |
|
635
|
|
|
$this->standards = explode(',', $standards); |
|
|
|
|
|
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
|
|
$this->overriddenDefaults['standards'] = true; |
|
639
|
|
|
} else if (substr($arg, 0, 11) === 'extensions=') { |
|
640
|
|
|
$extensions = explode(',', substr($arg, 11)); |
|
641
|
|
|
$newExtensions = array(); |
|
642
|
|
|
foreach ($extensions as $ext) { |
|
643
|
|
|
$slash = strpos($ext, '/'); |
|
644
|
|
|
if ($slash !== false) { |
|
645
|
|
|
// They specified the tokenizer too. |
|
646
|
|
|
list($ext, $tokenizer) = explode('/', $ext); |
|
647
|
|
|
$newExtensions[$ext] = strtoupper($tokenizer); |
|
648
|
|
|
continue; |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
if (isset($this->extensions[$ext]) === true) { |
|
|
|
|
|
|
652
|
|
|
$newExtensions[$ext] = $this->extensions[$ext]; |
|
|
|
|
|
|
653
|
|
|
} else { |
|
654
|
|
|
$newExtensions[$ext] = 'PHP'; |
|
655
|
|
|
} |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
$this->extensions = $newExtensions; |
|
|
|
|
|
|
659
|
|
|
$this->overriddenDefaults['extensions'] = true; |
|
660
|
|
|
} else if (substr($arg, 0, 7) === 'suffix=') { |
|
661
|
|
|
$this->suffix = explode(',', substr($arg, 7)); |
|
|
|
|
|
|
662
|
|
|
$this->overriddenDefaults['suffix'] = true; |
|
663
|
|
|
} else if (substr($arg, 0, 9) === 'severity=') { |
|
664
|
|
|
$this->errorSeverity = (int) substr($arg, 9); |
|
|
|
|
|
|
665
|
|
|
$this->warningSeverity = $this->errorSeverity; |
|
|
|
|
|
|
666
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
|
667
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
|
668
|
|
|
} else if (substr($arg, 0, 15) === 'error-severity=') { |
|
669
|
|
|
$this->errorSeverity = (int) substr($arg, 15); |
|
|
|
|
|
|
670
|
|
|
$this->overriddenDefaults['errorSeverity'] = true; |
|
671
|
|
|
} else if (substr($arg, 0, 17) === 'warning-severity=') { |
|
672
|
|
|
$this->warningSeverity = (int) substr($arg, 17); |
|
|
|
|
|
|
673
|
|
|
$this->overriddenDefaults['warningSeverity'] = true; |
|
674
|
|
|
} else if (substr($arg, 0, 7) === 'ignore=') { |
|
675
|
|
|
// Split the ignore string on commas, unless the comma is escaped |
|
676
|
|
|
// using 1 or 3 slashes (\, or \\\,). |
|
|
|
|
|
|
677
|
|
|
$patterns = preg_split( |
|
678
|
|
|
'/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', |
|
679
|
|
|
substr($arg, 7) |
|
680
|
|
|
); |
|
681
|
|
|
|
|
682
|
|
|
$ignored = array(); |
|
683
|
|
|
foreach ($patterns as $pattern) { |
|
684
|
|
|
$pattern = trim($pattern); |
|
685
|
|
|
if ($pattern === '') { |
|
686
|
|
|
continue; |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
$ignored[$pattern] = 'absolute'; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
$this->ignored = $ignored; |
|
|
|
|
|
|
693
|
|
|
$this->overriddenDefaults['ignored'] = true; |
|
694
|
|
|
} else if (substr($arg, 0, 10) === 'tab-width=') { |
|
695
|
|
|
$this->tabWidth = (int) substr($arg, 10); |
|
|
|
|
|
|
696
|
|
|
$this->overriddenDefaults['tabWidth'] = true; |
|
697
|
|
|
} else { |
|
698
|
|
|
if ($this->dieOnUnknownArg === false) { |
|
699
|
|
|
$eqPos = strpos($arg, '='); |
|
700
|
|
|
if ($eqPos === false) { |
|
701
|
|
|
$this->values[$arg] = $arg; |
|
702
|
|
|
} else { |
|
703
|
|
|
$value = substr($arg, ($eqPos + 1)); |
|
704
|
|
|
$arg = substr($arg, 0, $eqPos); |
|
705
|
|
|
$this->values[$arg] = $value; |
|
706
|
|
|
} |
|
707
|
|
|
} else { |
|
708
|
|
|
$this->processUnknownArgument('--'.$arg, $pos); |
|
709
|
|
|
} |
|
710
|
|
|
}//end if |
|
711
|
|
|
|
|
712
|
|
|
break; |
|
713
|
|
|
}//end switch |
|
714
|
|
|
|
|
715
|
|
|
}//end processLongArgument() |
|
716
|
|
|
|
|
717
|
|
|
|
|
718
|
|
|
/** |
|
719
|
|
|
* Processes an unknown command line argument. |
|
720
|
|
|
* |
|
721
|
|
|
* Assumes all unknown arguments are files and folders to check. |
|
722
|
|
|
* |
|
723
|
|
|
* @param string $arg The command line argument. |
|
724
|
|
|
* @param int $pos The position of the argument on the command line. |
|
725
|
|
|
* |
|
726
|
|
|
* @return void |
|
727
|
|
|
*/ |
|
728
|
|
|
public function processUnknownArgument($arg, $pos) |
|
|
|
|
|
|
729
|
|
|
{ |
|
730
|
|
|
// If we are processing STDIN, don't record any files to check. |
|
731
|
|
|
if ($this->stdin === true) { |
|
|
|
|
|
|
732
|
|
|
return; |
|
733
|
|
|
} |
|
734
|
|
|
|
|
735
|
|
|
// We don't know about any additional switches; just files. |
|
736
|
|
|
if ($arg{0} === '-') { |
|
737
|
|
|
if ($this->dieOnUnknownArg === false) { |
|
738
|
|
|
return; |
|
739
|
|
|
} |
|
740
|
|
|
|
|
741
|
|
|
echo "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL; |
|
742
|
|
|
$this->printUsage(); |
|
743
|
|
|
exit(2); |
|
|
|
|
|
|
744
|
|
|
} |
|
745
|
|
|
|
|
746
|
|
|
$file = Util\Common::realpath($arg); |
|
747
|
|
|
if (file_exists($file) === false) { |
|
748
|
|
|
if ($this->dieOnUnknownArg === false) { |
|
749
|
|
|
return; |
|
750
|
|
|
} |
|
751
|
|
|
|
|
752
|
|
|
echo 'ERROR: The file "'.$arg.'" does not exist.'.PHP_EOL.PHP_EOL; |
|
753
|
|
|
$this->printUsage(); |
|
754
|
|
|
exit(2); |
|
|
|
|
|
|
755
|
|
|
} else { |
|
756
|
|
|
$files = $this->files; |
|
|
|
|
|
|
757
|
|
|
$files[] = $file; |
|
758
|
|
|
$this->files = $files; |
|
|
|
|
|
|
759
|
|
|
$this->overriddenDefaults['files'] = true; |
|
760
|
|
|
} |
|
761
|
|
|
|
|
762
|
|
|
}//end processUnknownArgument() |
|
763
|
|
|
|
|
764
|
|
|
|
|
765
|
|
|
/** |
|
766
|
|
|
* Prints out the usage information for this script. |
|
767
|
|
|
* |
|
768
|
|
|
* @return void |
|
769
|
|
|
*/ |
|
770
|
|
|
public function printUsage() |
|
771
|
|
|
{ |
|
772
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
|
773
|
|
|
$this->printPHPCBFUsage(); |
|
774
|
|
|
} else { |
|
775
|
|
|
$this->printPHPCSUsage(); |
|
776
|
|
|
} |
|
777
|
|
|
|
|
778
|
|
|
}//end printUsage() |
|
779
|
|
|
|
|
780
|
|
|
|
|
781
|
|
|
/** |
|
782
|
|
|
* Prints out the usage information for PHPCS. |
|
783
|
|
|
* |
|
784
|
|
|
* @return void |
|
785
|
|
|
*/ |
|
786
|
|
|
public function printPHPCSUsage() |
|
787
|
|
|
{ |
|
788
|
|
|
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]]'.PHP_EOL; |
|
789
|
|
|
echo ' [--basepath=<basepath>] [--tab-width=<tabWidth>]'.PHP_EOL; |
|
790
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
|
791
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>]'.PHP_EOL; |
|
792
|
|
|
echo ' [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
|
793
|
|
|
echo ' - Check STDIN instead of local files and directories'.PHP_EOL; |
|
794
|
|
|
echo ' -n Do not print warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
|
795
|
|
|
echo ' -w Print both warnings and errors (this is the default)'.PHP_EOL; |
|
796
|
|
|
echo ' -s Show sniff codes in all reports'.PHP_EOL; |
|
797
|
|
|
echo ' -a Run interactively'.PHP_EOL; |
|
798
|
|
|
echo ' -e Explain a standard by showing the sniffs it includes'.PHP_EOL; |
|
799
|
|
|
echo ' -p Show progress of the run'.PHP_EOL; |
|
800
|
|
|
echo ' -m Stop error messages from being recorded'.PHP_EOL; |
|
801
|
|
|
echo ' (saves a lot of memory, but stops many reports from being used)'.PHP_EOL; |
|
802
|
|
|
echo ' -v[v][v] Print verbose output'.PHP_EOL; |
|
803
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
|
804
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
|
805
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
|
806
|
|
|
echo ' --version Print version information'.PHP_EOL; |
|
807
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
|
808
|
|
|
echo ' <file> One or more files and/or directories to check'.PHP_EOL; |
|
809
|
|
|
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL; |
|
810
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
|
811
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
|
812
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
|
813
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
|
814
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the check to'.PHP_EOL; |
|
815
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
|
816
|
|
|
echo ' <severity> The minimum severity required to display an error or warning'.PHP_EOL; |
|
817
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
|
818
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
|
819
|
|
|
|
|
820
|
|
|
}//end printPHPCSUsage() |
|
821
|
|
|
|
|
822
|
|
|
|
|
823
|
|
|
/** |
|
824
|
|
|
* Prints out the usage information for PHPCBF. |
|
825
|
|
|
* |
|
826
|
|
|
* @return void |
|
827
|
|
|
*/ |
|
828
|
|
|
public function printPHPCBFUsage() |
|
829
|
|
|
{ |
|
830
|
|
|
echo 'Usage: phpcbf [-nwli] [-d key[=value]]'.PHP_EOL; |
|
831
|
|
|
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--suffix=<suffix>]'.PHP_EOL; |
|
832
|
|
|
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL; |
|
833
|
|
|
echo ' [--tab-width=<tabWidth>]'.PHP_EOL; |
|
834
|
|
|
echo ' [--basepath=<basepath>] [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL; |
|
835
|
|
|
echo ' - Fix STDIN instead of local files and directories'.PHP_EOL; |
|
836
|
|
|
echo ' -n Do not fix warnings (shortcut for --warning-severity=0)'.PHP_EOL; |
|
837
|
|
|
echo ' -w Fix both warnings and errors (on by default)'.PHP_EOL; |
|
838
|
|
|
echo ' -i Show a list of installed coding standards'.PHP_EOL; |
|
839
|
|
|
echo ' -d Set the [key] php.ini value to [value] or [true] if value is omitted'.PHP_EOL; |
|
840
|
|
|
echo ' --help Print this help message'.PHP_EOL; |
|
841
|
|
|
echo ' --version Print version information'.PHP_EOL; |
|
842
|
|
|
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL; |
|
843
|
|
|
echo ' <file> One or more files and/or directories to fix'.PHP_EOL; |
|
844
|
|
|
echo ' <extensions> A comma separated list of file extensions to fix'.PHP_EOL; |
|
845
|
|
|
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL; |
|
846
|
|
|
echo ' The type of the file can be specified using: ext/type'.PHP_EOL; |
|
847
|
|
|
echo ' e.g., module/php,es/js'.PHP_EOL; |
|
848
|
|
|
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL; |
|
849
|
|
|
echo ' <sniffs> A comma separated list of sniff codes to limit the fixes to'.PHP_EOL; |
|
850
|
|
|
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL; |
|
851
|
|
|
echo ' <severity> The minimum severity required to fix an error or warning'.PHP_EOL; |
|
852
|
|
|
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL; |
|
853
|
|
|
echo ' <suffix> Write modified files to a filename using this suffix'.PHP_EOL; |
|
854
|
|
|
echo ' ("diff" and "patch" are not used in this mode)'.PHP_EOL; |
|
855
|
|
|
echo ' <tabWidth> The number of spaces each tab represents'.PHP_EOL; |
|
856
|
|
|
|
|
857
|
|
|
}//end printPHPCBFUsage() |
|
858
|
|
|
|
|
859
|
|
|
|
|
860
|
|
|
/** |
|
861
|
|
|
* Get a single config value. |
|
862
|
|
|
* |
|
863
|
|
|
* @param string $key The name of the config value. |
|
864
|
|
|
* |
|
865
|
|
|
* @return string|null |
|
866
|
|
|
* @see setConfigData() |
|
867
|
|
|
* @see getAllConfigData() |
|
868
|
|
|
*/ |
|
869
|
12 |
|
public static function getConfigData($key) |
|
870
|
|
|
{ |
|
871
|
12 |
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
|
872
|
|
|
|
|
873
|
12 |
|
if ($phpCodeSnifferConfig === null) { |
|
874
|
|
|
return null; |
|
875
|
|
|
} |
|
876
|
|
|
|
|
877
|
12 |
|
if (isset($phpCodeSnifferConfig[$key]) === false) { |
|
878
|
12 |
|
return null; |
|
879
|
|
|
} |
|
880
|
|
|
|
|
881
|
|
|
return $phpCodeSnifferConfig[$key]; |
|
882
|
|
|
|
|
883
|
|
|
}//end getConfigData() |
|
884
|
|
|
|
|
885
|
|
|
|
|
886
|
|
|
/** |
|
887
|
|
|
* Get the path to an executable utility. |
|
888
|
|
|
* |
|
889
|
|
|
* @param string $name The name of the executable utility. |
|
890
|
|
|
* |
|
891
|
|
|
* @return string|null |
|
892
|
|
|
* @see getConfigData() |
|
893
|
|
|
*/ |
|
894
|
|
|
public static function getExecutablePath($name) |
|
895
|
|
|
{ |
|
896
|
|
|
$data = self::getConfigData($name.'_path'); |
|
897
|
|
|
if ($data !== null) { |
|
898
|
|
|
return $data; |
|
899
|
|
|
} |
|
900
|
|
|
|
|
901
|
|
|
if (array_key_exists($name, self::$executablePaths) === true) { |
|
902
|
|
|
return self::$executablePaths[$name]; |
|
903
|
|
|
} |
|
904
|
|
|
|
|
905
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
906
|
|
|
$cmd = 'where '.escapeshellarg($name).' 2> nul'; |
|
907
|
|
|
} else { |
|
908
|
|
|
$cmd = 'which '.escapeshellarg($name); |
|
909
|
|
|
} |
|
910
|
|
|
|
|
911
|
|
|
$result = exec($cmd, $output, $retVal); |
|
912
|
|
|
if ($retVal !== 0) { |
|
913
|
|
|
$result = null; |
|
914
|
|
|
} |
|
915
|
|
|
|
|
916
|
|
|
self::$executablePaths[$name] = $result; |
|
917
|
|
|
return $result; |
|
918
|
|
|
|
|
919
|
|
|
}//end getExecutablePath() |
|
920
|
|
|
|
|
921
|
|
|
|
|
922
|
|
|
/** |
|
923
|
|
|
* Set a single config value. |
|
924
|
|
|
* |
|
925
|
|
|
* @param string $key The name of the config value. |
|
926
|
|
|
* @param string|null $value The value to set. If null, the config |
|
927
|
|
|
* entry is deleted, reverting it to the |
|
928
|
|
|
* default value. |
|
929
|
|
|
* @param boolean $temp Set this config data temporarily for this |
|
930
|
|
|
* script run. This will not write the config |
|
931
|
|
|
* data to the config file. |
|
932
|
|
|
* |
|
933
|
|
|
* @return bool |
|
934
|
|
|
* @see getConfigData() |
|
935
|
|
|
* @throws RuntimeException If the config file can not be written. |
|
936
|
|
|
*/ |
|
937
|
|
|
public static function setConfigData($key, $value, $temp=false) |
|
938
|
|
|
{ |
|
939
|
|
|
if ($temp === false) { |
|
940
|
|
|
$configFile = dirname(__FILE__).'/../CodeSniffer.conf'; |
|
941
|
|
|
if (is_file($configFile) === false |
|
942
|
|
|
&& strpos('@data_dir@', '@data_dir') === false |
|
943
|
|
|
) { |
|
944
|
|
|
// If data_dir was replaced, this is a PEAR install and we can |
|
945
|
|
|
// use the PEAR data dir to store the conf file. |
|
946
|
|
|
$configFile = '@data_dir@/Symplify\PHP7_CodeSniffer/CodeSniffer.conf'; |
|
947
|
|
|
} |
|
948
|
|
|
|
|
949
|
|
|
if (is_file($configFile) === true |
|
950
|
|
|
&& is_writable($configFile) === false |
|
951
|
|
|
) { |
|
952
|
|
|
$error = 'Config file '.$configFile.' is not writable'; |
|
953
|
|
|
throw new RuntimeException($error); |
|
954
|
|
|
} |
|
955
|
|
|
} |
|
956
|
|
|
|
|
957
|
|
|
$phpCodeSnifferConfig = self::getAllConfigData(); |
|
958
|
|
|
|
|
959
|
|
|
if ($value === null) { |
|
960
|
|
|
if (isset($phpCodeSnifferConfig[$key]) === true) { |
|
961
|
|
|
unset($phpCodeSnifferConfig[$key]); |
|
962
|
|
|
} |
|
963
|
|
|
} else { |
|
964
|
|
|
$phpCodeSnifferConfig[$key] = $value; |
|
965
|
|
|
} |
|
966
|
|
|
|
|
967
|
|
|
if ($temp === false) { |
|
968
|
|
|
$output = '<'.'?php'."\n".' $phpCodeSnifferConfig = '; |
|
969
|
|
|
$output .= var_export($phpCodeSnifferConfig, true); |
|
970
|
|
|
$output .= "\n?".'>'; |
|
971
|
|
|
|
|
972
|
|
|
if (file_put_contents($configFile, $output) === false) { |
|
|
|
|
|
|
973
|
|
|
return false; |
|
974
|
|
|
} |
|
975
|
|
|
} |
|
976
|
|
|
|
|
977
|
|
|
self::$configData = $phpCodeSnifferConfig; |
|
978
|
|
|
|
|
979
|
|
|
return true; |
|
980
|
|
|
|
|
981
|
|
|
}//end setConfigData() |
|
982
|
|
|
|
|
983
|
|
|
|
|
984
|
|
|
/** |
|
985
|
|
|
* Get all config data. |
|
986
|
|
|
* |
|
987
|
|
|
* @return array<string, string> |
|
|
|
|
|
|
988
|
|
|
* @see getConfigData() |
|
989
|
|
|
*/ |
|
990
|
12 |
|
public static function getAllConfigData() |
|
991
|
|
|
{ |
|
992
|
12 |
|
if (self::$configData !== null) { |
|
993
|
11 |
|
return self::$configData; |
|
994
|
|
|
} |
|
995
|
|
|
|
|
996
|
1 |
|
$configFile = dirname(__FILE__).'/../CodeSniffer.conf'; |
|
997
|
1 |
|
if (is_file($configFile) === false) { |
|
998
|
|
|
$configFile = '@data_dir@/Symplify\PHP7_CodeSniffer/CodeSniffer.conf'; |
|
999
|
|
|
} |
|
1000
|
|
|
|
|
1001
|
1 |
|
if (is_file($configFile) === false) { |
|
1002
|
|
|
self::$configData = array(); |
|
1003
|
|
|
return array(); |
|
1004
|
|
|
} |
|
1005
|
|
|
|
|
1006
|
1 |
|
include $configFile; |
|
1007
|
1 |
|
self::$configData = $phpCodeSnifferConfig; |
|
|
|
|
|
|
1008
|
1 |
|
return self::$configData; |
|
1009
|
|
|
|
|
1010
|
|
|
}//end getAllConfigData() |
|
1011
|
|
|
|
|
1012
|
|
|
|
|
1013
|
|
|
/** |
|
1014
|
|
|
* Prints out the gathered config data. |
|
1015
|
|
|
* |
|
1016
|
|
|
* @param array $data The config data to print. |
|
1017
|
|
|
* |
|
1018
|
|
|
* @return void |
|
1019
|
|
|
*/ |
|
1020
|
|
|
public function printConfigData($data) |
|
1021
|
|
|
{ |
|
1022
|
|
|
$max = 0; |
|
1023
|
|
|
$keys = array_keys($data); |
|
1024
|
|
|
foreach ($keys as $key) { |
|
1025
|
|
|
$len = strlen($key); |
|
1026
|
|
|
if (strlen($key) > $max) { |
|
1027
|
|
|
$max = $len; |
|
1028
|
|
|
} |
|
1029
|
|
|
} |
|
1030
|
|
|
|
|
1031
|
|
|
if ($max === 0) { |
|
1032
|
|
|
return; |
|
1033
|
|
|
} |
|
1034
|
|
|
|
|
1035
|
|
|
$max += 2; |
|
1036
|
|
|
ksort($data); |
|
1037
|
|
|
foreach ($data as $name => $value) { |
|
1038
|
|
|
echo str_pad($name.': ', $max).$value.PHP_EOL; |
|
1039
|
|
|
} |
|
1040
|
|
|
|
|
1041
|
|
|
}//end printConfigData() |
|
1042
|
|
|
|
|
1043
|
|
|
|
|
1044
|
|
|
}//end class |
|
1045
|
|
|
|
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.