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

PHPCSStandards / PHP_CodeSniffer / 15036337869

15 May 2025 04:03AM UTC coverage: 78.375% (-0.2%) from 78.556%
15036337869

Pull #856

github

web-flow
Merge 93f570b46 into f5e7943d0
Pull Request #856: [Doc] Cover all errors of PEAR ClassDeclaration

25112 of 32041 relevant lines covered (78.37%)

69.4 hits per line

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

89.14
/src/Ruleset.php
1
<?php
2
/**
3
 * Stores the rules used to check and fix files.
4
 *
5
 * A ruleset object directly maps to a ruleset XML file.
6
 *
7
 * @author    Greg Sherwood <gsherwood@squiz.net>
8
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
9
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10
 */
11

12
namespace PHP_CodeSniffer;
13

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

24
class Ruleset
25
{
26

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

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

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

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

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

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

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

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

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

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

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

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

149

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

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

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

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

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

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

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

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

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

216
                break;
6✔
217
            }
218

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

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

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

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

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

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

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

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

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

263
    }//end __construct()
25✔
264

265

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

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

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

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

287
        echo $summaryLine;
15✔
288

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

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

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

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

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

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

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

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

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

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

338
    }//end explain()
10✔
339

340

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

350
    }//end hasSniffDeprecations()
351

352

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

479
    }//end showSniffDeprecations()
12✔
480

481

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

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

508
    }//end displayCachedMessages()
15✔
509

510

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

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

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

546
        libxml_use_internal_errors(false);
35✔
547

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

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

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

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

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

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

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

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

583
            include_once $autoloadPath;
6✔
584

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

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

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

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

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

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

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

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

664
                        continue;
3✔
665
                    }
666

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

816
        return $files;
32✔
817

818
    }//end processRuleset()
819

820

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

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

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

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

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

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

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

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

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

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

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

876
        return $sniffs;
6✔
877

878
    }//end expandSniffDirectory()
879

880

881
    /**
882
     * Expands a ruleset reference into a list of sniff files.
883
     *
884
     * @param string $ref        The reference from the ruleset XML file.
885
     * @param string $rulesetDir The directory of the ruleset XML file, used to
886
     *                           evaluate relative paths.
887
     * @param int    $depth      How many nested processing steps we are in. This
888
     *                           is only used for debug output.
889
     *
890
     * @return array
891
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the reference is invalid.
892
     */
893
    private function expandRulesetReference($ref, $rulesetDir, $depth=0)
53✔
894
    {
895
        // Naming an (external) standard "Internal" is deprecated.
896
        if (strtolower($ref) === 'internal') {
53✔
897
            $message  = 'The name "Internal" is reserved for internal use. A PHP_CodeSniffer standard should not be called "Internal".'.PHP_EOL;
3✔
898
            $message .= 'Contact the maintainer of the standard to fix this.';
3✔
899
            $this->msgCache->add($message, MessageCollector::DEPRECATED);
3✔
900
        }
1✔
901

902
        // Ignore internal sniffs codes as they are used to only
903
        // hide and change internal messages.
904
        if (substr($ref, 0, 9) === 'Internal.') {
53✔
905
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
906
                echo str_repeat("\t", $depth);
×
907
                echo "\t\t* ignoring internal sniff code *".PHP_EOL;
×
908
            }
909

910
            return [];
3✔
911
        }
912

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

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

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

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

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

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

1011
                        $newRef = Common::realpath($dir.$path);
×
1012

1013
                        if ($newRef !== false) {
×
1014
                            $ref = $newRef;
×
1015
                        }
1016
                    }
11✔
1017
                } else {
11✔
1018
                    $ref = $newRef;
6✔
1019
                }
1020

1021
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
39✔
1022
                    echo str_repeat("\t", $depth);
×
1023
                    echo "\t\t=> ".Common::stripBasepath($ref, $this->config->basepath).PHP_EOL;
×
1024
                }
1025
            }//end if
13✔
1026
        }//end if
1027

1028
        if (is_dir($ref) === true) {
48✔
1029
            if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
9✔
1030
                // We are referencing an external coding standard.
1031
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
3✔
1032
                    echo str_repeat("\t", $depth);
×
1033
                    echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL;
×
1034
                }
1035

1036
                return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2));
