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

PHPCSStandards / PHP_CodeSniffer / 14516416464

17 Apr 2025 01:09PM UTC coverage: 77.945% (+0.3%) from 77.666%
14516416464

push

github

web-flow
Merge pull request #1010 from PHPCSStandards/phpcs-4.0/feature/sq-1612-stdout-vs-stderr

(Nearly) All status, debug, and progress output is now sent to STDERR instead of STDOUT

63 of 457 new or added lines in 18 files covered. (13.79%)

1 existing line in 1 file now uncovered.

19455 of 24960 relevant lines covered (77.94%)

78.64 hits per line

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

88.83
/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 InvalidArgumentException;
15
use PHP_CodeSniffer\Exceptions\RuntimeException;
16
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
17
use PHP_CodeSniffer\Util\Common;
18
use PHP_CodeSniffer\Util\MessageCollector;
19
use PHP_CodeSniffer\Util\Standards;
20
use PHP_CodeSniffer\Util\Writers\StatusWriter;
21
use RecursiveDirectoryIterator;
22
use RecursiveIteratorIterator;
23
use ReflectionClass;
24
use stdClass;
25

26
class Ruleset
27
{
28

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

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

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

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

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

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

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

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

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

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

127
    /**
128
     * The `<config>` directives which have been applied.
129
     *
130
     * @var array<string, int> Key is the name of the config. Value is the ruleset depth
131
     *                         at which this config was applied (if not overruled from the CLI).
132
     */
133
    private $configDirectivesApplied = [];
134

135
    /**
136
     * The `<arg>` directives which have been applied.
137
     *
138
     * @var array<string, int> Key is the name of the setting. Value is the ruleset depth
139
     *                         at which this setting was applied (if not overruled from the CLI).
140
     */
141
    private $cliSettingsApplied = [];
142

143
    /**
144
     * An array of the names of sniffs which have been marked as deprecated.
145
     *
146
     * The key is the sniff code and the value
147
     * is the fully qualified name of the sniff class.
148
     *
149
     * @var array<string, string>
150
     */
151
    private $deprecatedSniffs = [];
152

153
    /**
154
     * Message collector object.
155
     *
156
     * User-facing messages should be collected via this object for display once the ruleset processing has finished.
157
     *
158
     * The following type of errors should *NOT* be collected, but should still throw their own `RuntimeException`:
159
     * - Errors which could cause other (uncollectable) errors further into the ruleset processing, like a missing autoload file.
160
     * - Errors which are directly aimed at and only intended for sniff developers or integrators
161
     *   (in contrast to ruleset maintainers or end-users).
162
     *
163
     * @var \PHP_CodeSniffer\Util\MessageCollector
164
     */
165
    private $msgCache;
166

167

168
    /**
169
     * Initialise the ruleset that the run will use.
170
     *
171
     * @param \PHP_CodeSniffer\Config $config The config data for the run.
172
     *
173
     * @return void
174
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If blocking errors were encountered when processing the ruleset.
175
     */
176
    public function __construct(Config $config)
44✔
177
    {
178
        $this->config   = $config;
44✔
179
        $restrictions   = $config->sniffs;
44✔
180
        $exclusions     = $config->exclude;
44✔
181
        $sniffs         = [];
44✔
182
        $this->msgCache = new MessageCollector();
44✔
183

184
        $standardPaths = [];
44✔
185
        foreach ($config->standards as $standard) {
44✔
186
            $installed = Standards::getInstalledStandardPath($standard);
44✔
187
            if ($installed === null) {
44✔
188
                $standard = Common::realpath($standard);
14✔
189
                if (is_dir($standard) === true
14✔
190
                    && is_file(Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true
14✔
191
                ) {
192
                    $standard = Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml');
10✔
193
                }
194
            } else {
195
                $standard = $installed;
30✔
196
            }
197

198
            $standardPaths[] = $standard;
44✔
199
        }
200

201
        foreach ($standardPaths as $standard) {
44✔
202
            $ruleset = @simplexml_load_string(file_get_contents($standard));
44✔
203
            if ($ruleset !== false) {
44✔
204
                $standardName = (string) $ruleset['name'];
44✔
205
                if ($this->name !== '') {
44✔
206
                    $this->name .= ', ';
3✔
207
                }
208

209
                $this->name .= $standardName;
44✔
210

211
                // Allow autoloading of custom files inside this standard.
212
                if (isset($ruleset['namespace']) === true) {
44✔
213
                    $namespace = (string) $ruleset['namespace'];
6✔
214
                } else {
215
                    $namespace = basename(dirname($standard));
38✔
216
                }
217

218
                Autoload::addSearchPath(dirname($standard), $namespace);
44✔
219
            }
220

221
            if (PHP_CODESNIFFER_VERBOSITY === 1) {
44✔
NEW
222
                $newlines = 0;
×
223
                if (count($config->standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
×
NEW
224
                    $newlines = 1;
×
225
                }
226

NEW
227
                StatusWriter::write("Registering sniffs in the $standardName standard... ", 0, $newlines);
×
228
            }
229

230
            $sniffs = array_merge($sniffs, $this->processRuleset($standard));
44✔
231
        }//end foreach
232

233
        // Ignore sniff restrictions if caching is on.
234
        if ($config->cache === true) {
44✔
235
            $restrictions = [];
6✔
236
            $exclusions   = [];
6✔
237
        }
238

239
        $sniffRestrictions = [];
44✔
240
        foreach ($restrictions as $sniffCode) {
44✔
241
            $parts     = explode('.', strtolower($sniffCode));
9✔
242
            $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
9✔
243
            $sniffRestrictions[$sniffName] = true;
9✔
244
        }
245

246
        $sniffExclusions = [];
44✔
247
        foreach ($exclusions as $sniffCode) {
44✔
248
            $parts     = explode('.', strtolower($sniffCode));
3✔
249
            $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
3✔
250
            $sniffExclusions[$sniffName] = true;
3✔
251
        }
252

253
        $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions);
44✔
254
        $this->populateTokenListeners();
44✔
255

256
        $numSniffs = count($this->sniffs);
44✔
257
        if (PHP_CODESNIFFER_VERBOSITY === 1) {
44✔
NEW
258
            StatusWriter::write("DONE ($numSniffs sniffs registered)");
×
259
        }
260

261
        if ($numSniffs === 0) {
44✔
262
            $this->msgCache->add('No sniffs were registered.', MessageCollector::ERROR);
3✔
263
        }
264

265
        $this->displayCachedMessages();
44✔
266

267
    }//end __construct()
14✔
268

269

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

280
        $sniffCount = count($sniffs);
15✔
281

282
        // Add a dummy entry to the end so we loop one last time
283
        // and echo out the collected info about the last standard.
284
        $sniffs[] = '';
15✔
285

286
        $summaryLine = PHP_EOL."The $this->name standard contains 1 sniff".PHP_EOL;
15✔
287
        if ($sniffCount !== 1) {
15✔
288
            $summaryLine = str_replace('1 sniff', "$sniffCount sniffs", $summaryLine);
12✔
289
        }
290

291
        echo $summaryLine;
15✔
292

293
        $lastStandard     = null;
15✔
294
        $lastCount        = 0;
15✔
295
        $sniffsInStandard = [];
15✔
296

297
        foreach ($sniffs as $i => $sniff) {
15✔
298
            if ($i === $sniffCount) {
15✔
299
                $currentStandard = null;
15✔
300
            } else {
301
                $currentStandard = substr($sniff, 0, strpos($sniff, '.'));
15✔
302
                if ($lastStandard === null) {
15✔
303
                    $lastStandard = $currentStandard;
15✔
304
                }
305
            }
306

307
            // Reached the first item in the next standard.
308
            // Echo out the info collected from the previous standard.
309
            if ($currentStandard !== $lastStandard) {
15✔
310
                $subTitle = $lastStandard.' ('.$lastCount.' sniff';
15✔
311
                if ($lastCount > 1) {
15✔
312
                    $subTitle .= 's';
12✔
313
                }
314

315
                $subTitle .= ')';
15✔
316

317
                echo PHP_EOL.$subTitle.PHP_EOL;
15✔
318
                echo str_repeat('-', strlen($subTitle)).PHP_EOL;
15✔
319
                echo '  '.implode(PHP_EOL.'  ', $sniffsInStandard).PHP_EOL;
15✔
320

321
                $lastStandard     = $currentStandard;
15✔
322
                $lastCount        = 0;
15✔
323
                $sniffsInStandard = [];
15✔
324

325
                if ($currentStandard === null) {
15✔
326
                    break;
15✔
327
                }
328
            }//end if
329

330
            if (isset($this->deprecatedSniffs[$sniff]) === true) {
15✔
331
                $sniff .= ' *';
3✔
332
            }
333

334
            $sniffsInStandard[] = $sniff;
15✔
335
            ++$lastCount;
15✔
336
        }//end foreach
337

338
        if (count($this->deprecatedSniffs) > 0) {
15✔
339
            echo PHP_EOL.'* Sniffs marked with an asterix are deprecated.'.PHP_EOL;
3✔
340
        }
341

342
    }//end explain()
5✔
343

344

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

354
    }//end hasSniffDeprecations()
