• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 13753331620

09 Mar 2025 10:51PM UTC coverage: 78.621% (+0.07%) from 78.556%
13753331620

Pull #857

github

web-flow
Merge 94a9e77ce into 575523895
Pull Request #857: Ruleset: improve error handling

121 of 126 new or added lines in 2 files covered. (96.03%)

5 existing lines in 2 files now uncovered.

24794 of 31536 relevant lines covered (78.62%)

66.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.42
/src/Ruleset.php
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 <gsherwood@squiz.net>
8
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
9
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10
 */
11

12
namespace PHP_CodeSniffer;
13

14
use PHP_CodeSniffer\Exceptions\RuntimeException;
15
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
16
use PHP_CodeSniffer\Util\Common;
17
use PHP_CodeSniffer\Util\MsgCollector;
18
use PHP_CodeSniffer\Util\Standards;
19
use RecursiveDirectoryIterator;
20
use RecursiveIteratorIterator;
21
use ReflectionClass;
22
use stdClass;
23

24
class Ruleset
25
{
26

27
    /**
28
     * The name of the coding standard being used.
29
     *
30
     * If a top-level standard includes other standards, or sniffs
31
     * from other standards, only the name of the top-level standard
32
     * will be stored in here.
33
     *
34
     * If multiple top-level standards are being loaded into
35
     * a single ruleset object, this will store a comma separated list
36
     * of the top-level standard names.
37
     *
38
     * @var string
39
     */
40
    public $name = '';
41

42
    /**
43
     * A list of file paths for the ruleset files being used.
44
     *
45
     * @var string[]
46
     */
47
    public $paths = [];
48

49
    /**
50
     * A list of regular expressions used to ignore specific sniffs for files and folders.
51
     *
52
     * Is also used to set global exclude patterns.
53
     * The key is the regular expression and the value is the type
54
     * of ignore pattern (absolute or relative).
55
     *
56
     * @var array<string, array>
57
     */
58
    public $ignorePatterns = [];
59

60
    /**
61
     * A list of regular expressions used to include specific sniffs for files and folders.
62
     *
63
     * The key is the sniff code and the value is an array with
64
     * the key being a regular expression and the value is the type
65
     * of ignore pattern (absolute or relative).
66
     *
67
     * @var array<string, array<string, string>>
68
     */
69
    public $includePatterns = [];
70

71
    /**
72
     * An array of sniff objects that are being used to check files.
73
     *
74
     * The key is the fully qualified name of the sniff class
75
     * and the value is the sniff object.
76
     *
77
     * @var array<string, \PHP_CodeSniffer\Sniffs\Sniff>
78
     */
79
    public $sniffs = [];
80

81
    /**
82
     * A mapping of sniff codes to fully qualified class names.
83
     *
84
     * The key is the sniff code and the value
85
     * is the fully qualified name of the sniff class.
86
     *
87
     * @var array<string, string>
88
     */
89
    public $sniffCodes = [];
90

91
    /**
92
     * An array of token types and the sniffs that are listening for them.
93
     *
94
     * The key is the token name being listened for and the value
95
     * is the sniff object.
96
     *
97
     * @var array<int, array<string, array<string, mixed>>>
98
     */
99
    public $tokenListeners = [];
100

101
    /**
102
     * An array of rules from the ruleset.xml file.
103
     *
104
     * It may be empty, indicating that the ruleset does not override
105
     * any of the default sniff settings.
106
     *
107
     * @var array<string, mixed>
108
     */
109
    public $ruleset = [];
110

111
    /**
112
     * The directories that the processed rulesets are in.
113
     *
114
     * @var string[]
115
     */
116
    protected $rulesetDirs = [];
117

118
    /**
119
     * The config data for the run.
120
     *
121
     * @var \PHP_CodeSniffer\Config
122
     */
123
    private $config = null;
124

125
    /**
126
     * An array of the names of sniffs which have been marked as deprecated.
127
     *
128
     * The key is the sniff code and the value
129
     * is the fully qualified name of the sniff class.
130
     *
131
     * @var array<string, string>
132
     */
133
    private $deprecatedSniffs = [];
134

135
    /**
136
     * Message collector object.
137
     *
138
     * User-facing messages should be collected via this object for display once the ruleset processing has finished.
139
     *
140
     * The following type of errors should *NOT* be collected, but should still throw their own `RuntimeException`:
141
     * - Errors which could cause other (uncollectable) errors further into the ruleset processing, like a missing autoload file.
142
     * - Errors which are directly aimed at and only intended for sniff developers or integrators
143
     *   (in contrast to ruleset maintainers or end-users).
144
     *
145
     * @var \PHP_CodeSniffer\Util\MsgCollector
146
     */
147
    private $msgCache;
148

149

150
    /**
151
     * Initialise the ruleset that the run will use.
152
     *
153
     * @param \PHP_CodeSniffer\Config $config The config data for the run.
154
     *
155
     * @return void
156
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If blocking errors were encountered when processing the ruleset.
157
     */
158
    public function __construct(Config $config)
44✔
159
    {
160
        $this->config   = $config;
44✔
161
        $restrictions   = $config->sniffs;
44✔
162
        $exclusions     = $config->exclude;
44✔
163
        $sniffs         = [];
44✔
164
        $this->msgCache = new MsgCollector();
44✔
165

166
        $standardPaths = [];
44✔
167
        foreach ($config->standards as $standard) {
44✔
168
            $installed = Standards::getInstalledStandardPath($standard);
44✔
169
            if ($installed === null) {
44✔
170
                $standard = Common::realpath($standard);
14✔
171
                if (is_dir($standard) === true
14✔
172
                    && is_file(Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true
14✔
173
                ) {
5✔
174
                    $standard = Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml');
6✔
175
                }
1✔
176
            } else {
5✔
177
                $standard = $installed;
30✔
178
            }
179

180
            $standardPaths[] = $standard;
44✔
181
        }
15✔
182

183
        foreach ($standardPaths as $standard) {
44✔
184
            $ruleset = @simplexml_load_string(file_get_contents($standard));
44✔
185
            if ($ruleset !== false) {
44✔
186
                $standardName = (string) $ruleset['name'];
44✔
187
                if ($this->name !== '') {
44✔
188
                    $this->name .= ', ';
3✔
189
                }
1✔
190

191
                $this->name .= $standardName;
44✔
192

193
                // Allow autoloading of custom files inside this standard.
194
                if (isset($ruleset['namespace']) === true) {
44✔
195
                    $namespace = (string) $ruleset['namespace'];
6✔
196
                } else {
2✔
197
                    $namespace = basename(dirname($standard));
38✔
198
                }
199

200
                Autoload::addSearchPath(dirname($standard), $namespace);
44✔
201
            }
15✔
202

203
            if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) {
44✔
204
                // In unit tests, only register the sniffs that the test wants and not the entire standard.
205
                foreach ($restrictions as $restriction) {
9✔
206
                    $sniffs = array_merge($sniffs, $this->expandRulesetReference($restriction, dirname($standard)));
9✔
207
                }
3✔
208

209
                if (empty($sniffs) === true) {
9✔
210
                    // Sniff reference could not be expanded, which probably means this
211
                    // is an installed standard. Let the unit test system take care of
212
                    // setting the correct sniff for testing.
213
                    return;
3✔
214
                }
215

216
                break;
6✔
217
            }
218

219
            if (PHP_CODESNIFFER_VERBOSITY === 1) {
35✔
220
                echo "Registering sniffs in the $standardName standard... ";
×
221
                if (count($config->standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
×
222
                    echo PHP_EOL;
×
223
                }
224
            }
225

226
            $sniffs = array_merge($sniffs, $this->processRuleset($standard));
35✔
227
        }//end foreach
14✔
228

229
        // Ignore sniff restrictions if caching is on.
230
        if ($config->cache === true) {
41✔
231
            $restrictions = [];
6✔
232
            $exclusions   = [];
6✔
233
        }
2✔
234

235
        $sniffRestrictions = [];
41✔
236
        foreach ($restrictions as $sniffCode) {
41✔
237
            $parts     = explode('.', strtolower($sniffCode));
6✔
238
            $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
6✔
239
            $sniffRestrictions[$sniffName] = true;
6✔
240
        }
14✔
241

242
        $sniffExclusions = [];
41✔
243
        foreach ($exclusions as $sniffCode) {
41✔
244
            $parts     = explode('.', strtolower($sniffCode));
3✔
245
            $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
3✔
246
            $sniffExclusions[$sniffName] = true;
3✔
247
        }
14✔
248

249
        $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions);
41✔
250
        $this->populateTokenListeners();
41✔
251

252
        $numSniffs = count($this->sniffs);
41✔
253
        if (PHP_CODESNIFFER_VERBOSITY === 1) {
41✔
254
            echo "DONE ($numSniffs sniffs registered)".PHP_EOL;
×
255
        }
256

257
        if ($numSniffs === 0) {
41✔
258
            $this->msgCache->add('No sniffs were registered.', MsgCollector::ERROR);
3✔
259
        }
1✔
260

261
        $this->displayCachedMessages();
41✔
262

263
    }//end __construct()
25✔
264

265

266
    /**
267
     * Prints a report showing the sniffs contained in a standard.
268
     *
269
     * @return void
270
     */
271
    public function explain()
18✔
272
    {
273
        $sniffs = array_keys($this->sniffCodes);
18✔
274
        sort($sniffs, (SORT_NATURAL | SORT_FLAG_CASE));
18✔
275

276
        $sniffCount = count($sniffs);
18✔
277

278
        // Add a dummy entry to the end so we loop one last time
279
        // and echo out the collected info about the last standard.
280
        $sniffs[] = '';
18✔
281

282
        $summaryLine = PHP_EOL."The $this->name standard contains 1 sniff".PHP_EOL;
18✔
283
        if ($sniffCount !== 1) {
18✔
284
            $summaryLine = str_replace('1 sniff', "$sniffCount sniffs", $summaryLine);
15✔
285
        }
5✔
286

287
        echo $summaryLine;
18✔
288

289
        $lastStandard     = null;
18✔
290
        $lastCount        = 0;
18✔
291
        $sniffsInStandard = [];
18✔
292

293
        foreach ($sniffs as $i => $sniff) {
18✔
294
            if ($i === $sniffCount) {
18✔
295
                $currentStandard = null;
18✔
296
            } else {
6✔
297
                $currentStandard = substr($sniff, 0, strpos($sniff, '.'));
18✔
298
                if ($lastStandard === null) {
18✔
299
                    $lastStandard = $currentStandard;
18✔
300
                }
6✔
301
            }
302

303
            // Reached the first item in the next standard.
304
            // Echo out the info collected from the previous standard.
305
            if ($currentStandard !== $lastStandard) {
18✔
306
                $subTitle = $lastStandard.' ('.$lastCount.' sniff';
18✔
307
                if ($lastCount > 1) {
18✔
308
                    $subTitle .= 's';
15✔
309
                }
5✔
310

311
                $subTitle .= ')';
18✔
312

313
                echo PHP_EOL.$subTitle.PHP_EOL;
18✔
314
                echo str_repeat('-', strlen($subTitle)).PHP_EOL;
18✔
315
                echo '  '.implode(PHP_EOL.'  ', $sniffsInStandard).PHP_EOL;
18✔
316

317
                $lastStandard     = $currentStandard;
18✔
318
                $lastCount        = 0;
18✔
319
                $sniffsInStandard = [];
18✔
320

321
                if ($currentStandard === null) {
18✔
322
                    break;
18✔
323
                }
324
            }//end if
4✔
325

326
            if (isset($this->deprecatedSniffs[$sniff]) === true) {
18✔
327
                $sniff .= ' *';
3✔
328
            }
1✔
329

330
            $sniffsInStandard[] = $sniff;
18✔
331
            ++$lastCount;
18✔
332
        }//end foreach
6✔
333

334
        if (count($this->deprecatedSniffs) > 0) {
18✔
335
            echo PHP_EOL.'* Sniffs marked with an asterix are deprecated.'.PHP_EOL;
3✔
336
        }
1✔
337

338
    }//end explain()
12✔
339

340

341
    /**
342
     * Checks whether any deprecated sniffs were registered via the ruleset.
343
     *
344
     * @return bool
345
     */
346
    public function hasSniffDeprecations()
57✔
347
    {
348
        return (count($this->deprecatedSniffs) > 0);
57✔
349

350
    }//end hasSniffDeprecations()
351

352

353
    /**
354
     * Prints an information block about deprecated sniffs being used.
355
     *
356
     * @return void
357
     *
358
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When the interface implementation is faulty.
359
     */
360
    public function showSniffDeprecations()
51✔
361
    {
362
        if ($this->hasSniffDeprecations() === false) {
51✔
363
            return;
3✔
364
        }
365

366
        // Don't show deprecation notices in quiet mode, in explain mode
367
        // or when the documentation is being shown.
368
        // Documentation and explain will mark a sniff as deprecated natively
369
        // and also call the Ruleset multiple times which would lead to duplicate
370
        // display of the deprecation messages.
371
        if ($this->config->quiet === true
48✔
372
            || $this->config->explain === true
46✔
373
            || $this->config->generator !== null
47✔
374
        ) {
16✔
375
            return;
9✔
376
        }
377

378
        $reportWidth = $this->config->reportWidth;
39✔
379
        // Message takes report width minus the leading dash + two spaces, minus a one space gutter at the end.
380
        $maxMessageWidth = ($reportWidth - 4);
39✔
381
        $maxActualWidth  = 0;
39✔
382

383
        ksort($this->deprecatedSniffs, (SORT_NATURAL | SORT_FLAG_CASE));
39✔
384

385
        $messages        = [];
39✔
386
        $messageTemplate = 'This sniff has been deprecated since %s and will be removed in %s. %s';
39✔
387
        $errorTemplate   = 'ERROR: The %s::%s() method must return a %sstring, received %s';
39✔
388

389
        foreach ($this->deprecatedSniffs as $sniffCode => $className) {
39✔
390
            if (isset($this->sniffs[$className]) === false) {
39✔
391
                // Should only be possible in test situations, but some extra defensive coding is never a bad thing.
392
                continue;
6✔
393
            }
394

395
            // Verify the interface was implemented correctly.
396
            // Unfortunately can't be safeguarded via type declarations yet.
397
            $deprecatedSince = $this->sniffs[$className]->getDeprecationVersion();
33✔
398
            if (is_string($deprecatedSince) === false) {
33✔
399
                throw new RuntimeException(
3✔
400
                    sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', gettype($deprecatedSince))
3✔
401
                );
2✔
402
            }
403

404
            if ($deprecatedSince === '') {
30✔
405
                throw new RuntimeException(
3✔
406
                    sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', '""')
3✔
407
                );
2✔
408
            }
409

410
            $removedIn = $this->sniffs[$className]->getRemovalVersion();
27✔
411
            if (is_string($removedIn) === false) {
27✔
412
                throw new RuntimeException(
3✔
413
                    sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', gettype($removedIn))
3✔
414
                );
2✔
415
            }
416

417
            if ($removedIn === '') {
24✔
418
                throw new RuntimeException(
3✔
419
                    sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', '""')
3✔
420
                );
2✔
421
            }
422

423
            $customMessage = $this->sniffs[$className]->getDeprecationMessage();
21✔
424
            if (is_string($customMessage) === false) {
21✔
425
                throw new RuntimeException(
3✔
426
                    sprintf($errorTemplate, $className, 'getDeprecationMessage', '', gettype($customMessage))
3✔
427
                );
2✔
428
            }
429

430
            // Truncate the error code if there is not enough report width.
431
            if (strlen($sniffCode) > $maxMessageWidth) {
18✔
432
                $sniffCode = substr($sniffCode, 0, ($maxMessageWidth - 3)).'...';
3✔
433
            }
1✔
434

435
            $message        = '-  '."\033[36m".$sniffCode."\033[0m".PHP_EOL;
18✔
436
            $maxActualWidth = max($maxActualWidth, strlen($sniffCode));
18✔
437

438
            // Normalize new line characters in custom message.
439
            $customMessage = preg_replace('`\R`', PHP_EOL, $customMessage);
18✔
440

441
            $notice         = trim(sprintf($messageTemplate, $deprecatedSince, $removedIn, $customMessage));
18✔
442
            $maxActualWidth = max($maxActualWidth, min(strlen($notice), $maxMessageWidth));
18✔
443
            $wrapped        = wordwrap($notice, $maxMessageWidth, PHP_EOL);
18✔
444
            $message       .= '   '.implode(PHP_EOL.'   ', explode(PHP_EOL, $wrapped));
18✔
445

446
            $messages[] = $message;
18✔
447
        }//end foreach
8✔
448

449
        if (count($messages) === 0) {
24✔
450
            return;
6✔
451
        }
452

453
        $summaryLine = "WARNING: The $this->name standard uses 1 deprecated sniff";
18✔
454
        $sniffCount  = count($messages);
18✔
455
        if ($sniffCount !== 1) {
18✔
456
            $summaryLine = str_replace('1 deprecated sniff', "$sniffCount deprecated sniffs", $summaryLine);
6✔
457
        }
2✔
458

459
        $maxActualWidth = max($maxActualWidth, min(strlen($summaryLine), $maxMessageWidth));
18✔
460

461
        $summaryLine = wordwrap($summaryLine, $reportWidth, PHP_EOL);
18✔
462
        if ($this->config->colors === true) {
18✔
463
            echo "\033[33m".$summaryLine."\033[0m".PHP_EOL;
×
464
        } else {
465
            echo $summaryLine.PHP_EOL;
18✔
466
        }
467

468
        $messages = implode(PHP_EOL, $messages);
18✔
469
        if ($this->config->colors === false) {
18✔
470
            $messages = Common::stripColors($messages);
18✔
471
        }
6✔
472

473
        echo str_repeat('-', min(($maxActualWidth + 4), $reportWidth)).PHP_EOL;
18✔
474
        echo $messages;
18✔
475

476
        $closer = wordwrap('Deprecated sniffs are still run, but will stop working at some point in the future.', $reportWidth, PHP_EOL);
18✔
477
        echo PHP_EOL.PHP_EOL.$closer.PHP_EOL.PHP_EOL;
18✔
478

479
    }//end showSniffDeprecations()
12✔
480

481

482
    /**
483
     * Print any notices encountered while processing the ruleset(s).
484
     *
485
     * Note: these messages aren't shown at the time they are encountered to avoid "one error hiding behind another".
486
     * This way the (end-)user gets to see all of them in one go.
487
     *
488
     * @return void
489
     *
490
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If blocking errors were encountered.
491
     */
492
    private function displayCachedMessages()
50✔
493
    {
494
        // Don't show deprecations/notices/warnings in quiet mode, in explain mode
495
        // or when the documentation is being shown.
496
        // Documentation and explain will call the Ruleset multiple times which
497
        // would lead to duplicate display of the messages.
498
        if ($this->msgCache->containsBlockingErrors() === false
50✔
499
            && ($this->config->quiet === true
47✔
500
            || $this->config->explain === true
42✔
501
            || $this->config->generator !== null)
45✔
502
        ) {
17✔
503
            return;
18✔
504
        }
505

506
        $this->msgCache->display();
41✔
507

508
    }//end displayCachedMessages()
15✔
509

510

511
    /**
512
     * Processes a single ruleset and returns a list of the sniffs it represents.
513
     *
514
     * Rules founds within the ruleset are processed immediately, but sniff classes
515
     * are not registered by this method.
516
     *
517
     * @param string $rulesetPath The path to a ruleset XML file.
518
     * @param int    $depth       How many nested processing steps we are in. This
519
     *                            is only used for debug output.
520
     *
521
     * @return string[]
522
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException - If the ruleset path is invalid.
523
     *                                                      - If a specified autoload file could not be found.
524
     */
525
    public function processRuleset($rulesetPath, $depth=0)
44✔
526
    {
527
        $rulesetPath = Common::realpath($rulesetPath);
44✔
528
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
44✔
529
            echo str_repeat("\t", $depth);
×
530
            echo 'Processing ruleset '.Common::stripBasepath($rulesetPath, $this->config->basepath).PHP_EOL;
×
531
        }
532

533
        libxml_use_internal_errors(true);
44✔
534
        $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
44✔
535
        if ($ruleset === false) {
44✔
536
            $errorMsg = "ERROR: Ruleset $rulesetPath is not valid".PHP_EOL;
9✔
537
            $errors   = libxml_get_errors();
9✔
538
            foreach ($errors as $error) {
9✔
539
                $errorMsg .= '- On line '.$error->line.', column '.$error->column.': '.$error->message;
6✔
540
            }
3✔
541

542
            libxml_clear_errors();
9✔
543
            throw new RuntimeException($errorMsg);
9✔
544
        }
545

546
        libxml_use_internal_errors(false);
35✔
547

548
        $ownSniffs      = [];
35✔
549
        $includedSniffs = [];
35✔
550
        $excludedSniffs = [];
35✔
551

552
        $this->paths[]       = $rulesetPath;
35✔
553
        $rulesetDir          = dirname($rulesetPath);
35✔
554
        $this->rulesetDirs[] = $rulesetDir;
35✔
555

556
        $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs';
35✔
557
        if (is_dir($sniffDir) === true) {
35✔
558
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
559
                echo str_repeat("\t", $depth);
×
560
                echo "\tAdding sniff files from ".Common::stripBasepath($sniffDir, $this->config->basepath).' directory'.PHP_EOL;
×
561
            }
562

563
            $ownSniffs = $this->expandSniffDirectory($sniffDir, $depth);
9✔
564
        }
