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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

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

88.96
/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');
7✔
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✔
222
                $newlines = 0;
×
223
                if (count($config->standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
×
224
                    $newlines = 1;
×
225
                }
226

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✔
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✔
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)
70✔
531
    {
532
        $rulesetPath = Common::realpath($rulesetPath);
70✔
533
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
70✔
534
            StatusWriter::write('Processing ruleset '.Common::stripBasepath($rulesetPath, $this->config->basepath), $depth);
×
535
        }
536

537
        libxml_use_internal_errors(true);
70✔
538
        $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
70✔
539
        if ($ruleset === false) {
70✔
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);
55✔
551

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

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

560
        $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs';
55✔
561
        if (is_dir($sniffDir) === true) {
55✔
562
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
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) {
55✔
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✔
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) {
52✔
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✔
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✔
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 = [];
52✔
628
        foreach ($ruleset->{'arg'} as $arg) {
52✔
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✔
646
                        $statusMessage = '=> ignoring command line arg --'.$name;
×
647
                        if (isset($arg['value']) === true) {
×
648
                            $statusMessage .= '='.(string) $arg['value'];
×
649
                        }
650

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✔
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✔
702
                StatusWriter::write("=> set command line value $argString", ($depth + 1));
×
703
            }
704
        }//end foreach
705

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

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

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

721
            $parts = explode('.', $rule['ref']);
52✔
722
            if (count($parts) === 4
52✔
723
                && $parts[0] !== ''
52✔
724
                && $parts[1] !== ''
52✔
725
                && $parts[2] !== ''
52✔
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✔
735
                        StatusWriter::write('* disabling sniff exclusion for specific message code *', ($depth + 2));
×
736
                        StatusWriter::write('=> severity set to 5', ($depth + 2));
2✔
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✔
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) {
52✔
754
                foreach ($rule->exclude as $exclude) {
15✔
755
                    if (isset($exclude['name']) === false) {
15✔
756
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
757
                            StatusWriter::write('* ignoring empty exclude rule *', ($depth + 2));
×
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✔
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));
2✔
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);
52✔
790
        }//end foreach
791

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

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

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

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

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

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

830
        if (empty($cliArgs) === false) {
52✔
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);
35✔
835
            if ($inPhar === false) {
35✔
836
                $currentDir = getcwd();
35✔
837
                chdir($rulesetDir);
35✔
838
            }
839

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

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

847
        // Process custom ignore pattern rules.
848
        foreach ($ruleset->{'exclude-pattern'} as $pattern) {
44✔
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✔
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));
44✔
864
        $excludedSniffs = array_unique($excludedSniffs);
44✔
865

866
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
44✔
867
            $included = count($includedSniffs);
×
868
            $excluded = count($excludedSniffs);
×
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 = [];
44✔
875
        foreach ($includedSniffs as $sniff) {
44✔
876
            if (in_array($sniff, $excludedSniffs, true) === true) {
44✔
877
                continue;
6✔
878
            } else {
879
                $files[] = Common::realpath($sniff);
44✔
880
            }
881
        }
882

883
        return $files;
44✔
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✔
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✔
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✔
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✔
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($ref, 3);
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));
1✔
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✔
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✔
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✔
1102
                    StatusWriter::write('* rule is referencing a directory of sniffs *', ($depth + 2));
×
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✔
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✔
1179
                    $statusMessage = '=> severity set to '.(int) $rule->severity;
×
1180
                    if ($code !== $ref) {
×
1181
                        $statusMessage .= " for $code";
×
1182
                    }
1183

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✔
1203
                        $statusMessage = '=> message type set to '.(string) $rule->type;
×
1204
                        if ($code !== $ref) {
×
1205
                            $statusMessage .= " for $code";
×
1206
                        }
1207

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✔
1223
                    $statusMessage = '=> message set to '.(string) $rule->message;
×
1224
                    if ($code !== $ref) {
×
1225
                        $statusMessage .= " for $code";
×
1226
                    }
1227

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
                        $extend = false;
12✔
1271
                        if (isset($prop['extend']) === true && (string) $prop['extend'] === 'true') {
12✔
1272
                            if (isset($this->ruleset[$code]['properties'][$name]['value']) === true) {
9✔
1273
                                $values = $this->ruleset[$code]['properties'][$name]['value'];
9✔
1274
                                $extend = $this->ruleset[$code]['properties'][$name]['extend'];
9✔
1275
                            } else {
1276
                                $extend = true;
3✔
1277
                            }
1278
                        }
