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

PHPCSStandards / PHP_CodeSniffer / 17662127818

12 Sep 2025 01:50AM UTC coverage: 78.786%. Remained the same
17662127818

push

github

web-flow
Merge pull request #1241 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-3

CS: normalize code style rules [3]

343 of 705 new or added lines in 108 files covered. (48.65%)

3 existing lines in 3 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

88.97
/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\Sniffs\Sniff;
18
use PHP_CodeSniffer\Util\Common;
19
use PHP_CodeSniffer\Util\MessageCollector;
20
use PHP_CodeSniffer\Util\Standards;
21
use PHP_CodeSniffer\Util\Writers\StatusWriter;
22
use RecursiveDirectoryIterator;
23
use RecursiveIteratorIterator;
24
use ReflectionClass;
25
use SimpleXMLElement;
26
use stdClass;
27

28
class Ruleset
29
{
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

169

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

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

200
            $standardPaths[] = $standard;
44✔
201
        }
202

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

211
                $this->name .= $standardName;
44✔
212

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

220
                Autoload::addSearchPath(dirname($standard), $namespace);
44✔
221
            }
222

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

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

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

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

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

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

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

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

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

267
        $this->displayCachedMessages();
44✔
268

269
    }//end __construct()
14✔
270

271

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

282
        $sniffCount = count($sniffs);
15✔
283

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

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

293
        echo $summaryLine;
15✔
294

295
        $lastStandard     = null;
15✔
296
        $lastCount        = 0;
15✔
297
        $sniffsInStandard = [];
15✔
298

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

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

317
                $subTitle .= ')';
15✔
318

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

323
                $lastStandard     = $currentStandard;
15✔
324
                $lastCount        = 0;
15✔
325
                $sniffsInStandard = [];
15✔
326

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

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

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

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

344
    }//end explain()
5✔
345

346

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

356
    }//end hasSniffDeprecations()
357

358

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

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

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

389
        ksort($this->deprecatedSniffs, (SORT_NATURAL | SORT_FLAG_CASE));
33✔
390

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

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

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

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

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

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

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

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

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

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

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

452
            $messages[] = $message;
18✔
453
        }//end foreach
454

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

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

465
        $maxActualWidth = max($maxActualWidth, min(strlen($summaryLine), $maxMessageWidth));
18✔
466

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

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

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

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

486
    }//end showSniffDeprecations()
6✔
487

488

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

513
        $this->msgCache->display();
41✔
514

515
    }//end displayCachedMessages()
8✔
516

517

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

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

548
            libxml_clear_errors();
15✔
549
            throw new RuntimeException($errorMsg);
15✔
550
        }
551

552
        libxml_use_internal_errors(false);
55✔
553

554
        $ownSniffs      = [];
55✔
555
        $includedSniffs = [];
55✔
556
        $excludedSniffs = [];
55✔
557

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

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

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

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

577
            $autoloadPath = (string) $autoload;
9✔
578

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

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

588
            include_once $autoloadPath;
6✔
589

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

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

604
            $name = (string) $config['name'];
15✔
605

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

614
                continue;
3✔
615
            }
616

617
            $this->configDirectivesApplied[$name] = $depth;
15✔
618

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

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

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

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

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

656
                    continue;
3✔
657
                }
658

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

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

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

685
                        continue;
3✔
686
                    }
687

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

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

698
                $argString = '-' . $cleanedValue;
9✔
699
            }//end if
700

701
            $cliArgs[] = $argString;
12✔
702

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

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

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

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

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

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

763
                        continue;
3✔
764
                    }
765

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

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

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

791
            $this->processRule($rule, $newSniffs, $depth);
52✔
792
        }//end foreach
793

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

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

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

813
            $cliArgs[] = '-d';
23✔
814
            $cliArgs[] = $argString;
23✔
815

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

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

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

842
            $this->config->setCommandLineValues($cliArgs);
35✔
843

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

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

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

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

865
        $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs));
44✔
866
        $excludedSniffs = array_unique($excludedSniffs);
44✔
867

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

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

885
        return $files;
44✔
886

887
    }//end processRuleset()
888

889

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

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

906
        $dirLen = strlen($directory);
6✔
907

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

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

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

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

927
            $path = $file->getPathname();
6✔
928

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

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

941
            $sniffs[] = $path;
6✔
942
        }//end foreach
943

944
        return $sniffs;
6✔
945

946
    }//end expandSniffDirectory()
947

948

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

969
            return [];
3✔
970
        }
971

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

979
            return [];
3✔
980
        }
981

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

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

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

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

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

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

NEW
1077
                        $newRef = Common::realpath($dir . $path);
×
1078

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

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

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

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

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

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

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

1129
    }//end expandRulesetReference()
1130

1131

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

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

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

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

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

1186
                    StatusWriter::write($statusMessage, ($depth + 2));
×
1187
                }
1188
            }
1189

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

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

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

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

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

1230
                    StatusWriter::write($statusMessage, ($depth + 2));
×
1231
                }
1232
            }
1233

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

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

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

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

1268
                            continue;
×
1269
                        }
1270

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

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

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

1300
                            $printValue = rtrim($printValue, ',');
12✔
1301
                        }
1302

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

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

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

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

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

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

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

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

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

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

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

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

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

1384
    }//end processRule()
8✔
1385

1386

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

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

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

1417
        return false;
12✔
1418

1419
    }//end shouldProcessElement()
1420

1421

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

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

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

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

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

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

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

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

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

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

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

1504
            $listeners[$className] = $className;
47✔
1505

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

1511
        $this->sniffs = $listeners;
47✔
1512

1513
    }//end registerSniffs()
16✔
1514

1515

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1602
    }//end populateTokenListeners()
5✔
1603

1604

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

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

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

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

1648
            return;
18✔
1649
        }
1650

1651
        $value = $settings['value'];
29✔
1652

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

1667
            $value = $this->getRealPropertyValue($values);
3✔
1668
        } else {
1669
            $value = $this->getRealPropertyValue($value);
29✔
1670
        }
1671

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

1683
    }//end setSniffProperty()
10✔
1684

1685

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

1700
            return $value;
9✔
1701
        }
1702

1703
        if (is_string($value) === true) {
14✔
1704
            $value = trim($value);
14✔
1705

1706
            if ($value === '') {
14✔
1707
                return null;
6✔
1708
            }
1709

1710
            $valueLc = strtolower($value);
14✔
1711

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

1716
            if ($valueLc === 'false') {
14✔
1717
                return false;
9✔
1718
            }
1719

1720
            if ($valueLc === 'null') {
14✔
1721
                return null;
6✔
1722
            }
1723
        }//end if
1724

1725
        return $value;
14✔
1726

1727
    }//end getRealPropertyValue()
1728

1729

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

1747
        if (isset($this->ignorePatterns[$listener]) === true) {
17✔
1748
            return $this->ignorePatterns[$listener];
6✔
1749
        }
1750

1751
        return [];
11✔
1752

1753
    }//end getIgnorePatterns()
1754

1755

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

1773
        if (isset($this->includePatterns[$listener]) === true) {
17✔
1774
            return $this->includePatterns[$listener];
6✔
1775
        }
1776

1777
        return [];
11✔
1778

1779
    }//end getIncludePatterns()
1780

1781

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