355

356

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

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

382
        $reportWidth = $this->config->reportWidth;
33✔
383
        // Message takes report width minus the leading dash + two spaces, minus a one space gutter at the end.
384
        $maxMessageWidth = ($reportWidth - 4);
33✔
385
        $maxActualWidth  = 0;
33✔
386

387
        ksort($this->deprecatedSniffs, (SORT_NATURAL | SORT_FLAG_CASE));
33✔
388

389
        $messages        = [];
33✔
390
        $messageTemplate = 'This sniff has been deprecated since %s and will be removed in %s. %s';
33✔
391
        $errorTemplate   = 'ERROR: The %s::%s() method must return a %sstring, received %s';
33✔
392

393
        foreach ($this->deprecatedSniffs as $sniffCode => $className) {
33✔
394
            if (isset($this->sniffs[$className]) === false) {
33✔
395
                // Should only be possible in test situations, but some extra defensive coding is never a bad thing.
396
                continue;
×
397
            }
398

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

408
            if ($deprecatedSince === '') {
30✔
409
                throw new RuntimeException(
3✔
410
                    sprintf($errorTemplate, $className, 'getDeprecationVersion', 'non-empty ', '""')
3✔
411
                );
2✔
412
            }
413

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

421
            if ($removedIn === '') {
24✔
422
                throw new RuntimeException(
3✔
423
                    sprintf($errorTemplate, $className, 'getRemovalVersion', 'non-empty ', '""')
3✔
424
                );
2✔
425
            }
426

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

434
            // Truncate the error code if there is not enough report width.
435
            if (strlen($sniffCode) > $maxMessageWidth) {
18✔
436
                $sniffCode = substr($sniffCode, 0, ($maxMessageWidth - 3)).'...';
3✔
437
            }
438

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

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

445
            $notice         = trim(sprintf($messageTemplate, $deprecatedSince, $removedIn, $customMessage));
18✔
446
            $maxActualWidth = max($maxActualWidth, min(strlen($notice), $maxMessageWidth));
18✔
447
            $wrapped        = wordwrap($notice, $maxMessageWidth, PHP_EOL);
18✔
448
            $message       .= '   '.implode(PHP_EOL.'   ', explode(PHP_EOL, $wrapped));
18✔
449

450
            $messages[] = $message;
18✔
451
        }//end foreach
452

453
        if (count($messages) === 0) {
18✔
454
            return;
×
455
        }
456

457
        $summaryLine = "WARNING: The $this->name standard uses 1 deprecated sniff";
18✔
458
        $sniffCount  = count($messages);
18✔
459
        if ($sniffCount !== 1) {
18✔
460
            $summaryLine = str_replace('1 deprecated sniff', "$sniffCount deprecated sniffs", $summaryLine);
6✔
461
        }
462

463
        $maxActualWidth = max($maxActualWidth, min(strlen($summaryLine), $maxMessageWidth));
18✔
464

465
        $summaryLine = wordwrap($summaryLine, $reportWidth, PHP_EOL);
18✔
466
        if ($this->config->colors === true) {
18✔
NEW
467
            StatusWriter::write("\033[33m".$summaryLine."\033[0m");
×
468
        } else {
469
            StatusWriter::write($summaryLine);
18✔
470
        }
471

472
        $messages = implode(PHP_EOL, $messages);
18✔
473
        if ($this->config->colors === false) {
18✔
474
            $messages = Common::stripColors($messages);
18✔
475
        }
476

477
        StatusWriter::write(str_repeat('-', min(($maxActualWidth + 4), $reportWidth)));
18✔
478
        StatusWriter::write($messages, 0, 0);
18✔
479

480
        $closer = wordwrap('Deprecated sniffs are still run, but will stop working at some point in the future.', $reportWidth, PHP_EOL);
18✔
481
        StatusWriter::writeNewline(2);
18✔
482
        StatusWriter::write($closer, 0, 2);
18✔
483

484
    }//end showSniffDeprecations()
6✔
485

486

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

511
        $this->msgCache->display();
41✔
512

513
    }//end displayCachedMessages()
8✔
514

515

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

537
        libxml_use_internal_errors(true);
59✔
538
        $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
59✔
539
        if ($ruleset === false) {
59✔
540
            $errorMsg = "ERROR: Ruleset $rulesetPath is not valid".PHP_EOL;
15✔
541
            $errors   = libxml_get_errors();
15✔
542
            foreach ($errors as $error) {
15✔
543
                $errorMsg .= '- On line '.$error->line.', column '.$error->column.': '.$error->message;
10✔
544
            }
545

546
            libxml_clear_errors();
15✔
547
            throw new RuntimeException($errorMsg);
15✔
548
        }
549

550
        libxml_use_internal_errors(false);
44✔
551

552
        $ownSniffs      = [];
44✔
553
        $includedSniffs = [];
44✔
554
        $excludedSniffs = [];
44✔
555

556
        $this->paths[]       = $rulesetPath;
44✔
557
        $rulesetDir          = dirname($rulesetPath);
44✔
558
        $this->rulesetDirs[] = $rulesetDir;
44✔
559

560
        $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs';
44✔
561
        if (is_dir($sniffDir) === true) {
44✔
562
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
NEW
563
                StatusWriter::write('Adding sniff files from '.Common::stripBasepath($sniffDir, $this->config->basepath).' directory', ($depth + 1));
×
564
            }
565

566
            $ownSniffs = $this->expandSniffDirectory($sniffDir, $depth);
9✔
567
        }
568

569
        // Include custom autoloaders.
570
        foreach ($ruleset->{'autoload'} as $autoload) {
44✔
571
            if ($this->shouldProcessElement($autoload) === false) {
9✔
572
                continue;
6✔
573
            }
574

575
            $autoloadPath = (string) $autoload;
9✔
576

577
            // Try relative autoload paths first.
578
            $relativePath = Common::realpath(dirname($rulesetPath).DIRECTORY_SEPARATOR.$autoloadPath);
9✔
579

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

586
            include_once $autoloadPath;
6✔
587

588
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
589
                StatusWriter::write("=> included autoloader $autoloadPath", ($depth + 1));
×
590
            }
591
        }//end foreach
592

593
        // Process custom sniff config settings.
594
        // Processing rules:
595
        // - Highest level ruleset take precedence.
596
        // - If the same config is being set from two rulesets at the same level, *last* one "wins".