1279

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

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

1298
                            $printValue = rtrim($printValue, ',');
12✔
1299
                        }
1300

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

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

1325
                            StatusWriter::write($statusMessage, ($depth + 2));
×
1326
                        }
1327
                    }//end if
1328
                }//end foreach
1329
            }//end if
1330

1331
            // Ignore patterns.
1332
            foreach ($rule->{'exclude-pattern'} as $pattern) {
23✔
1333
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1334
                    continue;
6✔
1335
                }
1336

1337
                if (isset($this->ignorePatterns[$code]) === false) {
6✔
1338
                    $this->ignorePatterns[$code] = [];
6✔
1339
                }
1340

1341
                if (isset($pattern['type']) === false) {
6✔
1342
                    $pattern['type'] = 'absolute';
6✔
1343
                }
1344

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

1352
                    StatusWriter::write($statusMessage.': '.(string) $pattern, ($depth + 2));
×
1353
                }
1354
            }//end foreach
1355

1356
            // Include patterns.
1357
            foreach ($rule->{'include-pattern'} as $pattern) {
23✔
1358
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1359
                    continue;
6✔
1360
                }
1361

1362
                if (isset($this->includePatterns[$code]) === false) {
6✔
1363
                    $this->includePatterns[$code] = [];
6✔
1364
                }
1365

1366
                if (isset($pattern['type']) === false) {
6✔
1367
                    $pattern['type'] = 'absolute';
6✔
1368
                }
1369

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

1377
                    StatusWriter::write($statusMessage.': '.(string) $pattern, ($depth + 2));
×
1378
                }
1379
            }//end foreach
1380
        }//end foreach
1381

1382
    }//end processRule()
8✔
1383

1384

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

1401
        if (PHP_CODESNIFFER_CBF === true
12✔
1402
            && isset($element['phpcbf-only']) === true
12✔
1403
            && (string) $element['phpcbf-only'] === 'true'
12✔
1404
        ) {
1405
            return true;
6✔
1406
        }
1407

1408
        if (PHP_CODESNIFFER_CBF === false
12✔
1409
            && isset($element['phpcs-only']) === true
12✔
1410
            && (string) $element['phpcs-only'] === 'true'
12✔
1411
        ) {
1412
            return true;
6✔
1413
        }
1414

1415
        return false;
12✔
1416

1417
    }//end shouldProcessElement()
1418

1419

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

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

1443
            $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR);
47✔
1444
            if ($slashPos === false) {
47✔
1445
                continue;
×
1446
            }
1447

1448
            $className   = Autoload::loadFile($file);
47✔
1449
            $compareName = Common::cleanSniffClass($className);
47✔
1450

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

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

1467
            // Skip abstract classes.
1468
            $reflection = new ReflectionClass($className);
47✔
1469
            if ($reflection->isAbstract() === true) {
47✔
1470
                continue;
3✔
1471
            }
1472

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

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

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

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

1502
            $listeners[$className] = $className;
47✔
1503

1504
            if (PHP_CODESNIFFER_VERBOSITY > 2) {
47✔
1505
                StatusWriter::write("Registered $className");
×
1506
            }
1507
        }//end foreach
1508

1509
        $this->sniffs = $listeners;
47✔
1510

1511
    }//end registerSniffs()
16✔
1512

1513

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

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

1533
                // Unregister the sniff.
1534
                unset($this->sniffs[$sniffClass]);
3✔
1535
                continue;
3✔
1536
            }
1537

1538
            $this->sniffs[$sniffClass]    = new $sniffClass();
14✔
1539
            $this->sniffCodes[$sniffCode] = $sniffClass;
14✔
1540

1541
            if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) {
14✔
1542
                $this->deprecatedSniffs[$sniffCode] = $sniffClass;
3✔
1543
            }
1544

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

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

1557
                // Unregister the sniff.
1558
                unset($this->sniffs[$sniffClass], $this->sniffCodes[$sniffCode], $this->deprecatedSniffs[$sniffCode]);
3✔
1559
                continue;
3✔
1560
            }
1561

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

1570
                $ignorePatterns[] = strtr($pattern, $replacements);
3✔
1571
            }
1572

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

1581
                $includePatterns[] = strtr($pattern, $replacements);
3✔
1582
            }
1583

1584
            foreach ($tokens as $token) {
14✔
1585
                if (isset($this->tokenListeners[$token]) === false) {
14✔
1586
                    $this->tokenListeners[$token] = [];
14✔
1587
                }
1588

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

1600
    }//end populateTokenListeners()