3✔
1037
            } else {
1038
                // We are referencing a whole directory of sniffs.
1039
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
1040
                    echo str_repeat("\t", $depth);
×
1041
                    echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL;
×
1042
                    echo str_repeat("\t", $depth);
×
1043
                    echo "\t\tAdding sniff files from directory".PHP_EOL;
×
1044
                }
1045

1046
                return $this->expandSniffDirectory($ref, ($depth + 1));
9✔
1047
            }
1048
        } else {
1049
            if (is_file($ref) === false) {
42✔
1050
                $this->msgCache->add("Referenced sniff \"$ref\" does not exist.", MessageCollector::ERROR);
33✔
1051
                return [];
33✔
1052
            }
1053

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

1064
                return $this->processRuleset($ref, ($depth + 2));
6✔
1065
            }
1066
        }//end if
1067

1068
    }//end expandRulesetReference()
1069

1070

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

1087
        $parts      = explode('.', $ref);
26✔
1088
        $partsCount = count($parts);
26✔
1089
        if ($partsCount <= 2
17✔
1090
            || $partsCount > count(array_filter($parts))
23✔
1091
            || in_array($ref, $newSniffs) === true
24✔
1092
        ) {
9✔
1093
            // We are processing a standard, a category of sniffs or a relative path inclusion.
1094
            foreach ($newSniffs as $sniffFile) {
23✔
1095
                $parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
17✔
1096
                if (count($parts) === 1 && DIRECTORY_SEPARATOR === '\\') {
17✔
1097
                    // Path using forward slashes while running on Windows.
1098
                    $parts = explode('/', $sniffFile);
×
1099
                }
1100

1101
                $sniffName     = array_pop($parts);
17✔
1102
                $sniffCategory = array_pop($parts);
17✔
1103
                array_pop($parts);
17✔
1104
                $sniffStandard = array_pop($parts);
17✔
1105
                $todo[]        = $sniffStandard.'.'.$sniffCategory.'.'.substr($sniffName, 0, -9);
17✔
1106
            }
8✔
1107
        }
8✔
1108

1109
        foreach ($todo as $code) {
26✔
1110
            // Custom severity.
1111
            if (isset($rule->severity) === true
26✔
1112
                && $this->shouldProcessElement($rule->severity) === true
26✔
1113
            ) {
9✔
1114
                if (isset($this->ruleset[$code]) === false) {
9✔
1115
                    $this->ruleset[$code] = [];
9✔
1116
                }
3✔
1117

1118
                $this->ruleset[$code]['severity'] = (int) $rule->severity;
9✔
1119
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
9✔
1120
                    echo str_repeat("\t", $depth);
×
1121
                    echo "\t\t=> severity set to ".(int) $rule->severity;
×
1122
                    if ($code !== $ref) {
×
1123
                        echo " for $code";
×
1124
                    }
1125

1126
                    echo PHP_EOL;
×
1127
                }
1128
            }
3✔
1129

1130
            // Custom message type.
1131
            if (isset($rule->type) === true
26✔
1132
                && $this->shouldProcessElement($rule->type) === true
26✔
1133
            ) {
9✔
1134
                if (isset($this->ruleset[$code]) === false) {
9✔
1135
                    $this->ruleset[$code] = [];
3✔
1136
                }
1✔
1137

1138
                $type = strtolower((string) $rule->type);
9✔
1139
                if ($type !== 'error' && $type !== 'warning') {
9✔
1140
                    $message = "Message type \"$type\" for \"$code\" is invalid; must be \"error\" or \"warning\".";
3✔
1141
                    $this->msgCache->add($message, MessageCollector::ERROR);
3✔
1142
                } else {
1✔
1143
                    $this->ruleset[$code]['type'] = $type;
6✔
1144
                    if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1145
                        echo str_repeat("\t", $depth);
×
1146
                        echo "\t\t=> message type set to ".(string) $rule->type;
×
1147
                        if ($code !== $ref) {
×
1148
                            echo " for $code";
×
1149
                        }
1150

1151
                        echo PHP_EOL;
×
1152
                    }
1153
                }
1154
            }//end if
3✔
1155

1156
            // Custom message.