3✔
565

566
        // Include custom autoloaders.
567
        foreach ($ruleset->{'autoload'} as $autoload) {
35✔
568
            if ($this->shouldProcessElement($autoload) === false) {
9✔
569
                continue;
6✔
570
            }
571

572
            $autoloadPath = (string) $autoload;
9✔
573

574
            // Try relative autoload paths first.
575
            $relativePath = Common::realPath(dirname($rulesetPath).DIRECTORY_SEPARATOR.$autoloadPath);
9✔
576

577
            if ($relativePath !== false && is_file($relativePath) === true) {
9✔
578
                $autoloadPath = $relativePath;
6✔
579
            } else if (is_file($autoloadPath) === false) {
9✔
580
                throw new RuntimeException('ERROR: The specified autoload file "'.$autoload.'" does not exist');
3✔
581
            }
582

583
            include_once $autoloadPath;
6✔
584

585
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
586
                echo str_repeat("\t", $depth);
×
587
                echo "\t=> included autoloader $autoloadPath".PHP_EOL;
×
588
            }
589
        }//end foreach
11✔
590

591
        // Process custom sniff config settings.
592
        foreach ($ruleset->{'config'} as $config) {
32✔
593
            if ($this->shouldProcessElement($config) === false) {
12✔
594
                continue;
6✔
595
            }
596

597
            Config::setConfigData((string) $config['name'], (string) $config['value'], true);
12✔
598
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
599
                echo str_repeat("\t", $depth);
×
600
                echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL;
×
601
            }