597
        foreach ($ruleset->{'config'} as $config) {
41✔
598
            if ($this->shouldProcessElement($config) === false) {
15✔
599
                continue;
6✔
600
            }
601

602
            $name = (string) $config['name'];
15✔
603

604
            if (isset($this->configDirectivesApplied[$name]) === true
15✔
605
                && $this->configDirectivesApplied[$name] < $depth
15✔
606
            ) {
607
                // Ignore this config. A higher level ruleset has already set a value for this directive.
608
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
609
                    StatusWriter::write('=> ignoring config value '.$name.': '.(string) $config['value'].' => already changed by a higher level ruleset ', ($depth + 1));
×
610
                }
611

612
                continue;
3✔
613
            }
614

615
            $this->configDirectivesApplied[$name] = $depth;
15✔
616

617
            $applied = $this->config->setConfigData($name, (string) $config['value'], true);
15✔
618
            if ($applied === true && PHP_CODESNIFFER_VERBOSITY > 1) {
15✔
NEW
619
                StatusWriter::write('=> set config value '.$name.': '.(string) $config['value'], ($depth + 1));
×
620
            }
621
        }//end foreach
622

623
        // Process custom command line arguments.
624
        // Processing rules:
625
        // - Highest level ruleset take precedence.
626
        // - If the same CLI flag is being set from two rulesets at the same level, *first* one "wins".
627
        $cliArgs = [];
41✔
628
        foreach ($ruleset->{'arg'} as $arg) {
41✔
629
            if ($this->shouldProcessElement($arg) === false) {
12✔
630
                continue;
6✔
631
            }
632

633
            // "Long" CLI argument. Arg is in the format `<arg name="name" [value="value"]/>`.
634
            if (isset($arg['name']) === true) {
12✔
635
                $name           = (string) $arg['name'];
12✔
636
                $cliSettingName = $name;
12✔
637
                if (isset($this->config::CLI_FLAGS_TO_SETTING_NAME[$name]) === true) {
12✔
638
                    $cliSettingName = $this->config::CLI_FLAGS_TO_SETTING_NAME[$name];
3✔
639
                }
640

641
                if (isset($this->cliSettingsApplied[$cliSettingName]) === true
12✔
642
                    && $this->cliSettingsApplied[$cliSettingName] < $depth
12✔
643
                ) {
644
                    // Ignore this CLI flag. A higher level ruleset has already set a value for this setting.
645
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
646
                        $statusMessage = '=> ignoring command line arg --'.$name;
×
647
                        if (isset($arg['value']) === true) {
×
648
                            $statusMessage .= '='.(string) $arg['value'];
×
649
                        }
650

NEW
651
                        StatusWriter::write($statusMessage.' => already changed by a higher level ruleset ', ($depth + 1));
×
652
                    }
653

654
                    continue;
3✔
655
                }
656

657
                // Remember which settings we've seen.
658
                $this->cliSettingsApplied[$cliSettingName] = $depth;
12✔
659

660
                $argString = '--'.$name;
12✔
661
                if (isset($arg['value']) === true) {
12✔
662
                    $argString .= '='.(string) $arg['value'];
12✔
663
                }
664
            } else {
665
                // "Short" CLI flag. Arg is in the format `<arg value="value"/>` and
666
                // value can contain multiple flags, like `<arg value="ps"/>`.
667
                $cleanedValue    = '';
9✔
668
                $cliFlagsAsArray = str_split((string) $arg['value']);
9✔
669
                foreach ($cliFlagsAsArray as $flag) {
9✔
670
                    $cliSettingName = $flag;
9✔
671
                    if (isset($this->config::CLI_FLAGS_TO_SETTING_NAME[$flag]) === true) {
9✔
672
                        $cliSettingName = $this->config::CLI_FLAGS_TO_SETTING_NAME[$flag];
3✔
673
                    }
674

675
                    if (isset($this->cliSettingsApplied[$cliSettingName]) === true
9✔
676
                        && $this->cliSettingsApplied[$cliSettingName] < $depth
9✔
677
                    ) {
678
                        // Ignore this CLI flag. A higher level ruleset has already set a value for this setting.
679
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
680
                            StatusWriter::write('=> ignoring command line flag -'.$flag.' => already changed by a higher level ruleset ', ($depth + 1));
×
681
                        }
682

683
                        continue;
3✔
684
                    }
685

686
                    // Remember which settings we've seen.
687
                    $cleanedValue .= $flag;
9✔
688
                    $this->cliSettingsApplied[$cliSettingName] = $depth;
9✔
689
                }//end foreach
690

691
                if ($cleanedValue === '') {
9✔
692
                    // No flags found which should be applied.
693
                    continue;
×
694
                }
695

696
                $argString = '-'.$cleanedValue;
9✔
697
            }//end if
698

699
            $cliArgs[] = $argString;
12✔
700

701
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
NEW
702
                StatusWriter::write("=> set command line value $argString", ($depth + 1));
×
703
            }
704
        }//end foreach
705

706
        foreach ($ruleset->rule as $rule) {
41✔
707
            if (isset($rule['ref']) === false
41✔
708
                || $this->shouldProcessElement($rule) === false
41✔
709
            ) {
710
                continue;
9✔
711
            }
712

713
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
41✔
NEW
714
                StatusWriter::write('Processing rule "'.$rule['ref'].'"', ($depth + 1));
×
715
            }
716

717
            $expandedSniffs = $this->expandRulesetReference((string) $rule['ref'], $rulesetDir, $depth);
41✔
718
            $newSniffs      = array_diff($expandedSniffs, $includedSniffs);
41✔
719
            $includedSniffs = array_merge($includedSniffs, $expandedSniffs);
41✔
720

721
            $parts = explode('.', $rule['ref']);
41✔
722
            if (count($parts) === 4
41✔
723
                && $parts[0] !== ''
41✔
724
                && $parts[1] !== ''
41✔
725
                && $parts[2] !== ''
41✔
726
            ) {
727
                $sniffCode = $parts[0].'.'.$parts[1].'.'.$parts[2];
9✔
728
                if (isset($this->ruleset[$sniffCode]['severity']) === true
9✔
729
                    && $this->ruleset[$sniffCode]['severity'] === 0
9✔
730
                ) {
731
                    // This sniff code has already been turned off, but now
732
                    // it is being explicitly included again, so turn it back on.
733
                    $this->ruleset[(string) $rule['ref']]['severity'] = 5;
6✔
734
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
735
                        StatusWriter::write('* disabling sniff exclusion for specific message code *', ($depth + 2));
×
736
                        StatusWriter::write('=> severity set to 5', ($depth + 2));
4✔
737
                    }
738
                } else if (empty($newSniffs) === false) {
9✔
739
                    $newSniff = $newSniffs[0];
6✔
740
                    if (in_array($newSniff, $ownSniffs, true) === false) {
6✔
741
                        // Including a sniff that hasn't been included higher up, but
742
                        // only including a single message from it. So turn off all messages in
743
                        // the sniff, except this one.
744
                        $this->ruleset[$sniffCode]['severity']            = 0;
6✔
745
                        $this->ruleset[(string) $rule['ref']]['severity'] = 5;
6✔
746
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
747
                            StatusWriter::write('Excluding sniff "'.$sniffCode.'" except for "'.$parts[3].'"', ($depth + 2));
×
748
                        }
749
                    }
750
                }//end if
751
            }//end if
752

753
            if (isset($rule->exclude) === true) {
41✔
754
                foreach ($rule->exclude as $exclude) {
15✔
755
                    if (isset($exclude['name']) === false) {
15✔
756
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
757
                            StatusWriter::write('* ignoring empty exclude rule *', ($depth + 2));
×
NEW
758
                            StatusWriter::write('=> '.$exclude->asXML(), ($depth + 3));
×
759
                        }
760

761
                        continue;
3✔
762
                    }
763

764
                    if ($this->shouldProcessElement($exclude) === false) {
12✔
765
                        continue;
6✔
766
                    }
767

768
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
NEW
769
                        StatusWriter::write('Excluding rule "'.$exclude['name'].'"', ($depth + 2));
×
770
                    }
771

772
                    // Check if a single code is being excluded, which is a shortcut
773
                    // for setting the severity of the message to 0.