1157
            if (isset($rule->message) === true
26✔
1158
                && $this->shouldProcessElement($rule->message) === true
26✔
1159
            ) {
9✔
1160
                if (isset($this->ruleset[$code]) === false) {
6✔
1161
                    $this->ruleset[$code] = [];
×
1162
                }
1163

1164
                $this->ruleset[$code]['message'] = (string) $rule->message;
6✔
1165
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1166
                    echo str_repeat("\t", $depth);
×
1167
                    echo "\t\t=> message set to ".(string) $rule->message;
×
1168
                    if ($code !== $ref) {
×
1169
                        echo " for $code";
×
1170
                    }
1171

1172
                    echo PHP_EOL;
×
1173
                }
1174
            }
2✔
1175

1176
            // Custom properties.
1177
            if (isset($rule->properties) === true
26✔
1178
                && $this->shouldProcessElement($rule->properties) === true
26✔
1179
            ) {
9✔
1180
                $propertyScope = 'standard';
20✔
1181
                if ($code === $ref || substr($ref, -9) === 'Sniff.php') {
20✔
1182
                    $propertyScope = 'sniff';
20✔
1183
                }
7✔
1184

1185
                foreach ($rule->properties->property as $prop) {
20✔
1186
                    if ($this->shouldProcessElement($prop) === false) {
20✔
1187
                        continue;
6✔
1188
                    }
1189

1190
                    if (isset($this->ruleset[$code]) === false) {
20✔
1191
                        $this->ruleset[$code] = [
20✔
1192
                            'properties' => [],
20✔
1193
                        ];
7✔
1194
                    } else if (isset($this->ruleset[$code]['properties']) === false) {
17✔
1195
                        $this->ruleset[$code]['properties'] = [];
3✔
1196
                    }
1✔
1197

1198
                    $name = (string) $prop['name'];
20✔
1199
                    if (isset($prop['type']) === true
20✔
1200
                        && (string) $prop['type'] === 'array'
20✔
1201
                    ) {
7✔
1202
                        $values = [];
15✔
1203
                        if (isset($prop['extend']) === true
15✔
1204
                            && (string) $prop['extend'] === 'true'
15✔
1205
                            && isset($this->ruleset[$code]['properties'][$name]['value']) === true
15✔
1206
                        ) {
5✔
1207
                            $values = $this->ruleset[$code]['properties'][$name]['value'];
12✔
1208
                        }
4✔
1209

1210
                        if (isset($prop->element) === true) {
15✔
1211
                            $printValue = '';
15✔
1212
                            foreach ($prop->element as $element) {
15✔
1213
                                if ($this->shouldProcessElement($element) === false) {
15✔
1214
                                    continue;
6✔
1215
                                }
1216

1217
                                $value = (string) $element['value'];
15✔
1218
                                if (isset($element['key']) === true) {
15✔
1219
                                    $key          = (string) $element['key'];
6✔
1220
                                    $values[$key] = $value;
6✔
1221
                                    $printValue  .= $key.'=>'.$value.',';
6✔
1222
                                } else {
2✔
1223
                                    $values[]    = $value;
15✔
1224
                                    $printValue .= $value.',';
15✔
1225
                                }
1226
                            }
5✔
1227

1228
                            $printValue = rtrim($printValue, ',');
15✔
1229
                        } else if (isset($prop['value']) === true) {
9✔
1230
                            $message  = 'Passing an array of values to a property using a comma-separated string'.PHP_EOL;
6✔
1231
                            $message .= 'was deprecated in PHP_CodeSniffer 3.3.0. Support will be removed in PHPCS 4.0.0.'.PHP_EOL;
6✔
1232
                            $message .= "The deprecated syntax was used for property \"$name\"".PHP_EOL;
6✔
1233
                            $message .= "for sniff \"$code\".".PHP_EOL;
6✔
1234
                            $message .= 'Pass array values via <element [key="..." ]value="..."> nodes instead.';
6✔
1235
                            $this->msgCache->add($message, MessageCollector::DEPRECATED);
6✔
1236

1237
                            $value      = (string) $prop['value'];
6✔
1238
                            $printValue = $value;
6✔
1239
                            if ($value !== '') {
6✔
1240
                                foreach (explode(',', $value) as $val) {
6✔
1241
                                    list($k, $v) = explode('=>', $val.'=>');
6✔
1242
                                    if ($v !== '') {
6✔
1243
                                        $values[trim($k)] = trim($v);
6✔
1244
                                    } else {
2✔
1245
                                        $values[] = trim($k);
6✔
1246
                                    }
1247
                                }
2✔
1248
                            }
2✔
1249
                        }//end if
2✔
1250

1251
                        $this->ruleset[$code]['properties'][$name] = [
15✔
1252
                            'value' => $values,
15✔
1253
                            'scope' => $propertyScope,
15✔
1254
                        ];
5✔
1255
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
15✔
1256
                            echo str_repeat("\t", $depth);
×
1257
                            echo "\t\t=> array property \"$name\" set to \"$printValue\"";
×
1258
                            if ($code !== $ref) {
×
1259
                                echo " for $code";
×
1260
                            }
1261

1262
                            echo PHP_EOL;
5✔
1263
                        }
1264
                    } else {
5✔
1265
                        $this->ruleset[$code]['properties'][$name] = [
20✔
1266
                            'value' => (string) $prop['value'],
20✔
1267
                            'scope' => $propertyScope,
20✔
1268
                        ];
7✔
1269
                        if (PHP_CODESNIFFER_VERBOSITY > 1) {
20✔
1270
                            echo str_repeat("\t", $depth);
×
1271
                            echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"';
×
1272
                            if ($code !== $ref) {
×
1273
                                echo " for $code";
×
1274
                            }
1275

1276
                            echo PHP_EOL;
×
1277
                        }
1278
                    }//end if
1279
                }//end foreach