602
        }
11✔
603

604
        foreach ($ruleset->rule as $rule) {
32✔
605
            if (isset($rule['ref']) === false
32✔
606
                || $this->shouldProcessElement($rule) === false
32✔
607
            ) {
11✔
608
                continue;
9✔
609
            }
610

611
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
32✔
612
                echo str_repeat("\t", $depth);
×
613
                echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL;
×
614
            }
615

616
            $expandedSniffs = $this->expandRulesetReference((string) $rule['ref'], $rulesetDir, $depth);
32✔
617
            $newSniffs      = array_diff($expandedSniffs, $includedSniffs);
32✔
618
            $includedSniffs = array_merge($includedSniffs, $expandedSniffs);
32✔
619

620
            $parts = explode('.', $rule['ref']);
32✔
621
            if (count($parts) === 4
32✔
622
                && $parts[0] !== ''
32✔
623
                && $parts[1] !== ''
32✔
624
                && $parts[2] !== ''
32✔
625
            ) {
11✔
626
                $sniffCode = $parts[0].'.'.$parts[1].'.'.$parts[2];
9✔
627
                if (isset($this->ruleset[$sniffCode]['severity']) === true
9✔
628
                    && $this->ruleset[$sniffCode]['severity'] === 0
9✔
629
                ) {
3✔
630
                    // This sniff code has already been turned off, but now
631
                    // it is being explicitly included again, so turn it back on.
632
                    $this->ruleset[(string) $rule['ref']]['severity'] = 5;
6✔
633
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
634
                        echo str_repeat("\t", $depth);
×
635
                        echo "\t\t* disabling sniff exclusion for specific message code *".PHP_EOL;
×
636
                        echo str_repeat("\t", $depth);
×
637
                        echo "\t\t=> severity set to 5".PHP_EOL;
2✔
638
                    }
639
                } else if (empty($newSniffs) === false) {
9✔
640
                    $newSniff = $newSniffs[0];
6✔
641
                    if (in_array($newSniff, $ownSniffs, true) === false) {
6✔
642
                        // Including a sniff that hasn't been included higher up, but
643
                        // only including a single message from it. So turn off all messages in
644
                        // the sniff, except this one.
645
                        $this->ruleset[$sniffCode]['severity']            = 0;
6✔
646
                        $this->ruleset[(string) $rule['ref']]['severity'] = 5;
6✔
647
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
648
                            echo str_repeat("\t", $depth);
×
649
                            echo "\t\tExcluding sniff \"".$sniffCode.'" except for "'.$parts[3].'"'.PHP_EOL;
×
650
                        }
651
                    }
