|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Stores the rules used to check and fix files. |
|
4
|
|
|
* |
|
5
|
|
|
* A ruleset object directly maps to a ruleset XML file. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Greg Sherwood <[email protected]> |
|
8
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
|
9
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace PHP_CodeSniffer; |
|
13
|
|
|
|
|
14
|
|
|
use PHP_CodeSniffer\Util; |
|
15
|
|
|
use PHP_CodeSniffer\Exceptions\RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
class Ruleset |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The name of the coding standard being used. |
|
22
|
|
|
* |
|
23
|
|
|
* If a top-level standard includes other standards, or sniffs |
|
24
|
|
|
* from other standards, only the name of the top-level standard |
|
25
|
|
|
* will be stored in here. |
|
26
|
|
|
* |
|
27
|
|
|
* If multiple top-level standards are being loaded into |
|
28
|
|
|
* a single ruleset object, this will store a comma separated list |
|
29
|
|
|
* of the top-level standard names. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $name = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* A list of file paths for the ruleset files being used. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public $paths = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* A list of regular expressions used to ignore specific sniffs for files and folders. |
|
44
|
|
|
* |
|
45
|
|
|
* Is also used to set global exclude patterns. |
|
46
|
|
|
* The key is the regular expression and the value is the type |
|
47
|
|
|
* of ignore pattern (absolute or relative). |
|
48
|
|
|
* |
|
49
|
|
|
* @var array<string, string> |
|
50
|
|
|
*/ |
|
51
|
|
|
public $ignorePatterns = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* A list of regular expressions used to include specific sniffs for files and folders. |
|
55
|
|
|
* |
|
56
|
|
|
* The key is the sniff code and the value is an array with |
|
57
|
|
|
* the key being a regular expression and the value is the type |
|
58
|
|
|
* of ignore pattern (absolute or relative). |
|
59
|
|
|
* |
|
60
|
|
|
* @var array<string, array<string, string>> |
|
61
|
|
|
*/ |
|
62
|
|
|
public $includePatterns = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* An array of sniff objects that are being used to check files. |
|
66
|
|
|
* |
|
67
|
|
|
* The key is the fully qualified name of the sniff class |
|
68
|
|
|
* and the value is the sniff object. |
|
69
|
|
|
* |
|
70
|
|
|
* @var array<string, \PHP_CodeSniffer\Sniff> |
|
71
|
|
|
*/ |
|
72
|
|
|
public $sniffs = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* A mapping of sniff codes to fully qualified class names. |
|
76
|
|
|
* |
|
77
|
|
|
* The key is the sniff code and the value |
|
78
|
|
|
* is the fully qualified name of the sniff class. |
|
79
|
|
|
* |
|
80
|
|
|
* @var array<string, string> |
|
81
|
|
|
*/ |
|
82
|
|
|
public $sniffCodes = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* An array of token types and the sniffs that are listening for them. |
|
86
|
|
|
* |
|
87
|
|
|
* The key is the token name being listened for and the value |
|
88
|
|
|
* is the sniff object. |
|
89
|
|
|
* |
|
90
|
|
|
* @var array<int, \PHP_CodeSniffer\Sniff> |
|
91
|
|
|
*/ |
|
92
|
|
|
public $tokenListeners = []; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* An array of rules from the ruleset.xml file. |
|
96
|
|
|
* |
|
97
|
|
|
* It may be empty, indicating that the ruleset does not override |
|
98
|
|
|
* any of the default sniff settings. |
|
99
|
|
|
* |
|
100
|
|
|
* @var array<string, mixed> |
|
101
|
|
|
*/ |
|
102
|
|
|
public $ruleset = []; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* The directories that the processed rulesets are in. |
|
106
|
|
|
* |
|
107
|
|
|
* @var string[] |
|
108
|
|
|
*/ |
|
109
|
|
|
protected $rulesetDirs = []; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* The config data for the run. |
|
113
|
|
|
* |
|
114
|
|
|
* @var \PHP_CodeSniffer\Config |
|
115
|
|
|
*/ |
|
116
|
|
|
private $config = null; |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Initialise the ruleset that the run will use. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \PHP_CodeSniffer\Config $config The config data for the run. |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __construct(Config $config) |
|
127
|
|
|
{ |
|
128
|
|
|
// Ignore sniff restrictions if caching is on. |
|
129
|
|
|
$restrictions = []; |
|
130
|
|
|
$exclusions = []; |
|
131
|
|
|
if ($config->cache === false) { |
|
132
|
|
|
$restrictions = $config->sniffs; |
|
133
|
|
|
$exclusions = $config->exclude; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->config = $config; |
|
137
|
|
|
$sniffs = []; |
|
138
|
|
|
|
|
139
|
|
|
$standardPaths = []; |
|
140
|
|
|
foreach ($config->standards as $standard) { |
|
141
|
|
|
$installed = Util\Standards::getInstalledStandardPath($standard); |
|
142
|
|
|
if ($installed === null) { |
|
143
|
|
|
$standard = Util\Common::realpath($standard); |
|
144
|
|
|
if (is_dir($standard) === true |
|
145
|
|
|
&& is_file(Util\Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
146
|
|
|
) { |
|
147
|
|
|
$standard = Util\Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
$standard = $installed; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$standardPaths[] = $standard; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
foreach ($standardPaths as $standard) { |
|
157
|
|
|
$ruleset = @simplexml_load_string(file_get_contents($standard)); |
|
158
|
|
|
if ($ruleset !== false) { |
|
159
|
|
|
$standardName = (string) $ruleset['name']; |
|
160
|
|
|
if ($this->name !== '') { |
|
161
|
|
|
$this->name .= ', '; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$this->name .= $standardName; |
|
165
|
|
|
$this->paths[] = $standard; |
|
166
|
|
|
|
|
167
|
|
|
// Allow autoloading of custom files inside this standard. |
|
168
|
|
|
if (isset($ruleset['namespace']) === true) { |
|
169
|
|
|
$namespace = (string) $ruleset['namespace']; |
|
170
|
|
|
} else { |
|
171
|
|
|
$namespace = basename(dirname($standard)); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
Autoload::addSearchPath(dirname($standard), $namespace); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) { |
|
178
|
|
|
// In unit tests, only register the sniffs that the test wants and not the entire standard. |
|
179
|
|
|
try { |
|
180
|
|
|
foreach ($restrictions as $restriction) { |
|
181
|
|
|
$sniffs = array_merge($sniffs, $this->expandRulesetReference($restriction, dirname($standard))); |
|
182
|
|
|
} |
|
183
|
|
|
} catch (RuntimeException $e) { |
|
184
|
|
|
// Sniff reference could not be expanded, which probably means this |
|
185
|
|
|
// is an installed standard. Let the unit test system take care of |
|
186
|
|
|
// setting the correct sniff for testing. |
|
187
|
|
|
return; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
break; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
194
|
|
|
echo "Registering sniffs in the $standardName standard... "; |
|
195
|
|
|
if (count($config->standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) { |
|
196
|
|
|
echo PHP_EOL; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$sniffs = array_merge($sniffs, $this->processRuleset($standard)); |
|
201
|
|
|
}//end foreach |
|
202
|
|
|
|
|
203
|
|
|
$sniffRestrictions = []; |
|
204
|
|
|
foreach ($restrictions as $sniffCode) { |
|
205
|
|
|
$parts = explode('.', strtolower($sniffCode)); |
|
206
|
|
|
$sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff'; |
|
207
|
|
|
$sniffRestrictions[$sniffName] = true; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$sniffExclusions = []; |
|
211
|
|
|
foreach ($exclusions as $sniffCode) { |
|
212
|
|
|
$parts = explode('.', strtolower($sniffCode)); |
|
213
|
|
|
$sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff'; |
|
214
|
|
|
$sniffExclusions[$sniffName] = true; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions); |
|
218
|
|
|
$this->populateTokenListeners(); |
|
219
|
|
|
|
|
220
|
|
|
$numSniffs = count($this->sniffs); |
|
221
|
|
|
if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
222
|
|
|
echo "DONE ($numSniffs sniffs registered)".PHP_EOL; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if ($numSniffs === 0) { |
|
226
|
|
|
throw new RuntimeException('No sniffs were registered'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
}//end __construct() |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Prints a report showing the sniffs contained in a standard. |
|
234
|
|
|
* |
|
235
|
|
|
* @return void |
|
236
|
|
|
*/ |
|
237
|
|
|
public function explain() |
|
238
|
|
|
{ |
|
239
|
|
|
$sniffs = array_keys($this->sniffCodes); |
|
240
|
|
|
sort($sniffs); |
|
241
|
|
|
|
|
242
|
|
|
ob_start(); |
|
243
|
|
|
|
|
244
|
|
|
$lastStandard = null; |
|
245
|
|
|
$lastCount = ''; |
|
246
|
|
|
$sniffCount = count($sniffs); |
|
247
|
|
|
|
|
248
|
|
|
// Add a dummy entry to the end so we loop |
|
249
|
|
|
// one last time and clear the output buffer. |
|
250
|
|
|
$sniffs[] = ''; |
|
251
|
|
|
|
|
252
|
|
|
echo PHP_EOL."The $this->name standard contains $sniffCount sniffs".PHP_EOL; |
|
253
|
|
|
|
|
254
|
|
|
ob_start(); |
|
255
|
|
|
|
|
256
|
|
|
foreach ($sniffs as $i => $sniff) { |
|
257
|
|
|
if ($i === $sniffCount) { |
|
258
|
|
|
$currentStandard = null; |
|
259
|
|
|
} else { |
|
260
|
|
|
$currentStandard = substr($sniff, 0, strpos($sniff, '.')); |
|
261
|
|
|
if ($lastStandard === null) { |
|
262
|
|
|
$lastStandard = $currentStandard; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if ($currentStandard !== $lastStandard) { |
|
267
|
|
|
$sniffList = ob_get_contents(); |
|
268
|
|
|
ob_end_clean(); |
|
269
|
|
|
|
|
270
|
|
|
echo PHP_EOL.$lastStandard.' ('.$lastCount.' sniff'; |
|
271
|
|
|
if ($lastCount > 1) { |
|
272
|
|
|
echo 's'; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
echo ')'.PHP_EOL; |
|
276
|
|
|
echo str_repeat('-', (strlen($lastStandard.$lastCount) + 10)); |
|
277
|
|
|
echo PHP_EOL; |
|
278
|
|
|
echo $sniffList; |
|
279
|
|
|
|
|
280
|
|
|
$lastStandard = $currentStandard; |
|
281
|
|
|
$lastCount = 0; |
|
282
|
|
|
|
|
283
|
|
|
if ($currentStandard === null) { |
|
284
|
|
|
break; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
ob_start(); |
|
288
|
|
|
}//end if |
|
289
|
|
|
|
|
290
|
|
|
echo ' '.$sniff.PHP_EOL; |
|
291
|
|
|
$lastCount++; |
|
292
|
|
|
}//end foreach |
|
293
|
|
|
|
|
294
|
|
|
}//end explain() |
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Processes a single ruleset and returns a list of the sniffs it represents. |
|
299
|
|
|
* |
|
300
|
|
|
* Rules founds within the ruleset are processed immediately, but sniff classes |
|
301
|
|
|
* are not registered by this method. |
|
302
|
|
|
* |
|
303
|
|
|
* @param string $rulesetPath The path to a ruleset XML file. |
|
304
|
|
|
* @param int $depth How many nested processing steps we are in. This |
|
305
|
|
|
* is only used for debug output. |
|
306
|
|
|
* |
|
307
|
|
|
* @return string[] |
|
308
|
|
|
* @throws RuntimeException If the ruleset path is invalid. |
|
309
|
|
|
*/ |
|
310
|
|
|
public function processRuleset($rulesetPath, $depth=0) |
|
311
|
|
|
{ |
|
312
|
|
|
$rulesetPath = Util\Common::realpath($rulesetPath); |
|
313
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
314
|
|
|
echo str_repeat("\t", $depth); |
|
315
|
|
|
echo 'Processing ruleset '.Util\Common::stripBasepath($rulesetPath, $this->config->basepath).PHP_EOL; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$ruleset = @simplexml_load_string(file_get_contents($rulesetPath)); |
|
319
|
|
|
if ($ruleset === false) { |
|
320
|
|
|
throw new RuntimeException("Ruleset $rulesetPath is not valid"); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
$ownSniffs = []; |
|
324
|
|
|
$includedSniffs = []; |
|
325
|
|
|
$excludedSniffs = []; |
|
326
|
|
|
|
|
327
|
|
|
$rulesetDir = dirname($rulesetPath); |
|
328
|
|
|
$this->rulesetDirs[] = $rulesetDir; |
|
329
|
|
|
|
|
330
|
|
|
$sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs'; |
|
331
|
|
|
if (is_dir($sniffDir) === true) { |
|
332
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
333
|
|
|
echo str_repeat("\t", $depth); |
|
334
|
|
|
echo "\tAdding sniff files from ".Util\Common::stripBasepath($sniffDir, $this->config->basepath).' directory'.PHP_EOL; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$ownSniffs = $this->expandSniffDirectory($sniffDir, $depth); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
// Included custom autoloaders. |
|
341
|
|
|
foreach ($ruleset->{'autoload'} as $autoload) { |
|
342
|
|
|
if ($this->shouldProcessElement($autoload) === false) { |
|
343
|
|
|
continue; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
$autoloadPath = (string) $autoload; |
|
347
|
|
|
if (is_file($autoloadPath) === false) { |
|
348
|
|
|
$autoloadPath = Util\Common::realPath(dirname($rulesetPath).DIRECTORY_SEPARATOR.$autoloadPath); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
if ($autoloadPath === false) { |
|
352
|
|
|
throw new RuntimeException('The specified autoload file "'.$autoload.'" does not exist'); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
include_once $autoloadPath; |
|
356
|
|
|
|
|
357
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
358
|
|
|
echo str_repeat("\t", $depth); |
|
359
|
|
|
echo "\t=> included autoloader $autoloadPath".PHP_EOL; |
|
360
|
|
|
} |
|
361
|
|
|
}//end foreach |
|
362
|
|
|
|
|
363
|
|
|
// Process custom sniff config settings. |
|
364
|
|
|
foreach ($ruleset->{'config'} as $config) { |
|
365
|
|
|
if ($this->shouldProcessElement($config) === false) { |
|
366
|
|
|
continue; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
Config::setConfigData((string) $config['name'], (string) $config['value'], true); |
|
370
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
371
|
|
|
echo str_repeat("\t", $depth); |
|
372
|
|
|
echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL; |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
foreach ($ruleset->rule as $rule) { |
|
377
|
|
|
if (isset($rule['ref']) === false |
|
378
|
|
|
|| $this->shouldProcessElement($rule) === false |
|
379
|
|
|
) { |
|
380
|
|
|
continue; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
384
|
|
|
echo str_repeat("\t", $depth); |
|
385
|
|
|
echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
$expandedSniffs = $this->expandRulesetReference((string) $rule['ref'], $rulesetDir, $depth); |
|
389
|
|
|
$newSniffs = array_diff($expandedSniffs, $includedSniffs); |
|
390
|
|
|
$includedSniffs = array_merge($includedSniffs, $expandedSniffs); |
|
391
|
|
|
|
|
392
|
|
|
$parts = explode('.', $rule['ref']); |
|
393
|
|
|
if (count($parts) === 4) { |
|
394
|
|
|
$sniffCode = $parts[0].'.'.$parts[1].'.'.$parts[2]; |
|
395
|
|
|
if (isset($this->ruleset[$sniffCode]['severity']) === true |
|
396
|
|
|
&& $this->ruleset[$sniffCode]['severity'] === 0 |
|
397
|
|
|
) { |
|
398
|
|
|
// This sniff code has already been turned off, but now |
|
399
|
|
|
// it is being explicitly included again, so turn it back on. |
|
400
|
|
|
$this->ruleset[(string) $rule['ref']]['severity'] = 5; |
|
401
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
402
|
|
|
echo str_repeat("\t", $depth); |
|
403
|
|
|
echo "\t\t* disabling sniff exclusion for specific message code *".PHP_EOL; |
|
404
|
|
|
echo str_repeat("\t", $depth); |
|
405
|
|
|
echo "\t\t=> severity set to 5".PHP_EOL; |
|
406
|
|
|
} |
|
407
|
|
|
} else if (empty($newSniffs) === false) { |
|
408
|
|
|
$newSniff = $newSniffs[0]; |
|
409
|
|
|
if (in_array($newSniff, $ownSniffs) === false) { |
|
410
|
|
|
// Including a sniff that hasn't been included higher up, but |
|
411
|
|
|
// only including a single message from it. So turn off all messages in |
|
412
|
|
|
// the sniff, except this one. |
|
413
|
|
|
$this->ruleset[$sniffCode]['severity'] = 0; |
|
414
|
|
|
$this->ruleset[(string) $rule['ref']]['severity'] = 5; |
|
415
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
416
|
|
|
echo str_repeat("\t", $depth); |
|
417
|
|
|
echo "\t\tExcluding sniff \"".$sniffCode.'" except for "'.$parts[3].'"'.PHP_EOL; |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
}//end if |
|
421
|
|
|
}//end if |
|
422
|
|
|
|
|
423
|
|
|
if (isset($rule->exclude) === true) { |
|
424
|
|
|
foreach ($rule->exclude as $exclude) { |
|
425
|
|
|
if (isset($exclude['name']) === false) { |
|
426
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
427
|
|
|
echo str_repeat("\t", $depth); |
|
428
|
|
|
echo "\t\t* ignoring empty exclude rule *".PHP_EOL; |
|
429
|
|
|
echo "\t\t\t=> ".$exclude->asXML().PHP_EOL; |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
continue; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
if ($this->shouldProcessElement($exclude) === false) { |
|
436
|
|
|
continue; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
440
|
|
|
echo str_repeat("\t", $depth); |
|
441
|
|
|
echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
// Check if a single code is being excluded, which is a shortcut |
|
445
|
|
|
// for setting the severity of the message to 0. |
|
446
|
|
|
$parts = explode('.', $exclude['name']); |
|
447
|
|
|
if (count($parts) === 4) { |
|
448
|
|
|
$this->ruleset[(string) $exclude['name']]['severity'] = 0; |
|
449
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
450
|
|
|
echo str_repeat("\t", $depth); |
|
451
|
|
|
echo "\t\t=> severity set to 0".PHP_EOL; |
|
452
|
|
|
} |
|
453
|
|
|
} else { |
|
454
|
|
|
$excludedSniffs = array_merge( |
|
455
|
|
|
$excludedSniffs, |
|
456
|
|
|
$this->expandRulesetReference($exclude['name'], $rulesetDir, ($depth + 1)) |
|
457
|
|
|
); |
|
458
|
|
|
} |
|
459
|
|
|
}//end foreach |
|
460
|
|
|
}//end if |
|
461
|
|
|
|
|
462
|
|
|
$this->processRule($rule, $newSniffs, $depth); |
|
463
|
|
|
}//end foreach |
|
464
|
|
|
|
|
465
|
|
|
// Process custom command line arguments. |
|
466
|
|
|
$cliArgs = []; |
|
467
|
|
|
foreach ($ruleset->{'arg'} as $arg) { |
|
468
|
|
|
if ($this->shouldProcessElement($arg) === false) { |
|
469
|
|
|
continue; |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
if (isset($arg['name']) === true) { |
|
473
|
|
|
$argString = '--'.(string) $arg['name']; |
|
474
|
|
|
if (isset($arg['value']) === true) { |
|
475
|
|
|
$argString .= '='.(string) $arg['value']; |
|
476
|
|
|
} |
|
477
|
|
|
} else { |
|
478
|
|
|
$argString = '-'.(string) $arg['value']; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
$cliArgs[] = $argString; |
|
482
|
|
|
|
|
483
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
484
|
|
|
echo str_repeat("\t", $depth); |
|
485
|
|
|
echo "\t=> set command line value $argString".PHP_EOL; |
|
486
|
|
|
} |
|
487
|
|
|
}//end foreach |
|
488
|
|
|
|
|
489
|
|
|
// Set custom php ini values as CLI args. |
|
490
|
|
|
foreach ($ruleset->{'ini'} as $arg) { |
|
491
|
|
|
if ($this->shouldProcessElement($arg) === false) { |
|
492
|
|
|
continue; |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
if (isset($arg['name']) === false) { |
|
496
|
|
|
continue; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
$name = (string) $arg['name']; |
|
500
|
|
|
$argString = $name; |
|
501
|
|
|
if (isset($arg['value']) === true) { |
|
502
|
|
|
$value = (string) $arg['value']; |
|
503
|
|
|
$argString .= "=$value"; |
|
504
|
|
|
} else { |
|
505
|
|
|
$value = 'true'; |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
$cliArgs[] = '-d'; |
|
509
|
|
|
$cliArgs[] = $argString; |
|
510
|
|
|
|
|
511
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
512
|
|
|
echo str_repeat("\t", $depth); |
|
513
|
|
|
echo "\t=> set PHP ini value $name to $value".PHP_EOL; |
|
514
|
|
|
} |
|
515
|
|
|
}//end foreach |
|
516
|
|
|
|
|
517
|
|
|
if (empty($this->config->files) === true) { |
|
518
|
|
|
// Process hard-coded file paths. |
|
519
|
|
|
foreach ($ruleset->{'file'} as $file) { |
|
520
|
|
|
$file = (string) $file; |
|
521
|
|
|
$cliArgs[] = $file; |
|
522
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
523
|
|
|
echo str_repeat("\t", $depth); |
|
524
|
|
|
echo "\t=> added \"$file\" to the file list".PHP_EOL; |
|
525
|
|
|
} |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
if (empty($cliArgs) === false) { |
|
530
|
|
|
// Change the directory so all relative paths are worked |
|
531
|
|
|
// out based on the location of the ruleset instead of |
|
532
|
|
|
// the location of the user. |
|
533
|
|
|
$inPhar = Util\Common::isPharFile($rulesetDir); |
|
534
|
|
|
if ($inPhar === false) { |
|
535
|
|
|
$currentDir = getcwd(); |
|
536
|
|
|
chdir($rulesetDir); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
$this->config->setCommandLineValues($cliArgs); |
|
540
|
|
|
|
|
541
|
|
|
if ($inPhar === false) { |
|
542
|
|
|
chdir($currentDir); |
|
543
|
|
|
} |
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
|
|
// Process custom ignore pattern rules. |
|
547
|
|
|
foreach ($ruleset->{'exclude-pattern'} as $pattern) { |
|
548
|
|
|
if ($this->shouldProcessElement($pattern) === false) { |
|
549
|
|
|
continue; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
if (isset($pattern['type']) === false) { |
|
553
|
|
|
$pattern['type'] = 'absolute'; |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
$this->ignorePatterns[(string) $pattern] = (string) $pattern['type']; |
|
557
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
558
|
|
|
echo str_repeat("\t", $depth); |
|
559
|
|
|
echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
560
|
|
|
} |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
$includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs)); |
|
564
|
|
|
$excludedSniffs = array_unique($excludedSniffs); |
|
565
|
|
|
|
|
566
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
567
|
|
|
$included = count($includedSniffs); |
|
568
|
|
|
$excluded = count($excludedSniffs); |
|
569
|
|
|
echo str_repeat("\t", $depth); |
|
570
|
|
|
echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL; |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
// Merge our own sniff list with our externally included |
|
574
|
|
|
// sniff list, but filter out any excluded sniffs. |
|
575
|
|
|
$files = []; |
|
576
|
|
|
foreach ($includedSniffs as $sniff) { |
|
577
|
|
|
if (in_array($sniff, $excludedSniffs) === true) { |
|
578
|
|
|
continue; |
|
579
|
|
|
} else { |
|
580
|
|
|
$files[] = Util\Common::realpath($sniff); |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
return $files; |
|
585
|
|
|
|
|
586
|
|
|
}//end processRuleset() |
|
587
|
|
|
|
|
588
|
|
|
|
|
589
|
|
|
/** |
|
590
|
|
|
* Expands a directory into a list of sniff files within. |
|
591
|
|
|
* |
|
592
|
|
|
* @param string $directory The path to a directory. |
|
593
|
|
|
* @param int $depth How many nested processing steps we are in. This |
|
594
|
|
|
* is only used for debug output. |
|
595
|
|
|
* |
|
596
|
|
|
* @return array |
|
597
|
|
|
*/ |
|
598
|
|
|
private function expandSniffDirectory($directory, $depth=0) |
|
599
|
|
|
{ |
|
600
|
|
|
$sniffs = []; |
|
601
|
|
|
|
|
602
|
|
|
$rdi = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS); |
|
603
|
|
|
$di = new \RecursiveIteratorIterator($rdi, 0, \RecursiveIteratorIterator::CATCH_GET_CHILD); |
|
604
|
|
|
|
|
605
|
|
|
$dirLen = strlen($directory); |
|
606
|
|
|
|
|
607
|
|
|
foreach ($di as $file) { |
|
608
|
|
|
$filename = $file->getFilename(); |
|
609
|
|
|
|
|
610
|
|
|
// Skip hidden files. |
|
611
|
|
|
if (substr($filename, 0, 1) === '.') { |
|
612
|
|
|
continue; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
// We are only interested in PHP and sniff files. |
|
616
|
|
|
$fileParts = explode('.', $filename); |
|
617
|
|
|
if (array_pop($fileParts) !== 'php') { |
|
618
|
|
|
continue; |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
$basename = basename($filename, '.php'); |
|
622
|
|
|
if (substr($basename, -5) !== 'Sniff') { |
|
623
|
|
|
continue; |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
$path = $file->getPathname(); |
|
627
|
|
|
|
|
628
|
|
|
// Skip files in hidden directories within the Sniffs directory of this |
|
629
|
|
|
// standard. We use the offset with strpos() to allow hidden directories |
|
630
|
|
|
// before, valid example: |
|
631
|
|
|
// /home/foo/.composer/vendor/squiz/custom_tool/MyStandard/Sniffs/... |
|
632
|
|
|
if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) { |
|
633
|
|
|
continue; |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
637
|
|
|
echo str_repeat("\t", $depth); |
|
638
|
|
|
echo "\t\t=> ".Util\Common::stripBasepath($path, $this->config->basepath).PHP_EOL; |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
$sniffs[] = $path; |
|
642
|
|
|
}//end foreach |
|
643
|
|
|
|
|
644
|
|
|
return $sniffs; |
|
645
|
|
|
|
|
646
|
|
|
}//end expandSniffDirectory() |
|
647
|
|
|
|
|
648
|
|
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* Expands a ruleset reference into a list of sniff files. |
|
651
|
|
|
* |
|
652
|
|
|
* @param string $ref The reference from the ruleset XML file. |
|
653
|
|
|
* @param string $rulesetDir The directory of the ruleset XML file, used to |
|
654
|
|
|
* evaluate relative paths. |
|
655
|
|
|
* @param int $depth How many nested processing steps we are in. This |
|
656
|
|
|
* is only used for debug output. |
|
657
|
|
|
* |
|
658
|
|
|
* @return array |
|
659
|
|
|
* @throws RuntimeException If the reference is invalid. |
|
660
|
|
|
*/ |
|
661
|
|
|
private function expandRulesetReference($ref, $rulesetDir, $depth=0) |
|
662
|
|
|
{ |
|
663
|
|
|
// Ignore internal sniffs codes as they are used to only |
|
664
|
|
|
// hide and change internal messages. |
|
665
|
|
|
if (substr($ref, 0, 9) === 'Internal.') { |
|
666
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
667
|
|
|
echo str_repeat("\t", $depth); |
|
668
|
|
|
echo "\t\t* ignoring internal sniff code *".PHP_EOL; |
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
return []; |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
// As sniffs can't begin with a full stop, assume references in |
|
675
|
|
|
// this format are relative paths and attempt to convert them |
|
676
|
|
|
// to absolute paths. If this fails, let the reference run through |
|
677
|
|
|
// the normal checks and have it fail as normal. |
|
678
|
|
|
if (substr($ref, 0, 1) === '.') { |
|
679
|
|
|
$realpath = Util\Common::realpath($rulesetDir.'/'.$ref); |
|
680
|
|
|
if ($realpath !== false) { |
|
681
|
|
|
$ref = $realpath; |
|
682
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
683
|
|
|
echo str_repeat("\t", $depth); |
|
684
|
|
|
echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; |
|
685
|
|
|
} |
|
686
|
|
|
} |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
// As sniffs can't begin with a tilde, assume references in |
|
690
|
|
|
// this format are relative to the user's home directory. |
|
691
|
|
|
if (substr($ref, 0, 2) === '~/') { |
|
692
|
|
|
$realpath = Util\Common::realpath($ref); |
|
693
|
|
|
if ($realpath !== false) { |
|
694
|
|
|
$ref = $realpath; |
|
695
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
696
|
|
|
echo str_repeat("\t", $depth); |
|
697
|
|
|
echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; |
|
698
|
|
|
} |
|
699
|
|
|
} |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
if (is_file($ref) === true) { |
|
703
|
|
|
if (substr($ref, -9) === 'Sniff.php') { |
|
704
|
|
|
// A single external sniff. |
|
705
|
|
|
$this->rulesetDirs[] = dirname(dirname(dirname($ref))); |
|
706
|
|
|
return [$ref]; |
|
707
|
|
|
} |
|
708
|
|
|
} else { |
|
709
|
|
|
// See if this is a whole standard being referenced. |
|
710
|
|
|
$path = Util\Standards::getInstalledStandardPath($ref); |
|
711
|
|
|
if (Util\Common::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) { |
|
712
|
|
|
// If the ruleset exists inside the phar file, use it. |
|
713
|
|
|
if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
714
|
|
|
$path = $path.DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
715
|
|
|
} else { |
|
716
|
|
|
$path = null; |
|
717
|
|
|
} |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
if ($path !== null) { |
|
721
|
|
|
$ref = $path; |
|
722
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
723
|
|
|
echo str_repeat("\t", $depth); |
|
724
|
|
|
echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; |
|
725
|
|
|
} |
|
726
|
|
|
} else if (is_dir($ref) === false) { |
|
727
|
|
|
// Work out the sniff path. |
|
728
|
|
|
$sepPos = strpos($ref, DIRECTORY_SEPARATOR); |
|
729
|
|
|
if ($sepPos !== false) { |
|
730
|
|
|
$stdName = substr($ref, 0, $sepPos); |
|
731
|
|
|
$path = substr($ref, $sepPos); |
|
732
|
|
|
} else { |
|
733
|
|
|
$parts = explode('.', $ref); |
|
734
|
|
|
$stdName = $parts[0]; |
|
735
|
|
|
if (count($parts) === 1) { |
|
736
|
|
|
// A whole standard? |
|
737
|
|
|
$path = ''; |
|
738
|
|
|
} else if (count($parts) === 2) { |
|
739
|
|
|
// A directory of sniffs? |
|
740
|
|
|
$path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1]; |
|
741
|
|
|
} else { |
|
742
|
|
|
// A single sniff? |
|
743
|
|
|
$path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php'; |
|
744
|
|
|
} |
|
745
|
|
|
} |
|
746
|
|
|
|
|
747
|
|
|
$newRef = false; |
|
748
|
|
|
$stdPath = Util\Standards::getInstalledStandardPath($stdName); |
|
749
|
|
|
if ($stdPath !== null && $path !== '') { |
|
750
|
|
|
if (Util\Common::isPharFile($stdPath) === true |
|
751
|
|
|
&& strpos($stdPath, 'ruleset.xml') === false |
|
752
|
|
|
) { |
|
753
|
|
|
// Phar files can only return the directory, |
|
754
|
|
|
// since ruleset can be omitted if building one standard. |
|
755
|
|
|
$newRef = Util\Common::realpath($stdPath.$path); |
|
756
|
|
|
} else { |
|
757
|
|
|
$newRef = Util\Common::realpath(dirname($stdPath).$path); |
|
758
|
|
|
} |
|
759
|
|
|
} |
|
760
|
|
|
|
|
761
|
|
|
if ($newRef === false) { |
|
762
|
|
|
// The sniff is not locally installed, so check if it is being |
|
763
|
|
|
// referenced as a remote sniff outside the install. We do this |
|
764
|
|
|
// by looking through all directories where we have found ruleset |
|
765
|
|
|
// files before, looking for ones for this particular standard, |
|
766
|
|
|
// and seeing if it is in there. |
|
767
|
|
|
foreach ($this->rulesetDirs as $dir) { |
|
768
|
|
|
if (strtolower(basename($dir)) !== strtolower($stdName)) { |
|
769
|
|
|
continue; |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
|
|
$newRef = Util\Common::realpath($dir.$path); |
|
773
|
|
|
|
|
774
|
|
|
if ($newRef !== false) { |
|
775
|
|
|
$ref = $newRef; |
|
776
|
|
|
} |
|
777
|
|
|
} |
|
778
|
|
|
} else { |
|
779
|
|
|
$ref = $newRef; |
|
780
|
|
|
} |
|
781
|
|
|
|
|
782
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
783
|
|
|
echo str_repeat("\t", $depth); |
|
784
|
|
|
echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; |
|
785
|
|
|
} |
|
786
|
|
|
}//end if |
|
787
|
|
|
}//end if |
|
788
|
|
|
|
|
789
|
|
|
if (is_dir($ref) === true) { |
|
790
|
|
|
if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
791
|
|
|
// We are referencing an external coding standard. |
|
792
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
793
|
|
|
echo str_repeat("\t", $depth); |
|
794
|
|
|
echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL; |
|
795
|
|
|
} |
|
796
|
|
|
|
|
797
|
|
|
return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2)); |
|
798
|
|
|
} else { |
|
799
|
|
|
// We are referencing a whole directory of sniffs. |
|
800
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
801
|
|
|
echo str_repeat("\t", $depth); |
|
802
|
|
|
echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL; |
|
803
|
|
|
echo str_repeat("\t", $depth); |
|
804
|
|
|
echo "\t\tAdding sniff files from directory".PHP_EOL; |
|
805
|
|
|
} |
|
806
|
|
|
|
|
807
|
|
|
return $this->expandSniffDirectory($ref, ($depth + 1)); |
|
808
|
|
|
} |
|
809
|
|
|
} else { |
|
810
|
|
|
if (is_file($ref) === false) { |
|
811
|
|
|
$error = "Referenced sniff \"$ref\" does not exist"; |
|
812
|
|
|
throw new RuntimeException($error); |
|
813
|
|
|
} |
|
814
|
|
|
|
|
815
|
|
|
if (substr($ref, -9) === 'Sniff.php') { |
|
816
|
|
|
// A single sniff. |
|
817
|
|
|
return [$ref]; |
|
818
|
|
|
} else { |
|
819
|
|
|
// Assume an external ruleset.xml file. |
|
820
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
821
|
|
|
echo str_repeat("\t", $depth); |
|
822
|
|
|
echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL; |
|
823
|
|
|
} |
|
824
|
|
|
|
|
825
|
|
|
return $this->processRuleset($ref, ($depth + 2)); |
|
826
|
|
|
} |
|
827
|
|
|
}//end if |
|
828
|
|
|
|
|
829
|
|
|
}//end expandRulesetReference() |
|
830
|
|
|
|
|
831
|
|
|
|
|
832
|
|
|
/** |
|
833
|
|
|
* Processes a rule from a ruleset XML file, overriding built-in defaults. |
|
834
|
|
|
* |
|
835
|
|
|
* @param SimpleXMLElement $rule The rule object from a ruleset XML file. |
|
836
|
|
|
* @param string[] $newSniffs An array of sniffs that got included by this rule. |
|
837
|
|
|
* @param int $depth How many nested processing steps we are in. |
|
838
|
|
|
* This is only used for debug output. |
|
839
|
|
|
* |
|
840
|
|
|
* @return void |
|
841
|
|
|
* @throws RuntimeException If rule settings are invalid. |
|
842
|
|
|
*/ |
|
843
|
|
|
private function processRule($rule, $newSniffs, $depth=0) |
|
844
|
|
|
{ |
|
845
|
|
|
$ref = (string) $rule['ref']; |
|
846
|
|
|
$todo = [$ref]; |
|
847
|
|
|
|
|
848
|
|
|
$parts = explode('.', $ref); |
|
849
|
|
|
if (count($parts) <= 2) { |
|
850
|
|
|
// We are processing a standard or a category of sniffs. |
|
851
|
|
|
foreach ($newSniffs as $sniffFile) { |
|
852
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, $sniffFile); |
|
853
|
|
|
$sniffName = array_pop($parts); |
|
854
|
|
|
$sniffCategory = array_pop($parts); |
|
855
|
|
|
array_pop($parts); |
|
856
|
|
|
$sniffStandard = array_pop($parts); |
|
857
|
|
|
$todo[] = $sniffStandard.'.'.$sniffCategory.'.'.substr($sniffName, 0, -9); |
|
858
|
|
|
} |
|
859
|
|
|
} |
|
860
|
|
|
|
|
861
|
|
|
foreach ($todo as $code) { |
|
862
|
|
|
// Custom severity. |
|
863
|
|
|
if (isset($rule->severity) === true |
|
864
|
|
|
&& $this->shouldProcessElement($rule->severity) === true |
|
865
|
|
|
) { |
|
866
|
|
|
if (isset($this->ruleset[$code]) === false) { |
|
867
|
|
|
$this->ruleset[$code] = []; |
|
868
|
|
|
} |
|
869
|
|
|
|
|
870
|
|
|
$this->ruleset[$code]['severity'] = (int) $rule->severity; |
|
871
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
872
|
|
|
echo str_repeat("\t", $depth); |
|
873
|
|
|
echo "\t\t=> severity set to ".(int) $rule->severity; |
|
874
|
|
|
if ($code !== $ref) { |
|
875
|
|
|
echo " for $code"; |
|
876
|
|
|
} |
|
877
|
|
|
|
|
878
|
|
|
echo PHP_EOL; |
|
879
|
|
|
} |
|
880
|
|
|
} |
|
881
|
|
|
|
|
882
|
|
|
// Custom message type. |
|
883
|
|
|
if (isset($rule->type) === true |
|
884
|
|
|
&& $this->shouldProcessElement($rule->type) === true |
|
885
|
|
|
) { |
|
886
|
|
|
if (isset($this->ruleset[$code]) === false) { |
|
887
|
|
|
$this->ruleset[$code] = []; |
|
888
|
|
|
} |
|
889
|
|
|
|
|
890
|
|
|
$type = strtolower((string) $rule->type); |
|
891
|
|
|
if ($type !== 'error' && $type !== 'warning') { |
|
892
|
|
|
throw new RuntimeException("Message type \"$type\" is invalid; must be \"error\" or \"warning\""); |
|
893
|
|
|
} |
|
894
|
|
|
|
|
895
|
|
|
$this->ruleset[$code]['type'] = $type; |
|
896
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
897
|
|
|
echo str_repeat("\t", $depth); |
|
898
|
|
|
echo "\t\t=> message type set to ".(string) $rule->type; |
|
899
|
|
|
if ($code !== $ref) { |
|
900
|
|
|
echo " for $code"; |
|
901
|
|
|
} |
|
902
|
|
|
|
|
903
|
|
|
echo PHP_EOL; |
|
904
|
|
|
} |
|
905
|
|
|
}//end if |
|
906
|
|
|
|
|
907
|
|
|
// Custom message. |
|
908
|
|
|
if (isset($rule->message) === true |
|
909
|
|
|
&& $this->shouldProcessElement($rule->message) === true |
|
910
|
|
|
) { |
|
911
|
|
|
if (isset($this->ruleset[$code]) === false) { |
|
912
|
|
|
$this->ruleset[$code] = []; |
|
913
|
|
|
} |
|
914
|
|
|
|
|
915
|
|
|
$this->ruleset[$code]['message'] = (string) $rule->message; |
|
916
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
917
|
|
|
echo str_repeat("\t", $depth); |
|
918
|
|
|
echo "\t\t=> message set to ".(string) $rule->message; |
|
919
|
|
|
if ($code !== $ref) { |
|
920
|
|
|
echo " for $code"; |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
|
|
echo PHP_EOL; |
|
924
|
|
|
} |
|
925
|
|
|
} |
|
926
|
|
|
|
|
927
|
|
|
// Custom properties. |
|
928
|
|
|
if (isset($rule->properties) === true |
|
929
|
|
|
&& $this->shouldProcessElement($rule->properties) === true |
|
930
|
|
|
) { |
|
931
|
|
|
foreach ($rule->properties->property as $prop) { |
|
932
|
|
|
if ($this->shouldProcessElement($prop) === false) { |
|
933
|
|
|
continue; |
|
934
|
|
|
} |
|
935
|
|
|
|
|
936
|
|
|
if (isset($this->ruleset[$code]) === false) { |
|
937
|
|
|
$this->ruleset[$code] = [ |
|
938
|
|
|
'properties' => [], |
|
939
|
|
|
]; |
|
940
|
|
|
} else if (isset($this->ruleset[$code]['properties']) === false) { |
|
941
|
|
|
$this->ruleset[$code]['properties'] = []; |
|
942
|
|
|
} |
|
943
|
|
|
|
|
944
|
|
|
$name = (string) $prop['name']; |
|
945
|
|
|
if (isset($prop['type']) === true |
|
946
|
|
|
&& (string) $prop['type'] === 'array' |
|
947
|
|
|
) { |
|
948
|
|
|
$value = (string) $prop['value']; |
|
949
|
|
|
$values = []; |
|
950
|
|
|
foreach (explode(',', $value) as $val) { |
|
951
|
|
|
list($k, $v) = explode('=>', $val.'=>'); |
|
952
|
|
|
if ($v !== '') { |
|
953
|
|
|
$values[trim($k)] = trim($v); |
|
954
|
|
|
} else { |
|
955
|
|
|
$values[] = trim($k); |
|
956
|
|
|
} |
|
957
|
|
|
} |
|
958
|
|
|
|
|
959
|
|
|
$this->ruleset[$code]['properties'][$name] = $values; |
|
960
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
961
|
|
|
echo str_repeat("\t", $depth); |
|
962
|
|
|
echo "\t\t=> array property \"$name\" set to \"$value\""; |
|
963
|
|
|
if ($code !== $ref) { |
|
964
|
|
|
echo " for $code"; |
|
965
|
|
|
} |
|
966
|
|
|
|
|
967
|
|
|
echo PHP_EOL; |
|
968
|
|
|
} |
|
969
|
|
|
} else { |
|
970
|
|
|
$this->ruleset[$code]['properties'][$name] = (string) $prop['value']; |
|
971
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
972
|
|
|
echo str_repeat("\t", $depth); |
|
973
|
|
|
echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"'; |
|
974
|
|
|
if ($code !== $ref) { |
|
975
|
|
|
echo " for $code"; |
|
976
|
|
|
} |
|
977
|
|
|
|
|
978
|
|
|
echo PHP_EOL; |
|
979
|
|
|
} |
|
980
|
|
|
}//end if |
|
981
|
|
|
}//end foreach |
|
982
|
|
|
}//end if |
|
983
|
|
|
|
|
984
|
|
|
// Ignore patterns. |
|
985
|
|
|
foreach ($rule->{'exclude-pattern'} as $pattern) { |
|
986
|
|
|
if ($this->shouldProcessElement($pattern) === false) { |
|
987
|
|
|
continue; |
|
988
|
|
|
} |
|
989
|
|
|
|
|
990
|
|
|
if (isset($this->ignorePatterns[$code]) === false) { |
|
991
|
|
|
$this->ignorePatterns[$code] = []; |
|
992
|
|
|
} |
|
993
|
|
|
|
|
994
|
|
|
if (isset($pattern['type']) === false) { |
|
995
|
|
|
$pattern['type'] = 'absolute'; |
|
996
|
|
|
} |
|
997
|
|
|
|
|
998
|
|
|
$this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type']; |
|
999
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1000
|
|
|
echo str_repeat("\t", $depth); |
|
1001
|
|
|
echo "\t\t=> added rule-specific ".(string) $pattern['type'].' ignore pattern'; |
|
1002
|
|
|
if ($code !== $ref) { |
|
1003
|
|
|
echo " for $code"; |
|
1004
|
|
|
} |
|
1005
|
|
|
|
|
1006
|
|
|
echo ': '.(string) $pattern.PHP_EOL; |
|
1007
|
|
|
} |
|
1008
|
|
|
}//end foreach |
|
1009
|
|
|
|
|
1010
|
|
|
// Include patterns. |
|
1011
|
|
|
foreach ($rule->{'include-pattern'} as $pattern) { |
|
1012
|
|
|
if ($this->shouldProcessElement($pattern) === false) { |
|
1013
|
|
|
continue; |
|
1014
|
|
|
} |
|
1015
|
|
|
|
|
1016
|
|
|
if (isset($this->includePatterns[$code]) === false) { |
|
1017
|
|
|
$this->includePatterns[$code] = []; |
|
1018
|
|
|
} |
|
1019
|
|
|
|
|
1020
|
|
|
if (isset($pattern['type']) === false) { |
|
1021
|
|
|
$pattern['type'] = 'absolute'; |
|
1022
|
|
|
} |
|
1023
|
|
|
|
|
1024
|
|
|
$this->includePatterns[$code][(string) $pattern] = (string) $pattern['type']; |
|
1025
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1026
|
|
|
echo str_repeat("\t", $depth); |
|
1027
|
|
|
echo "\t\t=> added rule-specific ".(string) $pattern['type'].' include pattern'; |
|
1028
|
|
|
if ($code !== $ref) { |
|
1029
|
|
|
echo " for $code"; |
|
1030
|
|
|
} |
|
1031
|
|
|
|
|
1032
|
|
|
echo ': '.(string) $pattern.PHP_EOL; |
|
1033
|
|
|
} |
|
1034
|
|
|
}//end foreach |
|
1035
|
|
|
}//end foreach |
|
1036
|
|
|
|
|
1037
|
|
|
}//end processRule() |
|
1038
|
|
|
|
|
1039
|
|
|
|
|
1040
|
|
|
/** |
|
1041
|
|
|
* Determine if an element should be processed or ignored. |
|
1042
|
|
|
* |
|
1043
|
|
|
* @param SimpleXMLElement $element An object from a ruleset XML file. |
|
1044
|
|
|
* |
|
1045
|
|
|
* @return bool |
|
1046
|
|
|
*/ |
|
1047
|
|
|
private function shouldProcessElement($element) |
|
1048
|
|
|
{ |
|
1049
|
|
|
if (isset($element['phpcbf-only']) === false |
|
1050
|
|
|
&& isset($element['phpcs-only']) === false |
|
1051
|
|
|
) { |
|
1052
|
|
|
// No exceptions are being made. |
|
1053
|
|
|
return true; |
|
1054
|
|
|
} |
|
1055
|
|
|
|
|
1056
|
|
|
if (PHP_CODESNIFFER_CBF === true |
|
1057
|
|
|
&& isset($element['phpcbf-only']) === true |
|
1058
|
|
|
&& (string) $element['phpcbf-only'] === 'true' |
|
1059
|
|
|
) { |
|
1060
|
|
|
return true; |
|
1061
|
|
|
} |
|
1062
|
|
|
|
|
1063
|
|
|
if (PHP_CODESNIFFER_CBF === false |
|
1064
|
|
|
&& isset($element['phpcs-only']) === true |
|
1065
|
|
|
&& (string) $element['phpcs-only'] === 'true' |
|
1066
|
|
|
) { |
|
1067
|
|
|
return true; |
|
1068
|
|
|
} |
|
1069
|
|
|
|
|
1070
|
|
|
return false; |
|
1071
|
|
|
|
|
1072
|
|
|
}//end shouldProcessElement() |
|
1073
|
|
|
|
|
1074
|
|
|
|
|
1075
|
|
|
/** |
|
1076
|
|
|
* Loads and stores sniffs objects used for sniffing files. |
|
1077
|
|
|
* |
|
1078
|
|
|
* @param array $files Paths to the sniff files to register. |
|
1079
|
|
|
* @param array $restrictions The sniff class names to restrict the allowed |
|
1080
|
|
|
* listeners to. |
|
1081
|
|
|
* @param array $exclusions The sniff class names to exclude from the |
|
1082
|
|
|
* listeners list. |
|
1083
|
|
|
* |
|
1084
|
|
|
* @return void |
|
1085
|
|
|
*/ |
|
1086
|
|
|
public function registerSniffs($files, $restrictions, $exclusions) |
|
1087
|
|
|
{ |
|
1088
|
|
|
$listeners = []; |
|
1089
|
|
|
|
|
1090
|
|
|
foreach ($files as $file) { |
|
1091
|
|
|
// Work out where the position of /StandardName/Sniffs/... is |
|
1092
|
|
|
// so we can determine what the class will be called. |
|
1093
|
|
|
$sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR); |
|
1094
|
|
|
if ($sniffPos === false) { |
|
1095
|
|
|
continue; |
|
1096
|
|
|
} |
|
1097
|
|
|
|
|
1098
|
|
|
$slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR); |
|
1099
|
|
|
if ($slashPos === false) { |
|
1100
|
|
|
continue; |
|
1101
|
|
|
} |
|
1102
|
|
|
|
|
1103
|
|
|
$className = Autoload::loadFile($file); |
|
1104
|
|
|
$compareName = Util\Common::cleanSniffClass($className); |
|
1105
|
|
|
|
|
1106
|
|
|
// If they have specified a list of sniffs to restrict to, check |
|
1107
|
|
|
// to see if this sniff is allowed. |
|
1108
|
|
|
if (empty($restrictions) === false |
|
1109
|
|
|
&& isset($restrictions[$compareName]) === false |
|
1110
|
|
|
) { |
|
1111
|
|
|
continue; |
|
1112
|
|
|
} |
|
1113
|
|
|
|
|
1114
|
|
|
// If they have specified a list of sniffs to exclude, check |
|
1115
|
|
|
// to see if this sniff is allowed. |
|
1116
|
|
|
if (empty($exclusions) === false |
|
1117
|
|
|
&& isset($exclusions[$compareName]) === true |
|
1118
|
|
|
) { |
|
1119
|
|
|
continue; |
|
1120
|
|
|
} |
|
1121
|
|
|
|
|
1122
|
|
|
// Skip abstract classes. |
|
1123
|
|
|
$reflection = new \ReflectionClass($className); |
|
1124
|
|
|
if ($reflection->isAbstract() === true) { |
|
1125
|
|
|
continue; |
|
1126
|
|
|
} |
|
1127
|
|
|
|
|
1128
|
|
|
$listeners[$className] = $className; |
|
1129
|
|
|
|
|
1130
|
|
|
if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
1131
|
|
|
echo "Registered $className".PHP_EOL; |
|
1132
|
|
|
} |
|
1133
|
|
|
}//end foreach |
|
1134
|
|
|
|
|
1135
|
|
|
$this->sniffs = $listeners; |
|
1136
|
|
|
|
|
1137
|
|
|
}//end registerSniffs() |
|
1138
|
|
|
|
|
1139
|
|
|
|
|
1140
|
|
|
/** |
|
1141
|
|
|
* Populates the array of PHP_CodeSniffer_Sniff's for this file. |
|
1142
|
|
|
* |
|
1143
|
|
|
* @return void |
|
1144
|
|
|
* @throws RuntimeException If sniff registration fails. |
|
1145
|
|
|
*/ |
|
1146
|
|
|
public function populateTokenListeners() |
|
1147
|
|
|
{ |
|
1148
|
|
|
// Construct a list of listeners indexed by token being listened for. |
|
1149
|
|
|
$this->tokenListeners = []; |
|
1150
|
|
|
|
|
1151
|
|
|
foreach ($this->sniffs as $sniffClass => $sniffObject) { |
|
1152
|
|
|
$this->sniffs[$sniffClass] = null; |
|
1153
|
|
|
$this->sniffs[$sniffClass] = new $sniffClass(); |
|
1154
|
|
|
|
|
1155
|
|
|
$sniffCode = Util\Common::getSniffCode($sniffClass); |
|
1156
|
|
|
$this->sniffCodes[$sniffCode] = $sniffClass; |
|
1157
|
|
|
|
|
1158
|
|
|
// Set custom properties. |
|
1159
|
|
|
if (isset($this->ruleset[$sniffCode]['properties']) === true) { |
|
1160
|
|
|
foreach ($this->ruleset[$sniffCode]['properties'] as $name => $value) { |
|
1161
|
|
|
$this->setSniffProperty($sniffClass, $name, $value); |
|
1162
|
|
|
} |
|
1163
|
|
|
} |
|
1164
|
|
|
|
|
1165
|
|
|
$tokenizers = []; |
|
1166
|
|
|
$vars = get_class_vars($sniffClass); |
|
1167
|
|
|
if (isset($vars['supportedTokenizers']) === true) { |
|
1168
|
|
|
foreach ($vars['supportedTokenizers'] as $tokenizer) { |
|
1169
|
|
|
$tokenizers[$tokenizer] = $tokenizer; |
|
1170
|
|
|
} |
|
1171
|
|
|
} else { |
|
1172
|
|
|
$tokenizers = ['PHP' => 'PHP']; |
|
1173
|
|
|
} |
|
1174
|
|
|
|
|
1175
|
|
|
$tokens = $this->sniffs[$sniffClass]->register(); |
|
1176
|
|
|
if (is_array($tokens) === false) { |
|
1177
|
|
|
$msg = "Sniff $sniffClass register() method must return an array"; |
|
1178
|
|
|
throw new RuntimeException($msg); |
|
1179
|
|
|
} |
|
1180
|
|
|
|
|
1181
|
|
|
$ignorePatterns = []; |
|
1182
|
|
|
$patterns = $this->getIgnorePatterns($sniffCode); |
|
1183
|
|
|
foreach ($patterns as $pattern => $type) { |
|
1184
|
|
|
$replacements = [ |
|
1185
|
|
|
'\\,' => ',', |
|
1186
|
|
|
'*' => '.*', |
|
1187
|
|
|
]; |
|
1188
|
|
|
|
|
1189
|
|
|
$ignorePatterns[] = strtr($pattern, $replacements); |
|
1190
|
|
|
} |
|
1191
|
|
|
|
|
1192
|
|
|
$includePatterns = []; |
|
1193
|
|
|
$patterns = $this->getIncludePatterns($sniffCode); |
|
1194
|
|
|
foreach ($patterns as $pattern => $type) { |
|
1195
|
|
|
$replacements = [ |
|
1196
|
|
|
'\\,' => ',', |
|
1197
|
|
|
'*' => '.*', |
|
1198
|
|
|
]; |
|
1199
|
|
|
|
|
1200
|
|
|
$includePatterns[] = strtr($pattern, $replacements); |
|
1201
|
|
|
} |
|
1202
|
|
|
|
|
1203
|
|
|
foreach ($tokens as $token) { |
|
1204
|
|
|
if (isset($this->tokenListeners[$token]) === false) { |
|
1205
|
|
|
$this->tokenListeners[$token] = []; |
|
1206
|
|
|
} |
|
1207
|
|
|
|
|
1208
|
|
|
if (isset($this->tokenListeners[$token][$sniffClass]) === false) { |
|
1209
|
|
|
$this->tokenListeners[$token][$sniffClass] = [ |
|
1210
|
|
|
'class' => $sniffClass, |
|
1211
|
|
|
'source' => $sniffCode, |
|
1212
|
|
|
'tokenizers' => $tokenizers, |
|
1213
|
|
|
'ignore' => $ignorePatterns, |
|
1214
|
|
|
'include' => $includePatterns, |
|
1215
|
|
|
]; |
|
1216
|
|
|
} |
|
1217
|
|
|
} |
|
1218
|
|
|
}//end foreach |
|
1219
|
|
|
|
|
1220
|
|
|
}//end populateTokenListeners() |
|
1221
|
|
|
|
|
1222
|
|
|
|
|
1223
|
|
|
/** |
|
1224
|
|
|
* Set a single property for a sniff. |
|
1225
|
|
|
* |
|
1226
|
|
|
* @param string $sniffClass The class name of the sniff. |
|
1227
|
|
|
* @param string $name The name of the property to change. |
|
1228
|
|
|
* @param string $value The new value of the property. |
|
1229
|
|
|
* |
|
1230
|
|
|
* @return void |
|
1231
|
|
|
*/ |
|
1232
|
|
|
public function setSniffProperty($sniffClass, $name, $value) |
|
1233
|
|
|
{ |
|
1234
|
|
|
// Setting a property for a sniff we are not using. |
|
1235
|
|
|
if (isset($this->sniffs[$sniffClass]) === false) { |
|
1236
|
|
|
return; |
|
1237
|
|
|
} |
|
1238
|
|
|
|
|
1239
|
|
|
$name = trim($name); |
|
1240
|
|
|
if (is_string($value) === true) { |
|
1241
|
|
|
$value = trim($value); |
|
1242
|
|
|
} |
|
1243
|
|
|
|
|
1244
|
|
|
// Special case for booleans. |
|
1245
|
|
|
if ($value === 'true') { |
|
1246
|
|
|
$value = true; |
|
1247
|
|
|
} else if ($value === 'false') { |
|
1248
|
|
|
$value = false; |
|
1249
|
|
|
} |
|
1250
|
|
|
|
|
1251
|
|
|
$this->sniffs[$sniffClass]->$name = $value; |
|
1252
|
|
|
|
|
1253
|
|
|
}//end setSniffProperty() |
|
1254
|
|
|
|
|
1255
|
|
|
|
|
1256
|
|
|
/** |
|
1257
|
|
|
* Gets the array of ignore patterns. |
|
1258
|
|
|
* |
|
1259
|
|
|
* Optionally takes a listener to get ignore patterns specified |
|
1260
|
|
|
* for that sniff only. |
|
1261
|
|
|
* |
|
1262
|
|
|
* @param string $listener The listener to get patterns for. If NULL, all |
|
1263
|
|
|
* patterns are returned. |
|
1264
|
|
|
* |
|
1265
|
|
|
* @return array |
|
1266
|
|
|
*/ |
|
1267
|
|
|
public function getIgnorePatterns($listener=null) |
|
1268
|
|
|
{ |
|
1269
|
|
|
if ($listener === null) { |
|
1270
|
|
|
return $this->ignorePatterns; |
|
1271
|
|
|
} |
|
1272
|
|
|
|
|
1273
|
|
|
if (isset($this->ignorePatterns[$listener]) === true) { |
|
1274
|
|
|
return $this->ignorePatterns[$listener]; |
|
1275
|
|
|
} |
|
1276
|
|
|
|
|
1277
|
|
|
return []; |
|
1278
|
|
|
|
|
1279
|
|
|
}//end getIgnorePatterns() |
|
1280
|
|
|
|
|
1281
|
|
|
|
|
1282
|
|
|
/** |
|
1283
|
|
|
* Gets the array of include patterns. |
|
1284
|
|
|
* |
|
1285
|
|
|
* Optionally takes a listener to get include patterns specified |
|
1286
|
|
|
* for that sniff only. |
|
1287
|
|
|
* |
|
1288
|
|
|
* @param string $listener The listener to get patterns for. If NULL, all |
|
1289
|
|
|
* patterns are returned. |
|
1290
|
|
|
* |
|
1291
|
|
|
* @return array |
|
1292
|
|
|
*/ |
|
1293
|
|
|
public function getIncludePatterns($listener=null) |
|
1294
|
|
|
{ |
|
1295
|
|
|
if ($listener === null) { |
|
1296
|
|
|
return $this->includePatterns; |
|
1297
|
|
|
} |
|
1298
|
|
|
|
|
1299
|
|
|
if (isset($this->includePatterns[$listener]) === true) { |
|
1300
|
|
|
return $this->includePatterns[$listener]; |
|
1301
|
|
|
} |
|
1302
|
|
|
|
|
1303
|
|
|
return []; |
|
1304
|
|
|
|
|
1305
|
|
|
}//end getIncludePatterns() |
|
1306
|
|
|
|
|
1307
|
|
|
|
|
1308
|
|
|
}//end class |
|
1309
|
|
|
|