7✔
1280
            }//end if
7✔
1281

1282
            // Ignore patterns.
1283
            foreach ($rule->{'exclude-pattern'} as $pattern) {
26✔
1284
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1285
                    continue;
6✔
1286
                }
1287

1288
                if (isset($this->ignorePatterns[$code]) === false) {
6✔
1289
                    $this->ignorePatterns[$code] = [];
6✔
1290
                }
2✔
1291

1292
                if (isset($pattern['type']) === false) {
6✔
1293
                    $pattern['type'] = 'absolute';
6✔
1294
                }
2✔
1295

1296
                $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type'];
6✔
1297
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1298
                    echo str_repeat("\t", $depth);
×
1299
                    echo "\t\t=> added rule-specific ".(string) $pattern['type'].' ignore pattern';
×
1300
                    if ($code !== $ref) {
×
1301
                        echo " for $code";
×
1302
                    }
1303

1304
                    echo ': '.(string) $pattern.PHP_EOL;
×
1305
                }
1306
            }//end foreach
9✔
1307

1308
            // Include patterns.
1309
            foreach ($rule->{'include-pattern'} as $pattern) {
26✔
1310
                if ($this->shouldProcessElement($pattern) === false) {
6✔
1311
                    continue;
6✔
1312
                }
1313

1314
                if (isset($this->includePatterns[$code]) === false) {
6✔
1315
                    $this->includePatterns[$code] = [];
6✔
1316
                }
2✔
1317

1318
                if (isset($pattern['type']) === false) {
6✔
1319
                    $pattern['type'] = 'absolute';
6✔
1320
                }
2✔
1321

1322
                $this->includePatterns[$code][(string) $pattern] = (string) $pattern['type'];
6✔
1323
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
6✔
1324
                    echo str_repeat("\t", $depth);
×
1325
                    echo "\t\t=> added rule-specific ".(string) $pattern['type'].' include pattern';
×
1326
                    if ($code !== $ref) {
×
1327
                        echo " for $code";
×
1328
                    }
1329

1330
                    echo ': '.(string) $pattern.PHP_EOL;
×
1331
                }
1332
            }//end foreach
9✔
1333
        }//end foreach
9✔
1334

1335
    }//end processRule()
17✔
1336

1337

1338
    /**
1339
     * Determine if an element should be processed or ignored.
1340
     *
1341
     * @param \SimpleXMLElement $element An object from a ruleset XML file.
1342
     *
1343
     * @return bool
1344
     */
1345
    private function shouldProcessElement($element)