2✔
652
                }//end if
2✔
653
            }//end if
3✔
654

655
            if (isset($rule->exclude) === true) {
32✔
656
                foreach ($rule->exclude as $exclude) {
15✔
657
                    if (isset($exclude['name']) === false) {
15✔
658
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
659
                            echo str_repeat("\t", $depth);
×
660
                            echo "\t\t* ignoring empty exclude rule *".PHP_EOL;
×
661
                            echo "\t\t\t=> ".$exclude->asXML().PHP_EOL;
×
662
                        }
663

664
                        continue;
3✔
665
                    }
666

667
                    if ($this->shouldProcessElement($exclude) === false) {
12✔
668
                        continue;
6✔
669
                    }
670

671
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
672
                        echo str_repeat("\t", $depth);
×
673
                        echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL;
×
674
                    }
675

676
                    // Check if a single code is being excluded, which is a shortcut
677
                    // for setting the severity of the message to 0.
678
                    $parts = explode('.', $exclude['name']);
12✔
679
                    if (count($parts) === 4) {
12✔
680
                        $this->ruleset[(string) $exclude['name']]['severity'] = 0;
6✔
681
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
682
                            echo str_repeat("\t", $depth);
×
683
                            echo "\t\t=> severity set to 0".PHP_EOL;
2✔
684
                        }
685
                    } else {
2✔
686
                        $excludedSniffs = array_merge(
6✔
687
                            $excludedSniffs,
6✔
688
                            $this->expandRulesetReference((string) $exclude['name'], $rulesetDir, ($depth + 1))
6✔
689
                        );
4✔
690
                    }
691
                }//end foreach
5✔
692
            }//end if
5✔
693

694
            $this->processRule($rule, $newSniffs, $depth);
32✔
695
        }//end foreach
11✔
696

697
        // Process custom command line arguments.
698
        $cliArgs = [];
32✔
699
        foreach ($ruleset->{'arg'} as $arg) {
32✔
700
            if ($this->shouldProcessElement($arg) === false) {
9✔
701
                continue;
6✔
702
            }
703

704
            if (isset($arg['name']) === true) {
9✔
705
                $argString = '--'.(string) $arg['name'];
9✔
706
                if (isset($arg['value']) === true) {
9✔
707
                    $argString .= '='.(string) $arg['value'];
9✔
708
                }
3✔
709
            } else {
3✔
710
                $argString = '-'.(string) $arg['value'];
6✔
711
            }
712

713
            $cliArgs[] = $argString;
9✔
714

715
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
716
                echo str_repeat("\t", $depth);
×
717
                echo "\t=> set command line value $argString".PHP_EOL;
×
718
            }
719
        }//end foreach
11✔
720

721
        // Set custom php ini values as CLI args.
722
        foreach ($ruleset->{'ini'} as $arg) {
32✔
723
            if ($this->shouldProcessElement($arg) === false) {
9✔
724
                continue;
6✔
725
            }
726

727
            if (isset($arg['name']) === false) {
9✔
728
                continue;
3✔
729
            }
730

731
            $name      = (string) $arg['name'];
9✔
732
            $argString = $name;
9✔
733
            if (isset($arg['value']) === true) {
9✔
734
                $value      = (string) $arg['value'];
6✔
735
                $argString .= "=$value";
6✔
736
            } else {
2✔
737
                $value = 'true';
3✔
738
            }
739

740
            $cliArgs[] = '-d';
9✔
741
            $cliArgs[] = $argString;
9✔
742

743
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
744
                echo str_repeat("\t", $depth);
×
745
                echo "\t=> set PHP ini value $name to $value".PHP_EOL;
×
746
            }
747
        }//end foreach
11✔
748

749
        if (empty($this->config->files) === true) {
32✔
750
            // Process hard-coded file paths.
751
            foreach ($ruleset->{'file'} as $file) {
32✔
752
                $file      = (string) $file;
12✔
753
                $cliArgs[] = $file;
12✔
754
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
755
                    echo str_repeat("\t", $depth);
×
756
                    echo "\t=> added \"$file\" to the file list".PHP_EOL;
×
757
                }
758
            }
11✔
759
        }
11✔
760

761
        if (empty($cliArgs) === false) {
32✔
762
            // Change the directory so all relative paths are worked
763
            // out based on the location of the ruleset instead of
764
            // the location of the user.
765
            $inPhar = Common::isPharFile($rulesetDir);
18✔
766
            if ($inPhar === false) {
18✔
767
                $currentDir = getcwd();
18✔
768
                chdir($rulesetDir);
18✔
769
            }
6✔
770

771
            $this->config->setCommandLineValues($cliArgs);
18✔
772

773
            if ($inPhar === false) {
18✔
774
                chdir($currentDir);
18✔
775
            }
6✔
776
        }
6✔
777

778
        // Process custom ignore pattern rules.
779
        foreach ($ruleset->{'exclude-pattern'} as $pattern) {
32✔
780
            if ($this->shouldProcessElement($pattern) === false) {
6✔
781
                continue;
6✔
782
            }
783

784
            if (isset($pattern['type']) === false) {
6✔
785
                $pattern['type'] = 'absolute';
6✔
786
            }
2✔
787

788
            $this->ignorePatterns[(string) $pattern] = (string) $pattern['type'];
6✔
789
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
790
                echo str_repeat("\t", $depth);
×
791
                echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL;
×
792
            }
793
        }
11✔
794

795
        $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs));
32✔
796
        $excludedSniffs = array_unique($excludedSniffs);
32✔
797

798
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
32✔
799
            $included = count($includedSniffs);
×
800
            $excluded = count($excludedSniffs);
×
801
            echo str_repeat("\t", $depth);
×
802
            echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL;
×
803
        }
804

805
        // Merge our own sniff list with our externally included
806
        // sniff list, but filter out any excluded sniffs.
807
        $files = [];
32✔
808
        foreach ($includedSniffs as $sniff) {
32✔
809
            if (in_array($sniff, $excludedSniffs, true) === true) {
32✔
810
                continue;
6✔
811
            } else {
812
                $files[] = Common::realpath($sniff);
32✔
813
            }
814
        }
11✔
815

816
        return $files;
32✔
817

818
    }//end processRuleset()
819

820

821
    /**
822
     * Expands a directory into a list of sniff files within.
823
     *
824
     * @param string $directory The path to a directory.
825
     * @param int    $depth     How many nested processing steps we are in. This
826
     *                          is only used for debug output.
827
     *
828
     * @return array
829
     */
830
    private function expandSniffDirectory($directory, $depth=0)
6✔
831
    {
832
        $sniffs = [];
6✔
833

834
        $rdi = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
6✔
835
        $di  = new RecursiveIteratorIterator($rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD);
6✔
836

837
        $dirLen = strlen($directory);
6✔
838

839
        foreach ($di as $file) {
6✔
840
            $filename = $file->getFilename();
6✔
841

842
            // Skip hidden files.
843
            if (substr($filename, 0, 1) === '.') {
6✔
844
                continue;
6✔
845
            }
846

847
            // We are only interested in PHP and sniff files.
848
            $fileParts = explode('.', $filename);
6✔
849
            if (array_pop($fileParts) !== 'php') {
6✔
850
                continue;
3✔
851
            }
852

853
            $basename = basename($filename, '.php');
6✔
854
            if (substr($basename, -5) !== 'Sniff') {
6✔
855
                continue;
3✔
856
            }
857

858
            $path = $file->getPathname();
6✔
859

860
            // Skip files in hidden directories within the Sniffs directory of this
861
            // standard. We use the offset with strpos() to allow hidden directories
862
            // before, valid example:
863
            // /home/foo/.composer/vendor/squiz/custom_tool/MyStandard/Sniffs/...
864
            if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) {
6✔
865
                continue;
3✔
866
            }
867

868
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
869
                echo str_repeat("\t", $depth);
×
870
                echo "\t\t=> ".Common::stripBasepath($path, $this->config->basepath).PHP_EOL;
×
871
            }