5✔
1601

1602

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

1624
        $name         = trim($name);
41✔
1625
        $propertyName = $name;
41✔
1626
        if (substr($propertyName, -2) === '[]') {
41✔
1627
            $propertyName = substr($propertyName, 0, -2);
3✔
1628
        }
1629

1630
        $isSettable  = false;
41✔
1631
        $sniffObject = $this->sniffs[$sniffClass];
41✔
1632
        if (property_exists($sniffObject, $propertyName) === true
41✔
1633
            || ($sniffObject instanceof stdClass) === true
24✔
1634
            || method_exists($sniffObject, '__set') === true
41✔
1635
        ) {
1636
            $isSettable = true;
29✔
1637
        }
1638

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

1646
            return;
18✔
1647
        }
1648

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

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

1665
            $value = $this->getRealPropertyValue($values);
3✔
1666
        }
1667

1668
        if (isset($settings['extend']) === true
29✔
1669
            && $settings['extend'] === true
29✔
1670
            && isset($sniffObject->$propertyName) === true
29✔
1671
            && is_array($sniffObject->$propertyName) === true
29✔
1672
            && is_array($value) === true
29✔
1673
        ) {
1674
            $sniffObject->$propertyName = array_merge($sniffObject->$propertyName, $value);
3✔
1675
        } else {
1676
            $sniffObject->$propertyName = $value;
29✔
1677
        }
1678

1679
    }//end setSniffProperty()
10✔
1680

1681

1682
    /**
1683
     * Transform a property value received via a ruleset or inline annotation to a typed value.
1684
     *
1685
     * @param string|array<int|string, string> $value The current property value.
1686
     *
1687
     * @return mixed
1688
     */
1689
    private function getRealPropertyValue($value)
14✔
1690
    {
1691
        if (is_array($value) === true) {
14✔
1692
            foreach ($value as $k => $v) {
9✔
1693
                $value[$k] = $this->getRealPropertyValue($v);
9✔
1694
            }
1695

1696
            return $value;
9✔
1697
        }
1698

1699
        if (is_string($value) === true) {
14✔
1700
            $value = trim($value);
14✔
1701

1702
            if ($value === '') {
14✔
1703
                return null;
6✔
1704
            }
1705

1706
            $valueLc = strtolower($value);
14✔
1707

1708
            if ($valueLc === 'true') {
14✔
1709
                return true;
9✔
1710
            }
1711

1712
            if ($valueLc === 'false') {
14✔
1713
                return false;
9✔
1714
            }
1715

1716
            if ($valueLc === 'null') {
14✔
1717
                return null;
6✔
1718
            }
1719
        }//end if
1720

1721
        return $value;
14✔
1722

1723
    }//end getRealPropertyValue()
1724

1725

1726
    /**
1727
     * Gets the array of ignore patterns.
1728
     *
1729
     * Optionally takes a listener to get ignore patterns specified
1730
     * for that sniff only.
1731
     *
1732
     * @param string $listener The listener to get patterns for. If NULL, all
1733
     *                         patterns are returned.
1734
     *
1735
     * @return array
1736
     */
1737
    public function getIgnorePatterns($listener=null)
20✔
1738
    {
1739
        if ($listener === null) {
20✔
1740
            return $this->ignorePatterns;
3✔
1741
        }
1742

1743
        if (isset($this->ignorePatterns[$listener]) === true) {
17✔
1744
            return $this->ignorePatterns[$listener];
6✔
1745
        }
1746

1747
        return [];
11✔
1748

1749
    }//end getIgnorePatterns()
1750

1751

1752
    /**
1753
     * Gets the array of include patterns.
1754
     *
1755
     * Optionally takes a listener to get include patterns specified
1756
     * for that sniff only.
1757
     *
1758
     * @param string $listener The listener to get patterns for. If NULL, all
1759
     *                         patterns are returned.
1760
     *
1761
     * @return array
1762
     */
1763
    public function getIncludePatterns($listener=null)
20✔
1764
    {
1765
        if ($listener === null) {
20✔
1766
            return $this->includePatterns;
3✔
1767
        }
1768

1769
        if (isset($this->includePatterns[$listener]) === true) {
17✔
1770
            return $this->includePatterns[$listener];
6✔
1771
        }
1772

1773
        return [];
11✔
1774

1775
    }//end getIncludePatterns()
1776

1777

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