20✔
1346
    {
1347
        if (isset($element['phpcbf-only']) === false
20✔
1348
            && isset($element['phpcs-only']) === false
20✔
1349
        ) {
7✔
1350
            // No exceptions are being made.
1351
            return true;
20✔
1352
        }
1353

1354
        if (PHP_CODESNIFFER_CBF === true
12✔
1355
            && isset($element['phpcbf-only']) === true
12✔
1356
            && (string) $element['phpcbf-only'] === 'true'
12✔
1357
        ) {
4✔
1358
            return true;
6✔
1359
        }
1360

1361
        if (PHP_CODESNIFFER_CBF === false
12✔
1362
            && isset($element['phpcs-only']) === true
12✔
1363
            && (string) $element['phpcs-only'] === 'true'
12✔
1364
        ) {
4✔
1365
            return true;
6✔
1366
        }
1367

1368
        return false;
12✔
1369

1370
    }//end shouldProcessElement()
1371

1372

1373
    /**
1374
     * Loads and stores sniffs objects used for sniffing files.
1375
     *
1376
     * @param array $files        Paths to the sniff files to register.
1377
     * @param array $restrictions The sniff class names to restrict the allowed
1378
     *                            listeners to.
1379
     * @param array $exclusions   The sniff class names to exclude from the
1380
     *                            listeners list.
1381
     *
1382
     * @return void
1383
     */
1384
    public function registerSniffs($files, $restrictions, $exclusions)
44✔
1385
    {
1386
        $listeners = [];
44✔
1387

1388
        foreach ($files as $file) {
44✔
1389
            // Work out where the position of /StandardName/Sniffs/... is
1390
            // so we can determine what the class will be called.
1391
            $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR);
44✔
1392
            if ($sniffPos === false) {
44✔
1393
                continue;
3✔
1394
            }
1395

1396
            $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR);
44✔
1397
            if ($slashPos === false) {
44✔
1398
                continue;
×
1399
            }
1400

1401
            $className   = Autoload::loadFile($file);
44✔
1402
            $compareName = Common::cleanSniffClass($className);
44✔
1403

1404
            // If they have specified a list of sniffs to restrict to, check
1405
            // to see if this sniff is allowed.
1406
            if (empty($restrictions) === false
44✔
1407
                && isset($restrictions[$compareName]) === false
44✔
1408
            ) {
15✔
1409
                continue;
6✔
1410
            }
1411

1412
            // If they have specified a list of sniffs to exclude, check
1413
            // to see if this sniff is allowed.
1414
            if (empty($exclusions) === false
44✔
1415
                && isset($exclusions[$compareName]) === true
44✔
1416
            ) {
15✔
1417
                continue;
6✔
1418
            }
1419

1420
            // Skip abstract classes.
1421
            $reflection = new ReflectionClass($className);
44✔
1422
            if ($reflection->isAbstract() === true) {
44✔
1423
                continue;
3✔
1424
            }
1425

1426
            if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\Sniff') === false) {
44✔
1427
                $message  = 'All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL;
15✔
1428
                $message .= "Interface not implemented for sniff $className.".PHP_EOL;
15✔
1429
                $message .= 'Contact the sniff author to fix the sniff.';
15✔
1430
                $this->msgCache->add($message, MessageCollector::DEPRECATED);
15✔
1431

1432
                // Skip classes which don't implement the register() or process() methods.
1433
                if (method_exists($className, 'register') === false
15✔
1434
                    || method_exists($className, 'process') === false
15✔
1435
                ) {
5✔
1436
                    $errorMsg = 'Sniff class %s is missing required method %s().';
12✔
1437
                    if (method_exists($className, 'register') === false) {
12✔
1438
                        $this->msgCache->add(sprintf($errorMsg, $className, 'register'), MessageCollector::ERROR);
9✔
1439
                    }
3✔
1440

1441
                    if (method_exists($className, 'process') === false) {
12✔
1442
                        $this->msgCache->add(sprintf($errorMsg, $className, 'process'), MessageCollector::ERROR);
9✔
1443
                    }
3✔
1444

1445
                    continue;
12✔
1446
                }
1447
            }//end if
1✔
1448

1449
            $listeners[$className] = $className;
44✔
1450

1451
            if (PHP_CODESNIFFER_VERBOSITY > 2) {
44✔
1452
                echo "Registered $className".PHP_EOL;
×
1453
            }
1454
        }//end foreach
15✔
1455

1456
        $this->sniffs = $listeners;
44✔
1457