872

873
            $sniffs[] = $path;
6✔
874
        }//end foreach
2✔
875

876
        return $sniffs;
6✔
877

878
    }//end expandSniffDirectory()
879

880

881
    /**
882
     * Expands a ruleset reference into a list of sniff files.
883
     *
884
     * @param string $ref        The reference from the ruleset XML file.
885
     * @param string $rulesetDir The directory of the ruleset XML file, used to
886
     *                           evaluate relative paths.
887
     * @param int    $depth      How many nested processing steps we are in. This
888
     *                           is only used for debug output.
889
     *
890
     * @return array
891
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the reference is invalid.
892
     */
893
    private function expandRulesetReference($ref, $rulesetDir, $depth=0)
53✔
894
    {
895
        // Ignore internal sniffs codes as they are used to only
896
        // hide and change internal messages.
897
        if (substr($ref, 0, 9) === 'Internal.') {
53✔
898
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
899
                echo str_repeat("\t", $depth);
×
900
                echo "\t\t* ignoring internal sniff code *".PHP_EOL;
×
901
            }
902

903
            return [];
3✔
904
        }
905

906
        // As sniffs can't begin with a full stop, assume references in
907
        // this format are relative paths and attempt to convert them
908
        // to absolute paths. If this fails, let the reference run through
909
        // the normal checks and have it fail as normal.
910
        if (substr($ref, 0, 1) === '.') {
53✔
911
            $realpath = Common::realpath($rulesetDir.'/'.$ref);
12✔
912
            if ($realpath !== false) {
12✔
913
                $ref = $realpath;
6✔
914
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
915
                    echo str_repeat("\t", $depth);
×
916
                    echo "\t\t=> ".Common::stripBasepath($ref, $this->config->basepath).PHP_EOL;
×
917
                }
918
            }
2✔
919
        }
4✔
920

921
        // As sniffs can't begin with a tilde, assume references in
922
        // this format are relative to the user's home directory.
923
        if (substr($ref, 0, 2) === '~/') {
53✔
924
            $realpath = Common::realpath($ref);
9✔
925
            if ($realpath !== false) {
9✔
926
                $ref = $realpath;
3✔
927
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
928
                    echo str_repeat("\t", $depth);
×
929
                    echo "\t\t=> ".Common::stripBasepath($ref, $this->config->basepath).PHP_EOL;
×
930
                }
931
            }
1✔
932
        }
3✔
933

934
        if (is_file($ref) === true) {
53✔
935
            if (substr($ref, -9) === 'Sniff.php') {
11✔
936
                // A single external sniff.
937
                $this->rulesetDirs[] = dirname(dirname(dirname($ref)));
11✔
938
                return [$ref];
11✔
939
            }
940
        } else {
1✔
941
            // See if this is a whole standard being referenced.
942
            $path = Standards::getInstalledStandardPath($ref);
48✔
943
            if ($path !== null && Common::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) {
48✔
944
                // If the ruleset exists inside the phar file, use it.
945
                if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
×
946
                    $path .= DIRECTORY_SEPARATOR.'ruleset.xml';
×
947
                } else {
948
                    $path = null;
×
949
                }
950
            }
951

952
            if ($path !== null) {
48✔
953
                $ref = $path;
6✔
954
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
955
                    echo str_repeat("\t", $depth);
×
956
                    echo "\t\t=> ".Common::stripBasepath($ref, $this->config->basepath).PHP_EOL;
2✔
957
                }
958
            } else if (is_dir($ref) === false) {
46✔
959
                // Work out the sniff path.
960
                $sepPos = strpos($ref, DIRECTORY_SEPARATOR);
39✔
961
                if ($sepPos !== false) {
39✔
962
                    $stdName = substr($ref, 0, $sepPos);
9✔
963
                    $path    = substr($ref, $sepPos);
9✔
964
                } else {
3✔
965
                    $parts   = explode('.', $ref);
30✔
966
                    $stdName = $parts[0];
30✔
967
                    if (count($parts) === 1) {
30✔
968
                        // A whole standard?
969
                        $path = '';
3✔
970
                    } else if (count($parts) === 2) {
28✔
971
                        // A directory of sniffs?
972
                        $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1];
6✔
973
                    } else {
2✔
974
                        // A single sniff?
975
                        $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php';
24✔
976
                    }
977
                }
978

979
                $newRef  = false;
39✔
980
                $stdPath = Standards::getInstalledStandardPath($stdName);
39✔
981
                if ($stdPath !== null && $path !== '') {
39✔
982
                    if (Common::isPharFile($stdPath) === true
15✔
983
                        && strpos($stdPath, 'ruleset.xml') === false
15✔
984
                    ) {
5✔
985
                        // Phar files can only return the directory,
986
                        // since ruleset can be omitted if building one standard.
987
                        $newRef = Common::realpath($stdPath.$path);
×
988
                    } else {
989
                        $newRef = Common::realpath(dirname($stdPath).$path);
15✔
990
                    }
991
                }
5✔
992

993
                if ($newRef === false) {
39✔
994
                    // The sniff is not locally installed, so check if it is being
995
                    // referenced as a remote sniff outside the install. We do this
996
                    // by looking through all directories where we have found ruleset
997
                    // files before, looking for ones for this particular standard,
998
                    // and seeing if it is in there.
999
                    foreach ($this->rulesetDirs as $dir) {
33✔
1000
                        if (strtolower(basename($dir)) !== strtolower($stdName)) {
33✔
1001
                            continue;
33✔
1002
                        }
1003

1004
                        $newRef = Common::realpath($dir.$path);
×
1005

1006
                        if ($newRef !== false) {
×
1007
                            $ref = $newRef;
×
1008
                        }
1009
                    }
11✔
1010
                } else {
11✔
1011
                    $ref = $newRef;
6✔
1012
                }
1013

1014
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
39✔
1015
                    echo str_repeat("\t", $depth);
×
1016
                    echo "\t\t=> ".Common::stripBasepath($ref, $this->config->basepath).PHP_EOL;
×
1017
                }
1018
            }//end if
13✔
1019
        }//end if
1020

1021
        if (is_dir($ref) === true) {
48✔
1022
            if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
9✔
1023
                // We are referencing an external coding standard.
1024
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
1025
                    echo str_repeat("\t", $depth);
×
1026
                    echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL;
×
1027
                }
1028

1029
                return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2));
3✔
1030
            } else {
1031
                // We are referencing a whole directory of sniffs.
1032
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
1033
                    echo str_repeat("\t", $depth);
×
1034
                    echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL;
×
1035
                    echo str_repeat("\t", $depth);
×
1036
                    echo "\t\tAdding sniff files from directory".PHP_EOL;
×
1037
                }
1038

1039
                return $this->expandSniffDirectory($ref, ($depth + 1));
9✔
1040
            }
1041
        } else {
1042
            if (is_file($ref) === false) {
42✔
1043
                $this->msgCache->add("Referenced sniff \"$ref\" does not exist.", MsgCollector::ERROR);
33✔
1044
                return [];
33✔
1045
            }
1046

1047
            if (substr($ref, -9) === 'Sniff.php') {
9✔
1048
                // A single sniff.
1049
                return [$ref];
6✔
1050
            } else {
1051
                // Assume an external ruleset.xml file.
1052
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1053
                    echo str_repeat("\t", $depth);
×
1054
                    echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL;
×
1055
                }
1056

1057
                return $this->processRuleset($ref, ($depth + 2));
6✔
1058
            }
1059
        }//end if
1060

1061
    }//end expandRulesetReference()
1062

1063