774
                    $parts = explode('.', $exclude['name']);
12✔
775
                    if (count($parts) === 4) {
12✔
776
                        $this->ruleset[(string) $exclude['name']]['severity'] = 0;
6✔
777
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
778
                            StatusWriter::write('=> severity set to 0', ($depth + 2));
4✔
779
                        }
780
                    } else {
781
                        $excludedSniffs = array_merge(
6✔
782
                            $excludedSniffs,
6✔
783
                            $this->expandRulesetReference((string) $exclude['name'], $rulesetDir, ($depth + 1))
6✔
784
                        );
4✔
785
                    }
786
                }//end foreach
787
            }//end if
788

789
            $this->processRule($rule, $newSniffs, $depth);
41✔
790
        }//end foreach
791

792
        // Set custom php ini values as CLI args.
793
        foreach ($ruleset->{'ini'} as $arg) {
41✔
794
            if ($this->shouldProcessElement($arg) === false) {
12✔
795
                continue;
6✔
796
            }
797

798
            if (isset($arg['name']) === false) {
12✔
799
                continue;
3✔
800
            }
801

802
            $name      = (string) $arg['name'];
12✔
803
            $argString = $name;
12✔
804
            if (isset($arg['value']) === true) {
12✔
805
                $value      = (string) $arg['value'];
9✔
806
                $argString .= "=$value";
9✔
807
            } else {
808
                $value = 'true';
3✔
809
            }
810

811
            $cliArgs[] = '-d';
12✔
812
            $cliArgs[] = $argString;
12✔
813

814
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
NEW
815
                StatusWriter::write("=> set PHP ini value $name to $value", ($depth + 1));
×
816
            }
817
        }//end foreach
818

819
        if (empty($this->config->files) === true) {
41✔
820
            // Process hard-coded file paths.
821
            foreach ($ruleset->{'file'} as $file) {
41✔
822
                $file      = (string) $file;
12✔
823
                $cliArgs[] = $file;
12✔
824
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
NEW
825
                    StatusWriter::write("=> added \"$file\" to the file list", ($depth + 1));
×
826
                }
827
            }
828
        }
829

830
        if (empty($cliArgs) === false) {
41✔
831
            // Change the directory so all relative paths are worked
832
            // out based on the location of the ruleset instead of
833
            // the location of the user.
834
            $inPhar = Common::isPharFile($rulesetDir);
24✔
835
            if ($inPhar === false) {
24✔
836
                $currentDir = getcwd();
24✔
837
                chdir($rulesetDir);
24✔
838
            }
839

840
            $this->config->setCommandLineValues($cliArgs);
24✔
841

842
            if ($inPhar === false) {
24✔
843
                chdir($currentDir);
24✔
844
            }
845
        }
846

847
        // Process custom ignore pattern rules.
848
        foreach ($ruleset->{'exclude-pattern'} as $pattern) {
41✔
849
            if ($this->shouldProcessElement($pattern) === false) {
6✔
850
                continue;
6✔
851
            }
852

853
            if (isset($pattern['type']) === false) {
6✔
854
                $pattern['type'] = 'absolute';
6✔
855
            }
856

857
            $this->ignorePatterns[(string) $pattern] = (string) $pattern['type'];
6✔
858
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
859
                StatusWriter::write('=> added global '.(string) $pattern['type'].' ignore pattern: '.(string) $pattern, ($depth + 1));
×
860
            }
861
        }
862

863
        $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs));
41✔
864
        $excludedSniffs = array_unique($excludedSniffs);
41✔
865

866
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
41✔
867
            $included = count($includedSniffs);
×
868
            $excluded = count($excludedSniffs);
×
NEW
869
            StatusWriter::write("=> Ruleset processing complete; included $included sniffs and excluded $excluded", $depth);
×
870
        }
871

872
        // Merge our own sniff list with our externally included
873
        // sniff list, but filter out any excluded sniffs.
874
        $files = [];
41✔
875
        foreach ($includedSniffs as $sniff) {
41✔
876
            if (in_array($sniff, $excludedSniffs, true) === true) {
41✔
877
                continue;
6✔
878
            } else {
879
                $files[] = Common::realpath($sniff);
41✔
880
            }
881
        }
882

883
        return $files;
41✔
884

885
    }//end processRuleset()
886

887

888
    /**
889
     * Expands a directory into a list of sniff files within.
890
     *
891
     * @param string $directory The path to a directory.
892
     * @param int    $depth     How many nested processing steps we are in. This
893
     *                          is only used for debug output.
894
     *
895
     * @return array
896
     */
897
    private function expandSniffDirectory($directory, $depth=0)
6✔
898
    {
899
        $sniffs = [];
6✔
900

901
        $rdi = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
6✔
902
        $di  = new RecursiveIteratorIterator($rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD);
6✔
903

904
        $dirLen = strlen($directory);
6✔
905

906
        foreach ($di as $file) {
6✔
907
            $filename = $file->getFilename();
6✔
908

909
            // Skip hidden files.
910
            if (substr($filename, 0, 1) === '.') {
6✔
911
                continue;
6✔
912
            }
913

914
            // We are only interested in PHP and sniff files.
915
            $fileParts = explode('.', $filename);
6✔
916
            if (array_pop($fileParts) !== 'php') {
6✔
917
                continue;
3✔
918
            }
919

920
            $basename = basename($filename, '.php');
6✔
921
            if (substr($basename, -5) !== 'Sniff') {
6✔
922
                continue;
3✔
923
            }
924

925
            $path = $file->getPathname();
6✔
926

927
            // Skip files in hidden directories within the Sniffs directory of this
928
            // standard. We use the offset with strpos() to allow hidden directories
929
            // before, valid example:
930
            // /home/foo/.composer/vendor/squiz/custom_tool/MyStandard/Sniffs/...
931
            if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) {
6✔
932
                continue;
3✔
933
            }
934

935
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
936
                StatusWriter::write('=> '.Common::stripBasepath($path, $this->config->basepath), ($depth + 2));
×
937
            }
938

939
            $sniffs[] = $path;
6✔
940
        }//end foreach
941

942
        return $sniffs;
6✔
943

944
    }//end expandSniffDirectory()
945

946

947
    /**
948
     * Expands a ruleset reference into a list of sniff files.
949
     *
950
     * @param string $ref        The reference from the ruleset XML file.
951
     * @param string $rulesetDir The directory of the ruleset XML file, used to
952
     *                           evaluate relative paths.
953
     * @param int    $depth      How many nested processing steps we are in. This
954
     *                           is only used for debug output.
955
     *
956
     * @return array
957
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the reference is invalid.
958
     */
959
    private function expandRulesetReference($ref, $rulesetDir, $depth=0)
53✔
960
    {
961
        // Naming an (external) standard "Internal" is not supported.
962
        if (strtolower($ref) === 'internal') {
53✔
963
            $message  = 'The name "Internal" is reserved for internal use. A PHP_CodeSniffer standard should not be called "Internal".'.PHP_EOL;
3✔
964
            $message .= 'Contact the maintainer of the standard to fix this.';
3✔
965
            $this->msgCache->add($message, MessageCollector::ERROR);
3✔
966

967
            return [];
3✔
968
        }
969

970
        // Ignore internal sniffs codes as they are used to only
971
        // hide and change internal messages.
972
        if (substr($ref, 0, 9) === 'Internal.') {
53✔
973
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
974
                StatusWriter::write('* ignoring internal sniff code *', ($depth + 2));
×
975
            }
976

977
            return [];
3✔
978
        }
979

980
        // As sniffs can't begin with a full stop, assume references in
981
        // this format are relative paths and attempt to convert them
982
        // to absolute paths. If this fails, let the reference run through
983
        // the normal checks and have it fail as normal.