1458
    }//end registerSniffs()
29✔
1459

1460

1461
    /**
1462
     * Populates the array of PHP_CodeSniffer_Sniff objects for this file.
1463
     *
1464
     * @return void
1465
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If sniff registration fails.
1466
     */
1467
    public function populateTokenListeners()
29✔
1468
    {
1469
        // Construct a list of listeners indexed by token being listened for.
1470
        $this->tokenListeners = [];
29✔
1471

1472
        foreach ($this->sniffs as $sniffClass => $sniffObject) {
29✔
1473
            $this->sniffs[$sniffClass] = null;
29✔
1474
            $this->sniffs[$sniffClass] = new $sniffClass();
29✔
1475

1476
            $sniffCode = Common::getSniffCode($sniffClass);
29✔
1477

1478
            if (substr($sniffCode, 0, 1) === '.'
29✔
1479
                || substr($sniffCode, -1) === '.'
29✔
1480
                || strpos($sniffCode, '..') !== false
29✔
1481
                || preg_match('`(^|\.)Sniffs\.`', $sniffCode) === 1
29✔
1482
                || preg_match('`[^\s\.-]+\\\\Sniffs\\\\[^\s\.-]+\\\\[^\s\.-]+Sniff`', $sniffClass) !== 1
29✔
1483
            ) {
10✔
1484
                $message  = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.";
3✔
1485
                $message .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
3✔
1486
                $message .= 'Contact the sniff author to fix the sniff.';
3✔
1487
                $this->msgCache->add($message, MessageCollector::DEPRECATED);
3✔
1488
            }
1✔
1489

1490
            $this->sniffCodes[$sniffCode] = $sniffClass;
29✔
1491

1492
            $isDeprecated = false;
29✔
1493
            if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) {
29✔
1494
                $isDeprecated = true;
18✔
1495
                $this->deprecatedSniffs[$sniffCode] = $sniffClass;
18✔
1496
            }
6✔
1497

1498
            // Set custom properties.
1499
            if (isset($this->ruleset[$sniffCode]['properties']) === true) {
29✔
1500
                foreach ($this->ruleset[$sniffCode]['properties'] as $name => $settings) {
11✔
1501
                    $this->setSniffProperty($sniffClass, $name, $settings);
11✔
1502
                }
4✔
1503
            }
4✔
1504

1505
            $tokenizers = [];
29✔
1506
            $vars       = get_class_vars($sniffClass);
29✔
1507
            if (empty($vars['supportedTokenizers']) === false
29✔
1508
                && $isDeprecated === false
29✔
1509
                && in_array('PHP', $vars['supportedTokenizers'], true) === false
29✔
1510
            ) {
10✔
1511
                if (in_array('CSS', $vars['supportedTokenizers'], true) === true
15✔
1512
                    || in_array('JS', $vars['supportedTokenizers'], true) === true
15✔
1513
                ) {
5✔
1514
                    $message = 'Scanning CSS/JS files is deprecated and support will be removed in PHP_CodeSniffer 4.0.'.PHP_EOL;
15✔
1515
                } else {
5✔
1516
                    // Just in case someone has an integration with a custom tokenizer.
1517
                    $message = 'Support for custom tokenizers will be removed in PHP_CodeSniffer 4.0.'.PHP_EOL;
15✔
1518
                }
1519

1520
                $message .= 'The %s sniff is listening for %s.';
15✔
1521
                $message  = sprintf($message, $sniffCode, implode(', ', $vars['supportedTokenizers']));
15✔
1522
                $this->msgCache->add($message, MessageCollector::DEPRECATED);
15✔
1523
            }
5✔
1524

1525
            if (isset($vars['supportedTokenizers']) === true) {
29✔
1526
                foreach ($vars['supportedTokenizers'] as $tokenizer) {
24✔
1527
                    $tokenizers[$tokenizer] = $tokenizer;
24✔
1528
                }
8✔
1529
            } else {
8✔
1530
                $tokenizers = ['PHP' => 'PHP'];
11✔
1531
            }
1532

1533
            $tokens = $this->sniffs[$sniffClass]->register();
29✔
1534
            if (is_array($tokens) === false) {
29✔
1535
                $msg = "The sniff {$sniffClass}::register() method must return an array.";
3✔
1536
                $this->msgCache->add($msg, MessageCollector::ERROR);
3✔
1537

1538
                // Unregister the sniff.
1539
                unset($this->sniffs[$sniffClass], $this->sniffCodes[$sniffCode], $this->deprecatedSniffs[$sniffCode]);
3✔
1540
                continue;
3✔
1541
            }
1542

1543
            $ignorePatterns = [];
29✔
1544
            $patterns       = $this->getIgnorePatterns($sniffCode);
29✔
1545
            foreach ($patterns as $pattern => $type) {
29✔
1546
                $replacements = [
1✔
1547
                    '\\,' => ',',
3✔
1548
                    '*'   => '.*',
2✔
1549
                ];
2✔
1550

1551
                $ignorePatterns[] = strtr($pattern, $replacements);
3✔
1552
            }
10✔
1553

1554
            $includePatterns = [];
29✔
1555
            $patterns        = $this->getIncludePatterns($sniffCode);
29✔
1556
            foreach ($patterns as $pattern => $type) {
29✔
1557
                $replacements = [
1✔
1558
                    '\\,' => ',',
3✔
1559
                    '*'   => '.*',
2✔
1560
                ];
2✔
1561

1562
                $includePatterns[] = strtr($pattern, $replacements);
3✔
1563
            }
10✔
1564

1565
            foreach ($tokens as $token) {
29✔
1566
                if (isset($this->tokenListeners[$token]) === false) {
29✔
1567
                    $this->tokenListeners[$token] = [];
29✔
1568
                }
10✔
1569

1570
                if (isset($this->tokenListeners[$token][$sniffClass]) === false) {
29✔
1571
                    $this->tokenListeners[$token][$sniffClass] = [
29✔
1572
                        'class'      => $sniffClass,
29✔
1573
                        'source'     => $sniffCode,
29✔
1574
                        'tokenizers' => $tokenizers,
29✔
1575
                        'ignore'     => $ignorePatterns,
29✔
1576
                        'include'    => $includePatterns,
29✔
1577
                    ];
10✔
1578
                }
10✔
1579
            }
10✔
1580
        }//end foreach