1064
    /**
1065
     * Processes a rule from a ruleset XML file, overriding built-in defaults.
1066
     *
1067
     * @param \SimpleXMLElement $rule      The rule object from a ruleset XML file.
1068
     * @param string[]          $newSniffs An array of sniffs that got included by this rule.
1069
     * @param int               $depth     How many nested processing steps we are in.
1070
     *                                     This is only used for debug output.
1071
     *
1072
     * @return void
1073
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If rule settings are invalid.
1074
     */
1075
    private function processRule($rule, $newSniffs, $depth=0)
23✔
1076
    {
1077
        $ref  = (string) $rule['ref'];
23✔
1078
        $todo = [$ref];
23✔
1079

1080
        $parts      = explode('.', $ref);
23✔
1081
        $partsCount = count($parts);
23✔
1082
        if ($partsCount <= 2
15✔
1083
            || $partsCount > count(array_filter($parts))
20✔
1084
            || in_array($ref, $newSniffs) === true
21✔
1085
        ) {
8✔
1086
            // We are processing a standard, a category of sniffs or a relative path inclusion.
1087
            foreach ($newSniffs as $sniffFile) {
20✔
1088
                $parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
14✔
1089
                if (count($parts) === 1 && DIRECTORY_SEPARATOR === '\\') {
14✔
1090
                    // Path using forward slashes while running on Windows.
1091
                    $parts = explode('/', $sniffFile);
×
1092
                }
1093

1094
                $sniffName     = array_pop($parts);
14✔
1095
                $sniffCategory = array_pop($parts);
14✔
1096
                array_pop($parts);
14✔
1097
                $sniffStandard = array_pop($parts);
14✔
1098
                $todo[]        = $sniffStandard.'.'.$sniffCategory.'.'.substr($sniffName, 0, -9);
14✔
1099
            }
7✔
1100
        }
7✔
1101

1102
        foreach ($todo as $code) {
23✔
1103
            // Custom severity.
1104
            if (isset($rule->severity) === true
23✔
1105
                && $this->shouldProcessElement($rule->severity) === true
23✔
1106
            ) {
8✔
1107
                if (isset($this->ruleset[$code]) === false) {
9✔
1108
                    $this->ruleset[$code] = [];
9✔
1109
                }
3✔
1110

1111
                $this->ruleset[$code]['severity'] = (int) $rule->severity;
9✔
1112
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
1113
                    echo str_repeat("\t", $depth);
×
1114
                    echo "\t\t=> severity set to ".(int) $rule->severity;
×
1115
                    if ($code !== $ref) {
×
1116
                        echo " for $code";
×
1117
                    }
1118

1119
                    echo PHP_EOL;
×
1120
                }
1121
            }
3✔
1122

1123
            // Custom message type.
1124
            if (isset($rule->type) === true
23✔
1125
                && $this->shouldProcessElement($rule->type) === true
23✔
1126
            ) {
8✔
1127
                if (isset($this->ruleset[$code]) === false) {
9✔
1128
                    $this->ruleset[$code] = [];
3✔
1129
                }
1✔
1130

1131
                $type = strtolower((string) $rule->type);
9✔
1132
                if ($type !== 'error' && $type !== 'warning') {
9✔
1133
                    $message = "Message type \"$type\" for \"$code\" is invalid; must be \"error\" or \"warning\".";
3✔
1134
                    $this->msgCache->add($message, MsgCollector::ERROR);
3✔
1135
                } else {
1✔
1136
                    $this->ruleset[$code]['type'] = $type;
6✔
1137
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
1138
                        echo str_repeat("\t", $depth);
×
NEW
1139
                        echo "\t\t=> message type set to ".(string) $rule->type;
×
NEW
1140
                        if ($code !== $ref) {
×
NEW
1141
                            echo " for $code";
×
1142
                        }
1143

NEW
1144
                        echo PHP_EOL;
×
1145
                    }
1146
                }
1147
            }//end if
3✔
1148

1149
            // Custom message.
1150
            if (isset($rule->message) === true
23✔
1151
                && $this->shouldProcessElement($rule->message) === true
23✔
1152
            ) {
8✔
1153
                if (isset($this->ruleset[$code]) === false) {
6✔
1154
                    $this->ruleset[$code] = [];
×
1155
                }
1156

1157
                $this->ruleset[$code]['message'] = (string) $rule->message;
6✔
1158
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1159
                    echo str_repeat("\t", $depth);
×
1160
                    echo "\t\t=> message set to ".(string) $rule->message;
×
1161
                    if ($code !== $ref) {
×
1162
                        echo " for $code";
×
1163
                    }
1164

1165
                    echo PHP_EOL;
×
1166
                }
1167
            }
2✔
1168

1169
            // Custom properties.
1170
            if (isset($rule->properties) === true
23✔
1171
                && $this->shouldProcessElement($rule->properties) === true
23✔
1172
            ) {
8✔
1173
                $propertyScope = 'standard';
17✔
1174
                if ($code === $ref || substr($ref, -9) === 'Sniff.php') {
17✔
1175
                    $propertyScope = 'sniff';
17✔
1176
                }
6✔
1177

1178
                foreach ($rule->properties->property as $prop) {
17✔
1179
                    if ($this->shouldProcessElement($prop) === false) {
17✔
1180
                        continue;
6✔
1181
                    }
1182

1183
                    if (isset($this->ruleset[$code]) === false) {
17✔
1184
                        $this->ruleset[$code] = [
17✔
1185
                            'properties' => [],
17✔
1186
                        ];
6✔
1187
                    } else if (isset($this->ruleset[$code]['properties']) === false) {
14✔
1188
                        $this->ruleset[$code]['properties'] = [];
3✔
1189
                    }
1✔
1190

1191
                    $name = (string) $prop['name'];
17✔
1192
                    if (isset($prop['type']) === true
17✔
1193
                        && (string) $prop['type'] === 'array'
17✔
1194
                    ) {
6✔
1195
                        $values = [];
12✔
1196
                        if (isset($prop['extend']) === true
12✔
1197
                            && (string) $prop['extend'] === 'true'
12✔
1198
                            && isset($this->ruleset[$code]['properties'][$name]['value']) === true
12✔
1199
                        ) {
4✔
1200
                            $values = $this->ruleset[$code]['properties'][$name]['value'];
9✔
1201
                        }
3✔
1202

1203
                        if (isset($prop->element) === true) {
12✔
1204
                            $printValue = '';
12✔
1205
                            foreach ($prop->element as $element) {
12✔
1206
                                if ($this->shouldProcessElement($element) === false) {
12✔
1207
                                    continue;
6✔
1208
                                }
1209

1210
                                $value = (string) $element['value'];
12✔
1211
                                if (isset($element['key']) === true) {
12✔
1212
                                    $key          = (string) $element['key'];
3✔
1213
                                    $values[$key] = $value;
3✔
1214
                                    $printValue  .= $key.'=>'.$value.',';
3✔
1215
                                } else {
1✔
1216
                                    $values[]    = $value;
12✔
1217
                                    $printValue .= $value.',';
12✔
1218
                                }
1219
                            }
4✔
1220

1221
                            $printValue = rtrim($printValue, ',');
12✔
1222
                        } else {
4✔
1223
                            $value      = (string) $prop['value'];
3✔
1224
                            $printValue = $value;
3✔
1225
                            foreach (explode(',', $value) as $val) {
3✔
1226
                                list($k, $v) = explode('=>', $val.'=>');
3✔
1227
                                if ($v !== '') {
3✔
1228
                                    $values[trim($k)] = trim($v);
3✔
1229
                                } else {
1✔
1230
                                    $values[] = trim($k);
3✔
1231
                                }
1232
                            }
1✔
1233
                        }//end if
1234

1235
                        $this->ruleset[$code]['properties'][$name] = [
12✔
1236
                            'value' => $values,
12✔
1237
                            'scope' => $propertyScope,
12✔
1238
                        ];
4✔
1239
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
1240
                            echo str_repeat("\t", $depth);
×
1241
                            echo "\t\t=> array property \"$name\" set to \"$printValue\"";
×
1242
                            if ($code !== $ref) {
×
1243
                                echo " for $code";
×
1244
                            }
1245

1246
                            echo PHP_EOL;
4✔
1247
                        }
1248
                    } else {
4✔
1249
                        $this->ruleset[$code]['properties'][$name] = [
17✔
1250
                            'value' => (string) $prop['value'],
17✔
1251
                            'scope' => $propertyScope,
17✔
1252
                        ];
6✔
1253
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
17✔
1254
                            echo str_repeat("\t", $depth);
×
1255
                            echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"';
×
1256
                            if ($code !== $ref) {
×
1257
                                echo " for $code";
×
1258
                            }
1259

1260
                            echo PHP_EOL;
×
1261
                        }
1262
                    }//end if