984
        if (substr($ref, 0, 1) === '.') {
53✔
985
            $realpath = Common::realpath($rulesetDir.'/'.$ref);
12✔
986
            if ($realpath !== false) {
12✔
987
                $ref = $realpath;
6✔
988
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
989
                    StatusWriter::write('=> '.Common::stripBasepath($ref, $this->config->basepath), ($depth + 2));
×
990
                }
991
            }
992
        }
993

994
        // As sniffs can't begin with a tilde, assume references in
995
        // this format are relative to the user's home directory.
996
        if (substr($ref, 0, 2) === '~/') {
53✔
997
            $realpath = Common::realpath($ref);
9✔
998
            if ($realpath !== false) {
9✔
999
                $ref = $realpath;
3✔
1000
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
1001
                    StatusWriter::write('=> '.Common::stripBasepath($ref, $this->config->basepath), ($depth + 2));
×
1002
                }
1003
            }
1004
        }
1005

1006
        if (is_file($ref) === true) {
53✔
1007
            if (substr($ref, -9) === 'Sniff.php') {
11✔
1008
                // A single external sniff.
1009
                $this->rulesetDirs[] = dirname(dirname(dirname($ref)));
11✔
1010
                return [$ref];
11✔
1011
            }
1012
        } else {
1013
            // See if this is a whole standard being referenced.
1014
            $path = Standards::getInstalledStandardPath($ref);
48✔
1015
            if ($path !== null && Common::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) {
48✔
1016
                // If the ruleset exists inside the phar file, use it.
1017
                if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
×
1018
                    $path .= DIRECTORY_SEPARATOR.'ruleset.xml';
×
1019
                } else {
1020
                    $path = null;
×
1021
                }
1022
            }
1023

1024
            if ($path !== null) {
48✔
1025
                $ref = $path;
3✔
1026
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
1027
                    StatusWriter::write('=> '.Common::stripBasepath($ref, $this->config->basepath), ($depth + 2));
2✔
1028
                }
1029
            } else if (is_dir($ref) === false) {
48✔
1030
                // Work out the sniff path.
1031
                $sepPos = strpos($ref, DIRECTORY_SEPARATOR);
42✔
1032
                if ($sepPos !== false) {
42✔
1033
                    $stdName = substr($ref, 0, $sepPos);
9✔
1034
                    $path    = substr($ref, $sepPos);
9✔
1035
                } else {
1036
                    $parts   = explode('.', $ref);
33✔
1037
                    $stdName = $parts[0];
33✔
1038
                    if (count($parts) === 1) {
33✔
1039
                        // A whole standard?
1040
                        $path = '';
3✔
1041
                    } else if (count($parts) === 2) {
30✔
1042
                        // A directory of sniffs?
1043
                        $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1];
6✔
1044
                    } else {
1045
                        // A single sniff?
1046
                        $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php';
27✔
1047
                    }
1048
                }
1049

1050
                $newRef  = false;
42✔
1051
                $stdPath = Standards::getInstalledStandardPath($stdName);
42✔
1052
                if ($stdPath !== null && $path !== '') {
42✔
1053
                    if (Common::isPharFile($stdPath) === true
18✔
1054
                        && strpos($stdPath, 'ruleset.xml') === false
18✔
1055
                    ) {
1056
                        // Phar files can only return the directory,
1057
                        // since ruleset can be omitted if building one standard.
1058
                        $newRef = Common::realpath($stdPath.$path);
×
1059
                    } else {
1060
                        $newRef = Common::realpath(dirname($stdPath).$path);
18✔
1061
                    }
1062
                }
1063

1064
                if ($newRef === false) {
42✔
1065
                    // The sniff is not locally installed, so check if it is being
1066
                    // referenced as a remote sniff outside the install. We do this
1067
                    // by looking through all directories where we have found ruleset
1068
                    // files before, looking for ones for this particular standard,
1069
                    // and seeing if it is in there.
1070
                    foreach ($this->rulesetDirs as $dir) {
33✔
1071
                        if (strtolower(basename($dir)) !== strtolower($stdName)) {
33✔
1072
                            continue;
33✔
1073
                        }
1074

1075
                        $newRef = Common::realpath($dir.$path);
×
1076

1077
                        if ($newRef !== false) {
×
1078
                            $ref = $newRef;
×
1079
                        }
1080
                    }
1081
                } else {
1082
                    $ref = $newRef;
9✔
1083
                }
1084

1085
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
42✔
NEW
1086
                    StatusWriter::write('=> '.Common::stripBasepath($ref, $this->config->basepath), ($depth + 2));
×
1087
                }
1088
            }//end if
1089
        }//end if
1090

1091
        if (is_dir($ref) === true) {
48✔
1092
            if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
9✔
1093
                // We are referencing an external coding standard.
1094
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
1095
                    StatusWriter::write('* rule is referencing a standard using directory name; processing *', ($depth + 2));
×
1096
                }
1097

1098
                return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2));
3✔
1099
            } else {
1100
                // We are referencing a whole directory of sniffs.
1101
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
NEW
1102
                    StatusWriter::write('* rule is referencing a directory of sniffs *', ($depth + 2));
×
NEW
1103
                    StatusWriter::write('Adding sniff files from directory', ($depth + 2));
×
1104
                }
1105

1106
                return $this->expandSniffDirectory($ref, ($depth + 1));
9✔
1107
            }
1108
        } else {
1109
            if (is_file($ref) === false) {
42✔
1110
                $this->msgCache->add("Referenced sniff \"$ref\" does not exist.", MessageCollector::ERROR);
33✔
1111
                return [];
33✔
1112
            }
1113

1114
            if (substr($ref, -9) === 'Sniff.php') {
9✔
1115
                // A single sniff.
1116
                return [$ref];
9✔
1117
            } else {
1118
                // Assume an external ruleset.xml file.
1119
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
NEW
1120
                    StatusWriter::write('* rule is referencing a standard using ruleset path; processing *', ($depth + 2));
×
1121
                }
1122

1123
                return $this->processRuleset($ref, ($depth + 2));
3✔
1124
            }
1125
        }//end if
1126

1127
    }//end expandRulesetReference()
1128

1129

1130
    /**
1131
     * Processes a rule from a ruleset XML file, overriding built-in defaults.
1132
     *
1133
     * @param \SimpleXMLElement $rule      The rule object from a ruleset XML file.
1134
     * @param string[]          $newSniffs An array of sniffs that got included by this rule.
1135
     * @param int               $depth     How many nested processing steps we are in.
1136
     *                                     This is only used for debug output.
1137
     *
1138
     * @return void
1139
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If rule settings are invalid.
1140
     */
1141
    private function processRule($rule, $newSniffs, $depth=0)