10✔
1581

1582
    }//end populateTokenListeners()
19✔
1583

1584

1585
    /**
1586
     * Set a single property for a sniff.
1587
     *
1588
     * @param string $sniffClass The class name of the sniff.
1589
     * @param string $name       The name of the property to change.
1590
     * @param array  $settings   Array with the new value of the property and the scope of the property being set.
1591
     *
1592
     * @return void
1593
     *
1594
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When attempting to set a non-existent property on a sniff
1595
     *                                                      which doesn't declare the property or explicitly supports
1596
     *                                                      dynamic properties.
1597
     */
1598
    public function setSniffProperty($sniffClass, $name, $settings)
71✔
1599
    {
1600
        // Setting a property for a sniff we are not using.
1601
        if (isset($this->sniffs[$sniffClass]) === false) {
71✔
1602
            return;
3✔
1603
        }
1604

1605
        $name         = trim($name);
68✔
1606
        $propertyName = $name;
68✔
1607
        if (substr($propertyName, -2) === '[]') {
68✔
1608
            $propertyName = substr($propertyName, 0, -2);
3✔
1609
        }
1✔
1610

1611
        /*
1612
         * BC-compatibility layer for $settings using the pre-PHPCS 3.8.0 format.
1613
         *
1614
         * Prior to PHPCS 3.8.0, `$settings` was expected to only contain the new _value_
1615
         * for the property (which could be an array).
1616
         * Since PHPCS 3.8.0, `$settings` is expected to be an array with two keys: 'scope'
1617
         * and 'value', where 'scope' indicates whether the property should be set to the given 'value'
1618
         * for one individual sniff or for all sniffs in a standard.
1619
         *
1620
         * This BC-layer is only for integrations with PHPCS which may call this method directly
1621
         * and will be removed in PHPCS 4.0.0.
1622
         */
1623

1624
        if (is_array($settings) === false
68✔
1625
            || isset($settings['scope'], $settings['value']) === false
68✔
1626
        ) {
23✔
1627
            // This will be an "old" format value.
1628
            $settings = [
8✔
1629
                'value' => $settings,
24✔
1630
                'scope' => 'standard',
24✔
1631
            ];
16✔
1632

1633
            trigger_error(
24✔
1634
                __FUNCTION__.': the format of the $settings parameter has changed from (mixed) $value to array(\'scope\' => \'sniff|standard\', \'value\' => $value). Please update your integration code. See PR #3629 for more information.',
24✔
1635
                E_USER_DEPRECATED
16✔
1636
            );
16✔
1637
        }
7✔
1638

1639
        $isSettable  = false;
68✔
1640
        $sniffObject = $this->sniffs[$sniffClass];
68✔
1641
        if (property_exists($sniffObject, $propertyName) === true
68✔
1642
            || ($sniffObject instanceof stdClass) === true
39✔
1643
            || method_exists($sniffObject, '__set') === true
53✔
1644
        ) {
23✔
1645
            $isSettable = true;
56✔
1646
        }
19✔
1647

1648
        if ($isSettable === false) {
68✔
1649
            if ($settings['scope'] === 'sniff') {
18✔
1650
                $notice  = "Property \"$propertyName\" does not exist on sniff ";
6✔
1651
                $notice .= array_search($sniffClass, $this->sniffCodes, true).'.';
6✔
1652
                $this->msgCache->add($notice, MessageCollector::ERROR);
6✔
1653
            }
2✔
1654

1655
            return;
18✔
1656
        }
1657

1658
        $value = $settings['value'];
56✔
1659

1660
        if (is_string($value) === true) {
56✔
1661
            $value = trim($value);
56✔
1662
        }
19✔
1663

1664
        if ($value === '') {
56✔
1665
            $value = null;
9✔
1666
        }
3✔
1667

1668
        // Special case for booleans.
1669
        if ($value === 'true') {
56✔
1670
            $value = true;
12✔
1671
        } else if ($value === 'false') {
56✔
1672
            $value = false;
12✔
1673
        } else if (substr($name, -2) === '[]') {
56✔
1674
            $name   = $propertyName;
3✔
1675
            $values = [];
3✔
1676
            if ($value !== null) {
3✔
1677
                foreach (explode(',', $value) as $val) {
3✔
1678
                    list($k, $v) = explode('=>', $val.'=>');
3✔
1679
                    if ($v !== '') {
3✔
1680
                        $values[trim($k)] = trim($v);
3✔
1681
                    } else {
1✔
1682
                        $values[] = trim($k);
3✔
1683
                    }
1684
                }
1✔
1685
            }
1✔
1686

1687
            $value = $values;
3✔
1688
        }
1✔
1689

1690
        $sniffObject->$name = $value;
56✔
1691

1692
    }//end setSniffProperty()