1263
                }//end foreach
6✔
1264
            }//end if
6✔
1265

1266
            // Ignore patterns.
1267
            foreach ($rule->{'exclude-pattern'} as $pattern) {
23✔
1268
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1269
                    continue;
6✔
1270
                }
1271

1272
                if (isset($this->ignorePatterns[$code]) === false) {
6✔
1273
                    $this->ignorePatterns[$code] = [];
6✔
1274
                }
2✔
1275

1276
                if (isset($pattern['type']) === false) {
6✔
1277
                    $pattern['type'] = 'absolute';
6✔
1278
                }
2✔
1279

1280
                $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type'];
6✔
1281
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1282
                    echo str_repeat("\t", $depth);
×
1283
                    echo "\t\t=> added rule-specific ".(string) $pattern['type'].' ignore pattern';
×
1284
                    if ($code !== $ref) {
×
1285
                        echo " for $code";
×
1286
                    }
1287

1288
                    echo ': '.(string) $pattern.PHP_EOL;
×
1289
                }
1290
            }//end foreach
8✔
1291

1292
            // Include patterns.
1293
            foreach ($rule->{'include-pattern'} as $pattern) {
23✔
1294
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1295
                    continue;
6✔
1296
                }
1297

1298
                if (isset($this->includePatterns[$code]) === false) {
6✔
1299
                    $this->includePatterns[$code] = [];
6✔
1300
                }
2✔
1301

1302
                if (isset($pattern['type']) === false) {
6✔
1303
                    $pattern['type'] = 'absolute';
6✔
1304
                }
2✔
1305

1306
                $this->includePatterns[$code][(string) $pattern] = (string) $pattern['type'];
6✔
1307
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1308
                    echo str_repeat("\t", $depth);
×
1309
                    echo "\t\t=> added rule-specific ".(string) $pattern['type'].' include pattern';
×
1310
                    if ($code !== $ref) {
×
1311
                        echo " for $code";
×
1312
                    }
1313

1314
                    echo ': '.(string) $pattern.PHP_EOL;
×
1315
                }
1316
            }//end foreach
8✔
1317
        }//end foreach
8✔
1318

1319
    }//end processRule()
15✔
1320

1321

1322
    /**
1323
     * Determine if an element should be processed or ignored.
1324
     *
1325
     * @param \SimpleXMLElement $element An object from a ruleset XML file.
1326
     *
1327
     * @return bool
1328
     */
1329
    private function shouldProcessElement($element)
20✔
1330
    {
1331
        if (isset($element['phpcbf-only']) === false
20✔
1332
            && isset($element['phpcs-only']) === false
20✔
1333
        ) {
7✔
1334
            // No exceptions are being made.
1335
            return true;
20✔
1336
        }
1337

1338
        if (PHP_CODESNIFFER_CBF === true
12✔
1339
            && isset($element['phpcbf-only']) === true
12✔
1340
            && (string) $element['phpcbf-only'] === 'true'
12✔
1341
        ) {
4✔
1342
            return true;
6✔
1343
        }
1344

1345
        if (PHP_CODESNIFFER_CBF === false
12✔
1346
            && isset($element['phpcs-only']) === true
12✔
1347
            && (string) $element['phpcs-only'] === 'true'
12✔
1348
        ) {
4✔
1349
            return true;
6✔
1350
        }
1351

1352
        return false;
12✔
1353

1354
    }//end shouldProcessElement()
1355

1356

1357
    /**
1358
     * Loads and stores sniffs objects used for sniffing files.
1359
     *
1360
     * @param array $files        Paths to the sniff files to register.
1361
     * @param array $restrictions The sniff class names to restrict the allowed
1362
     *                            listeners to.
1363
     * @param array $exclusions   The sniff class names to exclude from the
1364
     *                            listeners list.
1365
     *
1366
     * @return void
1367
     */
1368
    public function registerSniffs($files, $restrictions, $exclusions)
26✔
1369
    {
1370
        $listeners = [];
26✔
1371

1372
        foreach ($files as $file) {
26✔
1373
            // Work out where the position of /StandardName/Sniffs/... is
1374
            // so we can determine what the class will be called.
1375
            $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR);
26✔
1376
            if ($sniffPos === false) {
26✔
1377
                continue;
3✔
1378
            }
1379

1380
            $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR);
26✔
1381
            if ($slashPos === false) {
26✔
1382
                continue;
×
1383
            }
1384

1385
            $className   = Autoload::loadFile($file);
26✔
1386
            $compareName = Common::cleanSniffClass($className);
26✔
1387

1388
            // If they have specified a list of sniffs to restrict to, check
1389
            // to see if this sniff is allowed.
1390
            if (empty($restrictions) === false
26✔
1391
                && isset($restrictions[$compareName]) === false
26✔
1392
            ) {
9✔
1393
                continue;
6✔
1394
            }
1395

1396
            // If they have specified a list of sniffs to exclude, check
1397
            // to see if this sniff is allowed.
1398
            if (empty($exclusions) === false
26✔
1399
                && isset($exclusions[$compareName]) === true
26✔
1400
            ) {
9✔
1401
                continue;
6✔
1402
            }
1403

1404
            // Skip abstract classes.
1405
            $reflection = new ReflectionClass($className);
26✔
1406
            if ($reflection->isAbstract() === true) {
26✔
1407
                continue;
3✔
1408
            }
1409

1410
            $listeners[$className] = $className;
26✔
1411

1412
            if (PHP_CODESNIFFER_VERBOSITY > 2) {
26✔
1413
                echo "Registered $className".PHP_EOL;
×
1414
            }
1415
        }//end foreach
9✔
1416

1417
        $this->sniffs = $listeners;
26✔
1418

1419
    }//end registerSniffs()
17✔
1420

1421

1422
    /**
1423
     * Populates the array of PHP_CodeSniffer_Sniff objects for this file.
1424
     *
1425
     * @return void
1426
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If sniff registration fails.
1427
     */
1428
    public function populateTokenListeners()