23✔
1142
    {
1143
        $ref  = (string) $rule['ref'];
23✔
1144
        $todo = [$ref];
23✔
1145

1146
        $parts      = explode('.', $ref);
23✔
1147
        $partsCount = count($parts);
23✔
1148
        if ($partsCount <= 2
23✔
1149
            || $partsCount > count(array_filter($parts))
18✔
1150
            || in_array($ref, $newSniffs) === true
23✔
1151
        ) {
1152
            // We are processing a standard, a category of sniffs or a relative path inclusion.
1153
            foreach ($newSniffs as $sniffFile) {
20✔
1154
                $parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
14✔
1155
                if (count($parts) === 1 && DIRECTORY_SEPARATOR === '\\') {
14✔
1156
                    // Path using forward slashes while running on Windows.
1157
                    $parts = explode('/', $sniffFile);
×
1158
                }
1159

1160
                $sniffName     = array_pop($parts);
14✔
1161
                $sniffCategory = array_pop($parts);
14✔
1162
                array_pop($parts);
14✔
1163
                $sniffStandard = array_pop($parts);
14✔
1164
                $todo[]        = $sniffStandard.'.'.$sniffCategory.'.'.substr($sniffName, 0, -9);
14✔
1165
            }
1166
        }
1167

1168
        foreach ($todo as $code) {
23✔
1169
            // Custom severity.
1170
            if (isset($rule->severity) === true
23✔
1171
                && $this->shouldProcessElement($rule->severity) === true
23✔
1172
            ) {
1173
                if (isset($this->ruleset[$code]) === false) {
9✔
1174
                    $this->ruleset[$code] = [];
9✔
1175
                }
1176

1177
                $this->ruleset[$code]['severity'] = (int) $rule->severity;
9✔
1178
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
NEW
1179
                    $statusMessage = '=> severity set to '.(int) $rule->severity;
×
1180
                    if ($code !== $ref) {
×
NEW
1181
                        $statusMessage .= " for $code";
×
1182
                    }
1183

NEW
1184
                    StatusWriter::write($statusMessage, ($depth + 2));
×
1185
                }
1186
            }
1187

1188
            // Custom message type.
1189
            if (isset($rule->type) === true
23✔
1190
                && $this->shouldProcessElement($rule->type) === true
23✔
1191
            ) {
1192
                if (isset($this->ruleset[$code]) === false) {
9✔
1193
                    $this->ruleset[$code] = [];
3✔
1194
                }
1195

1196
                $type = strtolower((string) $rule->type);
9✔
1197
                if ($type !== 'error' && $type !== 'warning') {
9✔
1198
                    $message = "Message type \"$type\" for \"$code\" is invalid; must be \"error\" or \"warning\".";
3✔
1199
                    $this->msgCache->add($message, MessageCollector::ERROR);
3✔
1200
                } else {
1201
                    $this->ruleset[$code]['type'] = $type;
6✔
1202
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
1203
                        $statusMessage = '=> message type set to '.(string) $rule->type;
×
1204
                        if ($code !== $ref) {
×
NEW
1205
                            $statusMessage .= " for $code";
×
1206
                        }
1207

NEW
1208
                        StatusWriter::write($statusMessage, ($depth + 2));
×
1209
                    }
1210
                }
1211
            }//end if
1212

1213
            // Custom message.
1214
            if (isset($rule->message) === true
23✔
1215
                && $this->shouldProcessElement($rule->message) === true
23✔
1216
            ) {
1217
                if (isset($this->ruleset[$code]) === false) {
6✔
1218
                    $this->ruleset[$code] = [];
×
1219
                }
1220

1221
                $this->ruleset[$code]['message'] = (string) $rule->message;
6✔
1222
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
1223
                    $statusMessage = '=> message set to '.(string) $rule->message;
×
1224
                    if ($code !== $ref) {
×
NEW
1225
                        $statusMessage .= " for $code";
×
1226
                    }
1227

NEW
1228
                    StatusWriter::write($statusMessage, ($depth + 2));
×
1229
                }
1230
            }
1231

1232
            // Custom properties.
1233
            if (isset($rule->properties) === true
23✔
1234
                && $this->shouldProcessElement($rule->properties) === true
23✔
1235
            ) {
1236
                $propertyScope = 'standard';
17✔
1237
                if ($code === $ref || substr($ref, -9) === 'Sniff.php') {
17✔
1238
                    $propertyScope = 'sniff';
17✔
1239
                }
1240

1241
                foreach ($rule->properties->property as $prop) {
17✔
1242
                    if ($this->shouldProcessElement($prop) === false) {
17✔
1243
                        continue;
6✔
1244
                    }
1245

1246
                    if (isset($this->ruleset[$code]) === false) {
17✔
1247
                        $this->ruleset[$code] = [
17✔
1248
                            'properties' => [],
17✔
1249
                        ];
11✔
1250
                    } else if (isset($this->ruleset[$code]['properties']) === false) {
12✔
1251
                        $this->ruleset[$code]['properties'] = [];
3✔
1252
                    }
1253

1254
                    $name = (string) $prop['name'];
17✔
1255
                    if (isset($prop['type']) === true
17✔
1256
                        && (string) $prop['type'] === 'array'
17✔
1257
                    ) {
1258
                        if (isset($prop['value']) === true) {
12✔
1259
                            $message  = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL;
×
1260
                            $message .= 'is no longer supported since PHP_CodeSniffer 4.0.0.'.PHP_EOL;
×
1261
                            $message .= "The unsupported syntax was used for property \"$name\"".PHP_EOL;
×
1262
                            $message .= "for sniff \"$code\".".PHP_EOL;
×
1263
                            $message .= 'Pass array values via <element [key="..." ]value="..."> nodes instead.';
×
1264
                            $this->msgCache->add($message, MessageCollector::ERROR);
×
1265

1266
                            continue;
×
1267
                        }
1268

1269
                        $values = [];
12✔
1270
                        if (isset($prop['extend']) === true
12✔
1271
                            && (string) $prop['extend'] === 'true'
12✔
1272
                            && isset($this->ruleset[$code]['properties'][$name]['value']) === true
12✔
1273
                        ) {
1274
                            $values = $this->ruleset[$code]['properties'][$name]['value'];
9✔
1275
                        }
1276

1277
                        if (isset($prop->element) === true) {
12✔
1278
                            $printValue = '';
12✔
1279
                            foreach ($prop->element as $element) {
12✔
1280
                                if ($this->shouldProcessElement($element) === false) {
12✔
1281
                                    continue;
6✔
1282
                                }
1283

1284
                                $value = (string) $element['value'];
12✔
1285
                                if (isset($element['key']) === true) {
12✔
1286
                                    $key          = (string) $element['key'];
3✔
1287
                                    $values[$key] = $value;
3✔
1288
                                    $printValue  .= $key.'=>'.$value.',';
3✔
1289
                                } else {
1290
                                    $values[]    = $value;
12✔
1291
                                    $printValue .= $value.',';
12✔
1292
                                }
1293
                            }
1294

1295
                            $printValue = rtrim($printValue, ',');
12✔
1296
                        }
1297

1298
                        $this->ruleset[$code]['properties'][$name] = [
12✔
1299
                            'value' => $values,
12✔
1300
                            'scope' => $propertyScope,
12✔
1301
                        ];
8✔
1302
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
12✔
NEW
1303
                            $statusMessage = "=> array property \"$name\" set to \"$printValue\"";
×
1304
                            if ($code !== $ref) {
×
NEW
1305
                                $statusMessage .= " for $code";
×
1306
                            }
1307

1308
                            StatusWriter::write($statusMessage, ($depth + 2));
8✔
1309
                        }
1310
                    } else {
1311
                        $this->ruleset[$code]['properties'][$name] = [
17✔
1312
                            'value' => (string) $prop['value'],
17✔
1313
                            'scope' => $propertyScope,
17✔
1314
                        ];
11✔
1315
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
17✔
NEW
1316
                            $statusMessage = "=> property \"$name\" set to \"".(string) $prop['value'].'"';
×
1317
                            if ($code !== $ref) {
×
NEW
1318
                                $statusMessage .= " for $code";
×
1319
                            }
1320

NEW
1321
                            StatusWriter::write($statusMessage, ($depth + 2));
×
1322
                        }
1323
                    }//end if
1324
                }//end foreach
1325
            }//end if
1326

1327
            // Ignore patterns.
1328
            foreach ($rule->{'exclude-pattern'} as $pattern) {
23✔
1329
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1330
                    continue;
6✔
1331
                }
1332

1333
                if (isset($this->ignorePatterns[$code]) === false) {
6✔
1334
                    $this->ignorePatterns[$code] = [];
6✔
1335
                }
1336

1337
                if (isset($pattern['type']) === false) {
6✔
1338
                    $pattern['type'] = 'absolute';
6✔
1339
                }
1340

1341
                $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type'];
6✔
1342
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
1343
                    $statusMessage = '=> added rule-specific '.(string) $pattern['type'].' ignore pattern';
×
1344
                    if ($code !== $ref) {
×
NEW
1345
                        $statusMessage .= " for $code";
×
1346
                    }
1347

NEW
1348
                    StatusWriter::write($statusMessage.': '.(string) $pattern, ($depth + 2));
×
1349
                }
1350
            }//end foreach
1351

1352
            // Include patterns.
1353
            foreach ($rule->{'include-pattern'} as $pattern) {
23✔
1354
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1355
                    continue;
6✔
1356
                }
1357

1358
                if (isset($this->includePatterns[$code]) === false) {
6✔
1359
                    $this->includePatterns[$code] = [];
6✔
1360
                }
1361

1362
                if (isset($pattern['type']) === false) {
6✔
1363
                    $pattern['type'] = 'absolute';
6✔
1364
                }
1365

1366
                $this->includePatterns[$code][(string) $pattern] = (string) $pattern['type'];
6✔
1367
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
NEW
1368
                    $statusMessage = '=> added rule-specific '.(string) $pattern['type'].' include pattern';
×
1369
                    if ($code !== $ref) {
×
NEW
1370
                        $statusMessage .= " for $code";
×
1371
                    }
1372

NEW
1373
                    StatusWriter::write($statusMessage.': '.(string) $pattern, ($depth + 2));
×
1374
                }
1375
            }//end foreach
1376
        }//end foreach
1377

1378
    }//end processRule()
8✔
1379

1380

1381
    /**
1382
     * Determine if an element should be processed or ignored.
1383
     *
1384
     * @param \SimpleXMLElement $element An object from a ruleset XML file.
1385
     *
1386
     * @return bool
1387
     */
1388
    private function shouldProcessElement($element)
20✔
1389
    {
1390
        if (isset($element['phpcbf-only']) === false
20✔
1391
            && isset($element['phpcs-only']) === false
20✔
1392
        ) {
1393
            // No exceptions are being made.
1394
            return true;
20✔
1395
        }
1396

1397
        if (PHP_CODESNIFFER_CBF === true
12✔
1398
            && isset($element['phpcbf-only']) === true
12✔
1399
            && (string) $element['phpcbf-only'] === 'true'
12✔
1400
        ) {
1401
            return true;
6✔
1402
        }
1403

1404
        if (PHP_CODESNIFFER_CBF === false
12✔
1405
            && isset($element['phpcs-only']) === true
12✔
1406
            && (string) $element['phpcs-only'] === 'true'
12✔
1407
        ) {
1408
            return true;
6✔
1409
        }
1410

1411
        return false;
12✔
1412

1413
    }//end shouldProcessElement()
1414

1415

1416
    /**
1417
     * Loads and stores sniffs objects used for sniffing files.
1418
     *
1419
     * @param array $files        Paths to the sniff files to register.
1420
     * @param array $restrictions The sniff class names to restrict the allowed
1421
     *                            listeners to.
1422
     * @param array $exclusions   The sniff class names to exclude from the
1423
     *                            listeners list.
1424
     *
1425
     * @return void
1426
     */
1427
    public function registerSniffs($files, $restrictions, $exclusions)
47✔
1428
    {
1429
        $listeners = [];
47✔
1430

1431
        foreach ($files as $file) {
47✔
1432
            // Work out where the position of /StandardName/Sniffs/... is
1433
            // so we can determine what the class will be called.
1434
            $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR);
47✔
1435
            if ($sniffPos === false) {
47✔
1436
                continue;
3✔
1437
            }
1438

1439
            $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR);
47✔
1440
            if ($slashPos === false) {
47✔
1441
                continue;
×
1442
            }
1443

1444
            $className   = Autoload::loadFile($file);
47✔
1445
            $compareName = Common::cleanSniffClass($className);
47✔
1446

1447
            // If they have specified a list of sniffs to restrict to, check
1448
            // to see if this sniff is allowed.
1449
            if (empty($restrictions) === false
47✔
1450
                && isset($restrictions[$compareName]) === false
47✔
1451
            ) {
1452
                continue;
6✔
1453
            }
1454

1455
            // If they have specified a list of sniffs to exclude, check
1456
            // to see if this sniff is allowed.
1457
            if (empty($exclusions) === false
47✔
1458
                && isset($exclusions[$compareName]) === true
47✔
1459
            ) {
1460
                continue;
6✔
1461
            }
1462

1463
            // Skip abstract classes.
1464
            $reflection = new ReflectionClass($className);
47✔
1465
            if ($reflection->isAbstract() === true) {
47✔
1466
                continue;
3✔
1467
            }
1468

1469
            if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\Sniff') === false) {
47✔
1470
                $message  = 'All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL;
3✔
1471
                $message .= "Interface not implemented for sniff $className.".PHP_EOL;
3✔
1472
                $message .= 'Contact the sniff author to fix the sniff.';
3✔
1473
                $this->msgCache->add($message, MessageCollector::ERROR);
3✔
1474
                continue;
3✔
1475
            }
1476

1477
            if ($reflection->hasProperty('supportedTokenizers') === true) {
47✔
1478
                // Using the default value as the class is not yet instantiated and this is not a property which should get changed anyway.
1479
                $value = $reflection->getDefaultProperties()['supportedTokenizers'];
15✔
1480

1481
                if (is_array($value) === true
15✔
1482
                    && empty($value) === false
15✔
1483
                    && in_array('PHP', $value, true) === false
15✔
1484
                ) {
1485
                    if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\DeprecatedSniff') === true) {
15✔
1486
                        // Silently ignore the sniff if the sniff is marked as deprecated.
1487
                        continue;
15✔
1488
                    }
1489

1490
                    $message  = 'Support for scanning files other than PHP, like CSS/JS files, has been removed in PHP_CodeSniffer 4.0.'.PHP_EOL;
15✔
1491
                    $message .= 'The %s sniff is listening for %s.';
15✔
1492
                    $message  = sprintf($message, Common::getSniffCode($className), implode(', ', $value));
15✔
1493
                    $this->msgCache->add($message, MessageCollector::ERROR);
15✔
1494
                    continue;
15✔
1495
                }
1496
            }//end if
1497

1498
            $listeners[$className] = $className;
47✔
1499

1500
            if (PHP_CODESNIFFER_VERBOSITY > 2) {
47✔
NEW
1501
                StatusWriter::write("Registered $className");
×
1502
            }
1503
        }//end foreach
1504

1505
        $this->sniffs = $listeners;
47✔
1506

1507
    }//end registerSniffs()
16✔
1508

1509

1510
    /**
1511
     * Populates the array of PHP_CodeSniffer_Sniff objects for this file.
1512
     *
1513
     * @return void
1514
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If sniff registration fails.
1515
     */
1516
    public function populateTokenListeners()
14✔
1517
    {
1518
        // Construct a list of listeners indexed by token being listened for.
1519
        $this->tokenListeners = [];
14✔
1520

1521
        foreach ($this->sniffs as $sniffClass => $sniffObject) {
14✔
1522
            try {
1523
                $sniffCode = Common::getSniffCode($sniffClass);
14✔
1524
            } catch (InvalidArgumentException $e) {
3✔
1525
                $message  = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.".PHP_EOL;
3✔
1526
                $message .= 'Contact the sniff author to fix the sniff.';
3✔
1527
                $this->msgCache->add($message, MessageCollector::ERROR);
3✔
1528

1529
                // Unregister the sniff.
1530
                unset($this->sniffs[$sniffClass]);
3✔
1531
                continue;
3✔
1532
            }
1533

1534
            $this->sniffs[$sniffClass]    = new $sniffClass();
14✔
1535
            $this->sniffCodes[$sniffCode] = $sniffClass;
14✔
1536

1537
            if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) {
14✔
1538
                $this->deprecatedSniffs[$sniffCode] = $sniffClass;
3✔
1539
            }
1540

1541
            // Set custom properties.
1542
            if (isset($this->ruleset[$sniffCode]['properties']) === true) {
14✔
1543
                foreach ($this->ruleset[$sniffCode]['properties'] as $name => $settings) {
11✔
1544
                    $this->setSniffProperty($sniffClass, $name, $settings);
11✔
1545
                }
1546
            }
1547

1548
            $tokens = $this->sniffs[$sniffClass]->register();
14✔
1549
            if (is_array($tokens) === false) {
14✔
1550
                $msg = "The sniff {$sniffClass}::register() method must return an array.";
3✔
1551
                $this->msgCache->add($msg, MessageCollector::ERROR);
3✔
1552

1553
                // Unregister the sniff.
1554
                unset($this->sniffs[$sniffClass], $this->sniffCodes[$sniffCode], $this->deprecatedSniffs[$sniffCode]);
3✔
1555
                continue;
3✔
1556
            }
1557

1558
            $ignorePatterns = [];
14✔
1559
            $patterns       = $this->getIgnorePatterns($sniffCode);
14✔
1560
            foreach ($patterns as $pattern => $type) {
14✔
1561
                $replacements = [
2✔
1562
                    '\\,' => ',',
3✔
1563
                    '*'   => '.*',
2✔
1564
                ];
2✔
1565

1566
                $ignorePatterns[] = strtr($pattern, $replacements);
3✔
1567
            }
1568

1569
            $includePatterns = [];
14✔
1570
            $patterns        = $this->getIncludePatterns($sniffCode);
14✔
1571
            foreach ($patterns as $pattern => $type) {
14✔
1572
                $replacements = [
2✔
1573
                    '\\,' => ',',
3✔
1574
                    '*'   => '.*',
2✔
1575
                ];
2✔
1576

1577
                $includePatterns[] = strtr($pattern, $replacements);
3✔
1578
            }
1579

1580
            foreach ($tokens as $token) {
14✔
1581
                if (isset($this->tokenListeners[$token]) === false) {
14✔
1582
                    $this->tokenListeners[$token] = [];
14✔
1583
                }
1584

1585
                if (isset($this->tokenListeners[$token][$sniffClass]) === false) {
14✔
1586
                    $this->tokenListeners[$token][$sniffClass] = [
14✔
1587
                        'class'   => $sniffClass,
14✔
1588
                        'source'  => $sniffCode,
14✔
1589
                        'ignore'  => $ignorePatterns,
14✔
1590
                        'include' => $includePatterns,
14✔
1591
                    ];
9✔
1592
                }
1593
            }
1594
        }//end foreach
1595

1596
    }//end populateTokenListeners()
5✔
1597

1598

1599
    /**
1600
     * Set a single property for a sniff.
1601
     *
1602
     * @param string $sniffClass The class name of the sniff.
1603
     * @param string $name       The name of the property to change.
1604
     * @param array  $settings   Array with the new value of the property and the scope of the property being set.
1605
     *
1606
     * @return void
1607
     *
1608
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When attempting to set a non-existent property on a sniff
1609
     *                                                      which doesn't declare the property or explicitly supports
1610
     *                                                      dynamic properties.
1611
     */
1612
    public function setSniffProperty($sniffClass, $name, $settings)
44✔
1613
    {
1614
        // Setting a property for a sniff we are not using.
1615
        if (isset($this->sniffs[$sniffClass]) === false) {
44✔
1616
            return;
3✔
1617
        }
1618

1619
        $name         = trim($name);
41✔
1620
        $propertyName = $name;
41✔
1621
        if (substr($propertyName, -2) === '[]') {
41✔
1622
            $propertyName = substr($propertyName, 0, -2);
3✔
1623
        }
1624

1625
        $isSettable  = false;
41✔
1626
        $sniffObject = $this->sniffs[$sniffClass];
41✔
1627
        if (property_exists($sniffObject, $propertyName) === true
41✔
1628
            || ($sniffObject instanceof stdClass) === true
24✔
1629
            || method_exists($sniffObject, '__set') === true
41✔
1630
        ) {
1631
            $isSettable = true;
29✔
1632
        }
1633

1634
        if ($isSettable === false) {
41✔
1635
            if ($settings['scope'] === 'sniff') {
18✔
1636
                $notice  = "Property \"$propertyName\" does not exist on sniff ";
6✔
1637
                $notice .= array_search($sniffClass, $this->sniffCodes, true).'.';
6✔
1638
                $this->msgCache->add($notice, MessageCollector::ERROR);
6✔
1639
            }
1640

1641
            return;
18✔
1642
        }
1643

1644
        $value = $this->getRealPropertyValue($settings['value']);
29✔
1645

1646
        // Handle properties set inline via phpcs:set.
1647
        if (substr($name, -2) === '[]') {
29✔
1648
            $values = [];
3✔
1649
            if (is_string($value) === true) {
3✔
1650
                foreach (explode(',', $value) as $val) {
3✔
1651
                    list($k, $v) = explode('=>', $val.'=>');
3✔
1652
                    if ($v !== '') {
3✔
1653
                        $values[trim($k)] = $v;
3✔
1654
                    } else {
1655
                        $values[] = $k;
3✔
1656
                    }
1657
                }
1658
            }
1659

1660
            $value = $this->getRealPropertyValue($values);
3✔
1661
        }
1662

1663
        $sniffObject->$propertyName = $value;
29✔
1664

1665
    }//end setSniffProperty()
10✔
1666

1667

1668
    /**
1669
     * Transform a property value received via a ruleset or inline annotation to a typed value.
1670
     *
1671
     * @param string|array<int|string, string> $value The current property value.
1672
     *
1673
     * @return mixed
1674
     */
1675
    private function getRealPropertyValue($value)
14✔
1676
    {
1677
        if (is_array($value) === true) {
14✔
1678
            foreach ($value as $k => $v) {
9✔
1679
                $value[$k] = $this->getRealPropertyValue($v);
9✔
1680
            }
1681

1682
            return $value;
9✔
1683
        }
1684

1685
        if (is_string($value) === true) {
14✔
1686
            $value = trim($value);
14✔
1687

1688
            if ($value === '') {
14✔
1689
                return null;
6✔
1690
            }
1691

1692
            $valueLc = strtolower($value);
14✔
1693

1694
            if ($valueLc === 'true') {
14✔
1695
                return true;
9✔
1696
            }
1697

1698
            if ($valueLc === 'false') {
14✔
1699
                return false;
9✔
1700
            }
1701

1702
            if ($valueLc === 'null') {
14✔
1703
                return null;
6✔
1704
            }
1705
        }//end if
1706

1707
        return $value;
14✔
1708

1709
    }//end getRealPropertyValue()
1710

1711

1712
    /**
1713
     * Gets the array of ignore patterns.
1714
     *
1715
     * Optionally takes a listener to get ignore patterns specified
1716
     * for that sniff only.
1717
     *
1718
     * @param string $listener The listener to get patterns for. If NULL, all
1719
     *                         patterns are returned.
1720
     *
1721
     * @return array
1722
     */
1723
    public function getIgnorePatterns($listener=null)
20✔
1724
    {
1725
        if ($listener === null) {
20✔
1726
            return $this->ignorePatterns;
3✔
1727
        }
1728

1729
        if (isset($this->ignorePatterns[$listener]) === true) {
17✔
1730
            return $this->ignorePatterns[$listener];
6✔
1731
        }
1732

1733
        return [];
11✔
1734

1735
    }//end getIgnorePatterns()
1736

1737

1738
    /**
1739
     * Gets the array of include patterns.
1740
     *
1741
     * Optionally takes a listener to get include patterns specified
1742
     * for that sniff only.
1743
     *
1744
     * @param string $listener The listener to get patterns for. If NULL, all
1745
     *                         patterns are returned.
1746
     *
1747
     * @return array
1748
     */
1749
    public function getIncludePatterns($listener=null)
20✔
1750
    {
1751
        if ($listener === null) {
20✔
1752
            return $this->includePatterns;
3✔
1753
        }
1754

1755
        if (isset($this->includePatterns[$listener]) === true) {
17✔
1756
            return $this->includePatterns[$listener];
6✔
1757
        }
1758

1759
        return [];
11✔
1760

1761
    }//end getIncludePatterns()
1762

1763

1764
}//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

© 2026 Coveralls, Inc