37✔
1693

1694

1695
    /**
1696
     * Gets the array of ignore patterns.
1697
     *
1698
     * Optionally takes a listener to get ignore patterns specified
1699
     * for that sniff only.
1700
     *
1701
     * @param string $listener The listener to get patterns for. If NULL, all
1702
     *                         patterns are returned.
1703
     *
1704
     * @return array
1705
     */
1706
    public function getIgnorePatterns($listener=null)
20✔
1707
    {
1708
        if ($listener === null) {
20✔
1709
            return $this->ignorePatterns;
3✔
1710
        }
1711

1712
        if (isset($this->ignorePatterns[$listener]) === true) {
17✔
1713
            return $this->ignorePatterns[$listener];
6✔
1714
        }
1715

1716
        return [];
11✔
1717

1718
    }//end getIgnorePatterns()
1719

1720

1721
    /**
1722
     * Gets the array of include patterns.
1723
     *
1724
     * Optionally takes a listener to get include patterns specified
1725
     * for that sniff only.
1726
     *
1727
     * @param string $listener The listener to get patterns for. If NULL, all
1728
     *                         patterns are returned.
1729
     *
1730
     * @return array
1731
     */
1732
    public function getIncludePatterns($listener=null)
20✔
1733
    {
1734
        if ($listener === null) {
20✔
1735
            return $this->includePatterns;
3✔
1736
        }
1737

1738
        if (isset($this->includePatterns[$listener]) === true) {
17✔
1739
            return $this->includePatterns[$listener];
6✔
1740
        }
1741

1742
        return [];
11✔
1743

1744
    }//end getIncludePatterns()
1745

1746

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