11✔
1429
    {
1430
        // Construct a list of listeners indexed by token being listened for.
1431
        $this->tokenListeners = [];
11✔
1432

1433
        foreach ($this->sniffs as $sniffClass => $sniffObject) {
11✔
1434
            $this->sniffs[$sniffClass] = null;
11✔
1435
            $this->sniffs[$sniffClass] = new $sniffClass();
11✔
1436

1437
            $sniffCode = Common::getSniffCode($sniffClass);
11✔
1438
            $this->sniffCodes[$sniffCode] = $sniffClass;
11✔
1439

1440
            if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) {
11✔
1441
                $this->deprecatedSniffs[$sniffCode] = $sniffClass;
3✔
1442
            }
1✔
1443

1444
            // Set custom properties.
1445
            if (isset($this->ruleset[$sniffCode]['properties']) === true) {
11✔
1446
                foreach ($this->ruleset[$sniffCode]['properties'] as $name => $settings) {
11✔
1447
                    $this->setSniffProperty($sniffClass, $name, $settings);
11✔
1448
                }
4✔
1449
            }
4✔
1450

1451
            $tokenizers = [];
11✔
1452
            $vars       = get_class_vars($sniffClass);
11✔
1453
            if (isset($vars['supportedTokenizers']) === true) {
11✔
1454
                foreach ($vars['supportedTokenizers'] as $tokenizer) {
9✔
1455
                    $tokenizers[$tokenizer] = $tokenizer;
9✔
1456
                }
3✔
1457
            } else {
3✔
1458
                $tokenizers = ['PHP' => 'PHP'];
8✔
1459
            }
1460

1461
            $tokens = $this->sniffs[$sniffClass]->register();
11✔
1462
            if (is_array($tokens) === false) {
11✔
1463
                $msg = "The sniff {$sniffClass}::register() method must return an array.";
3✔
1464
                $this->msgCache->add($msg, MsgCollector::ERROR);
3✔
1465

1466
                // Unregister the sniff.
1467
                unset($this->sniffs[$sniffClass], $this->sniffCodes[$sniffCode], $this->deprecatedSniffs[$sniffCode]);
3✔
1468
                continue;
3✔
1469
            }
1470

1471
            $ignorePatterns = [];
11✔
1472
            $patterns       = $this->getIgnorePatterns($sniffCode);
11✔
1473
            foreach ($patterns as $pattern => $type) {
11✔
1474
                $replacements = [
1✔
1475
                    '\\,' => ',',
3✔
1476
                    '*'   => '.*',
2✔
1477
                ];
2✔
1478

1479
                $ignorePatterns[] = strtr($pattern, $replacements);
3✔
1480
            }
4✔
1481

1482
            $includePatterns = [];
11✔
1483
            $patterns        = $this->getIncludePatterns($sniffCode);
11✔
1484
            foreach ($patterns as $pattern => $type) {
11✔
1485
                $replacements = [
1✔
1486
                    '\\,' => ',',
3✔
1487
                    '*'   => '.*',
2✔
1488
                ];
2✔
1489

1490
                $includePatterns[] = strtr($pattern, $replacements);
3✔
1491
            }
4✔
1492

1493
            foreach ($tokens as $token) {
11✔
1494
                if (isset($this->tokenListeners[$token]) === false) {
11✔
1495
                    $this->tokenListeners[$token] = [];
11✔
1496
                }
4✔
1497

1498
                if (isset($this->tokenListeners[$token][$sniffClass]) === false) {
11✔
1499
                    $this->tokenListeners[$token][$sniffClass] = [
11✔
1500
                        'class'      => $sniffClass,
11✔
1501
                        'source'     => $sniffCode,
11✔
1502
                        'tokenizers' => $tokenizers,
11✔
1503
                        'ignore'     => $ignorePatterns,
11✔
1504
                        'include'    => $includePatterns,
11✔
1505
                    ];
4✔
1506
                }
4✔
1507
            }
4✔
1508
        }//end foreach
4✔
1509

1510
    }//end populateTokenListeners()
7✔
1511

1512

1513
    /**
1514
     * Set a single property for a sniff.
1515
     *
1516
     * @param string $sniffClass The class name of the sniff.
1517
     * @param string $name       The name of the property to change.
1518
     * @param array  $settings   Array with the new value of the property and the scope of the property being set.
1519
     *
1520
     * @return void
1521
     *
1522
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When attempting to set a non-existent property on a sniff
1523
     *                                                      which doesn't declare the property or explicitly supports
1524
     *                                                      dynamic properties.
1525
     */
1526
    public function setSniffProperty($sniffClass, $name, $settings)
68✔
1527
    {
1528
        // Setting a property for a sniff we are not using.
1529
        if (isset($this->sniffs[$sniffClass]) === false) {
68✔
1530
            return;
3✔
1531
        }
1532

1533
        $name         = trim($name);
65✔
1534
        $propertyName = $name;
65✔
1535
        if (substr($propertyName, -2) === '[]') {
65✔
1536
            $propertyName = substr($propertyName, 0, -2);
3✔
1537
        }
1✔
1538

1539
        /*
1540
         * BC-compatibility layer for $settings using the pre-PHPCS 3.8.0 format.
1541
         *
1542
         * Prior to PHPCS 3.8.0, `$settings` was expected to only contain the new _value_
1543
         * for the property (which could be an array).
1544
         * Since PHPCS 3.8.0, `$settings` is expected to be an array with two keys: 'scope'
1545
         * and 'value', where 'scope' indicates whether the property should be set to the given 'value'
1546
         * for one individual sniff or for all sniffs in a standard.
1547
         *
1548
         * This BC-layer is only for integrations with PHPCS which may call this method directly
1549
         * and will be removed in PHPCS 4.0.0.
1550
         */
1551

1552
        if (is_array($settings) === false
65✔
1553
            || isset($settings['scope'], $settings['value']) === false
65✔
1554
        ) {
22✔
1555
            // This will be an "old" format value.
1556
            $settings = [
8✔
1557
                'value' => $settings,
24✔
1558
                'scope' => 'standard',
24✔
1559
            ];
16✔
1560

1561
            trigger_error(
24✔
1562
                __FUNCTION__.': the format of the $settings parameter has changed from (mixed) $value to array(\'scope\' => \'sniff|standard\', \'value\' => $value). Please update your integration code. See PR #3629 for more information.',
24✔
1563
                E_USER_DEPRECATED
16✔
1564
            );
16✔
1565
        }
7✔
1566

1567
        $isSettable  = false;
65✔
1568
        $sniffObject = $this->sniffs[$sniffClass];
65✔
1569
        if (property_exists($sniffObject, $propertyName) === true
65✔
1570
            || ($sniffObject instanceof stdClass) === true
38✔
1571
            || method_exists($sniffObject, '__set') === true
51✔
1572
        ) {
22✔
1573
            $isSettable = true;
53✔
1574
        }
18✔
1575

1576
        if ($isSettable === false) {
65✔
1577
            if ($settings['scope'] === 'sniff') {
18✔
1578
                $notice  = "Property \"$propertyName\" does not exist on sniff ";
6✔
1579
                $notice .= array_search($sniffClass, $this->sniffCodes, true).'.';
6✔
1580
                $this->msgCache->add($notice, MsgCollector::ERROR);
6✔
1581
            }
2✔
1582

1583
            return;
18✔
1584
        }
1585

1586
        $value = $settings['value'];
53✔
1587

1588
        if (is_string($value) === true) {
53✔
1589
            $value = trim($value);
53✔
1590
        }
18✔
1591

1592
        if ($value === '') {
53✔
1593
            $value = null;
6✔
1594
        }
2✔
1595

1596
        // Special case for booleans.
1597
        if ($value === 'true') {
53✔
1598
            $value = true;
9✔
1599
        } else if ($value === 'false') {
53✔
1600
            $value = false;
9✔
1601
        } else if (substr($name, -2) === '[]') {
53✔
1602
            $name   = $propertyName;
3✔
1603
            $values = [];
3✔
1604
            if ($value !== null) {
3✔
1605
                foreach (explode(',', $value) as $val) {
3✔
1606
                    list($k, $v) = explode('=>', $val.'=>');
3✔
1607
                    if ($v !== '') {
3✔
1608
                        $values[trim($k)] = trim($v);
3✔
1609
                    } else {
1✔
1610
                        $values[] = trim($k);
3✔
1611
                    }
1612
                }
1✔
1613
            }
1✔
1614

1615
            $value = $values;
3✔
1616
        }
1✔
1617

1618
        $sniffObject->$name = $value;
53✔
1619

1620
    }//end setSniffProperty()
35✔
1621

1622

1623
    /**
1624
     * Gets the array of ignore patterns.
1625
     *
1626
     * Optionally takes a listener to get ignore patterns specified
1627
     * for that sniff only.
1628
     *
1629
     * @param string $listener The listener to get patterns for. If NULL, all
1630
     *                         patterns are returned.
1631
     *
1632
     * @return array
1633
     */
1634
    public function getIgnorePatterns($listener=null)
20✔
1635
    {
1636
        if ($listener === null) {
20✔
1637
            return $this->ignorePatterns;
3✔
1638
        }
1639

1640
        if (isset($this->ignorePatterns[$listener]) === true) {
17✔
1641
            return $this->ignorePatterns[$listener];
6✔
1642
        }
1643

1644
        return [];
11✔
1645

1646
    }//end getIgnorePatterns()
1647

1648

1649
    /**
1650
     * Gets the array of include patterns.
1651
     *
1652
     * Optionally takes a listener to get include patterns specified
1653
     * for that sniff only.
1654
     *
1655
     * @param string $listener The listener to get patterns for. If NULL, all
1656
     *                         patterns are returned.
1657
     *
1658
     * @return array
1659
     */
1660
    public function getIncludePatterns($listener=null)
20✔
1661
    {
1662
        if ($listener === null) {
20✔
1663
            return $this->includePatterns;
3✔
1664
        }
1665

1666
        if (isset($this->includePatterns[$listener]) === true) {
17✔
1667
            return $this->includePatterns[$listener];
6✔
1668
        }
1669

1670
        return [];
11✔
1671

1672
    }//end getIncludePatterns()
1673

1674

1675
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc