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

PHPCSStandards / PHP_CodeSniffer / 15253296250

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

Pull #1105

github

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

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

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

38.19
/src/Config.php
1
<?php
2
/**
3
 * Stores the configuration used to run PHPCS and PHPCBF.
4
 *
5
 * Parses the command line to determine user supplied values
6
 * and provides functions to access data stored in config files.
7
 *
8
 * @author    Greg Sherwood <gsherwood@squiz.net>
9
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
10
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
11
 */
12

13
namespace PHP_CodeSniffer;
14

15
use Exception;
16
use Phar;
17
use PHP_CodeSniffer\Exceptions\DeepExitException;
18
use PHP_CodeSniffer\Exceptions\RuntimeException;
19
use PHP_CodeSniffer\Util\Common;
20
use PHP_CodeSniffer\Util\ExitCode;
21
use PHP_CodeSniffer\Util\Help;
22
use PHP_CodeSniffer\Util\Standards;
23

24
/**
25
 * Stores the configuration used to run PHPCS and PHPCBF.
26
 *
27
 * @property string[]   $files           The files and directories to check.
28
 * @property string[]   $standards       The standards being used for checking.
29
 * @property int        $verbosity       How verbose the output should be.
30
 *                                       0: no unnecessary output
31
 *                                       1: basic output for files being checked
32
 *                                       2: ruleset and file parsing output
33
 *                                       3: sniff execution output
34
 * @property bool       $interactive     Enable interactive checking mode.
35
 * @property int        $parallel        Check files in parallel.
36
 * @property bool       $cache           Enable the use of the file cache.
37
 * @property string     $cacheFile       Path to the file where the cache data should be written
38
 * @property bool       $colors          Display colours in output.
39
 * @property bool       $explain         Explain the coding standards.
40
 * @property bool       $local           Process local files in directories only (no recursion).
41
 * @property bool       $showSources     Show sniff source codes in report output.
42
 * @property bool       $showProgress    Show basic progress information while running.
43
 * @property bool       $quiet           Quiet mode; disables progress and verbose output.
44
 * @property bool       $annotations     Process phpcs: annotations.
45
 * @property int        $tabWidth        How many spaces each tab is worth.
46
 * @property string     $encoding        The encoding of the files being checked.
47
 * @property string[]   $sniffs          The sniffs that should be used for checking.
48
 *                                       If empty, all sniffs in the supplied standards will be used.
49
 * @property string[]   $exclude         The sniffs that should be excluded from checking.
50
 *                                       If empty, all sniffs in the supplied standards will be used.
51
 * @property string[]   $ignored         Regular expressions used to ignore files and folders during checking.
52
 * @property string     $reportFile      A file where the report output should be written.
53
 * @property string     $generator       The documentation generator to use.
54
 * @property string     $filter          The filter to use for the run.
55
 * @property string[]   $bootstrap       One of more files to include before the run begins.
56
 * @property int|string $reportWidth     The maximum number of columns that reports should use for output.
57
 *                                       Set to "auto" for have this value changed to the width of the terminal.
58
 * @property int        $errorSeverity   The minimum severity an error must have to be displayed.
59
 * @property int        $warningSeverity The minimum severity a warning must have to be displayed.
60
 * @property bool       $recordErrors    Record the content of error messages as well as error counts.
61
 * @property string     $suffix          A suffix to add to fixed files.
62
 * @property string     $basepath        A file system location to strip from the paths of files shown in reports.
63
 * @property bool       $stdin           Read content from STDIN instead of supplied files.
64
 * @property string     $stdinContent    Content passed directly to PHPCS on STDIN.
65
 * @property string     $stdinPath       The path to use for content passed on STDIN.
66
 * @property bool       $trackTime       Whether or not to track sniff run time.
67
 *
68
 * @property array<string, string>      $extensions File extensions that should be checked, and what tokenizer is used.
69
 *                                                  E.g., array('inc' => 'PHP');
70
 *                                                  Note: since PHPCS 4.0.0, the tokenizer used will always be 'PHP',
71
 *                                                  but the array format of the property has not been changed to prevent
72
 *                                                  breaking integrations which may be accessing this property.
73
 * @property array<string, string|null> $reports    The reports to use for printing output after the run.
74
 *                                                  The format of the array is:
75
 *                                                      array(
76
 *                                                          'reportName1' => 'outputFile',
77
 *                                                          'reportName2' => null,
78
 *                                                      );
79
 *                                                  If the array value is NULL, the report will be written to the screen.
80
 *
81
 * @property string[] $unknown Any arguments gathered on the command line that are unknown to us.
82
 *                             E.g., using `phpcs -c` will give array('c');
83
 */
84
class Config
85
{
86

87
    /**
88
     * The current version.
89
     *
90
     * @var string
91
     */
92
    public const VERSION = '4.0.0';
93

94
    /**
95
     * Package stability; either stable, beta or alpha.
96
     *
97
     * @var string
98
     */
99
    public const STABILITY = 'beta';
100

101
    /**
102
     * Default report width when no report width is provided and 'auto' does not yield a valid width.
103
     *
104
     * @var int
105
     */
106
    public const DEFAULT_REPORT_WIDTH = 80;
107

108
    /**
109
     * Translation table for config settings which can be changed via multiple CLI flags.
110
     *
111
     * If the flag name matches the setting name, there is no need to add it to this translation table.
112
     * Similarly, if there is only one flag which can change a setting, there is no need to include
113
     * it in this table, even if the flag name and the setting name don't match.
114
     *
115
     * @var array<string, string> Key is the CLI flag name, value the corresponding config setting name.
116
     */
117
    public const CLI_FLAGS_TO_SETTING_NAME = [
118
        'n'                => 'warningSeverity',
119
        'w'                => 'warningSeverity',
120
        'warning-severity' => 'warningSeverity',
121
        'no-colors'        => 'colors',
122
        'no-cache'         => 'cache',
123
    ];
124

125
    /**
126
     * An array of settings that PHPCS and PHPCBF accept.
127
     *
128
     * This array is not meant to be accessed directly. Instead, use the settings
129
     * as if they are class member vars so the __get() and __set() magic methods
130
     * can be used to validate the values. For example, to set the verbosity level to
131
     * level 2, use $this->verbosity = 2; instead of accessing this property directly.
132
     *
133
     * Each of these settings is described in the class comment property list.
134
     *
135
     * @var array<string, mixed>
136
     */
137
    private $settings = [
138
        'files'           => null,
139
        'standards'       => null,
140
        'verbosity'       => null,
141
        'interactive'     => null,
142
        'parallel'        => null,
143
        'cache'           => null,
144
        'cacheFile'       => null,
145
        'colors'          => null,
146
        'explain'         => null,
147
        'local'           => null,
148
        'showSources'     => null,
149
        'showProgress'    => null,
150
        'quiet'           => null,
151
        'annotations'     => null,
152
        'tabWidth'        => null,
153
        'encoding'        => null,
154
        'extensions'      => null,
155
        'sniffs'          => null,
156
        'exclude'         => null,
157
        'ignored'         => null,
158
        'reportFile'      => null,
159
        'generator'       => null,
160
        'filter'          => null,
161
        'bootstrap'       => null,
162
        'reports'         => null,
163
        'basepath'        => null,
164
        'reportWidth'     => null,
165
        'errorSeverity'   => null,
166
        'warningSeverity' => null,
167
        'recordErrors'    => null,
168
        'suffix'          => null,
169
        'stdin'           => null,
170
        'stdinContent'    => null,
171
        'stdinPath'       => null,
172
        'trackTime'       => null,
173
        'unknown'         => null,
174
    ];
175

176
    /**
177
     * Whether or not to kill the process when an unknown command line arg is found.
178
     *
179
     * If FALSE, arguments that are not command line options or file/directory paths
180
     * will be ignored and execution will continue. These values will be stored in
181
     * $this->unknown.
182
     *
183
     * @var boolean
184
     */
185
    public $dieOnUnknownArg;
186

187
    /**
188
     * The current command line arguments we are processing.
189
     *
190
     * @var string[]
191
     */
192
    private $cliArgs = [];
193

194
    /**
195
     * A list of valid generators.
196
     *
197
     * {@internal Once support for PHP < 5.6 is dropped, this property should be refactored into a
198
     * class constant.}
199
     *
200
     * @var array<string, string> Keys are the lowercase version of the generator name, while values
201
     *                            are the associated PHP generator class.
202
     */
203
    private $validGenerators = [
204
        'text'     => 'Text',
205
        'html'     => 'HTML',
206
        'markdown' => 'Markdown',
207
    ];
208

209
    /**
210
     * Command line values that the user has supplied directly.
211
     *
212
     * @var array<string, true|array<string, true>>
213
     */
214
    private $overriddenDefaults = [];
215

216
    /**
217
     * Config file data that has been loaded for the run.
218
     *
219
     * @var array<string, string>
220
     */
221
    private static $configData = null;
222

223
    /**
224
     * The full path to the config data file that has been loaded.
225
     *
226
     * @var string
227
     */
228
    private static $configDataFile = null;
229

230
    /**
231
     * Automatically discovered executable utility paths.
232
     *
233
     * @var array<string, string>
234
     */
235
    private static $executablePaths = [];
236

237

238
    /**
239
     * Get the value of an inaccessible property.
240
     *
241
     * @param string $name The name of the property.
242
     *
243
     * @return mixed
244
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the setting name is invalid.
245
     */
246
    public function __get($name)
48✔
247
    {
248
        if (array_key_exists($name, $this->settings) === false) {
48✔
249
            throw new RuntimeException("ERROR: unable to get value of property \"$name\"");
×
250
        }
251

252
        // Figure out what the terminal width needs to be for "auto".
253
        if ($name === 'reportWidth' && $this->settings[$name] === 'auto') {
48✔
254
            if (function_exists('shell_exec') === true) {
9✔
255
                $dimensions = shell_exec('stty size 2>&1');
9✔
256
                if (is_string($dimensions) === true && preg_match('|\d+ (\d+)|', $dimensions, $matches) === 1) {
9✔
257
                    $this->settings[$name] = (int) $matches[1];
×
258
                }
259
            }
260

261
            if ($this->settings[$name] === 'auto') {
9✔
262
                // If shell_exec wasn't available or didn't yield a usable value, set to the default.
263
                // This will prevent subsequent retrievals of the reportWidth from making another call to stty.
264
                $this->settings[$name] = self::DEFAULT_REPORT_WIDTH;
9✔
265
            }
266
        }
267

268
        return $this->settings[$name];
48✔
269

270
    }//end __get()
271

272

273
    /**
274
     * Set the value of an inaccessible property.
275
     *
276
     * @param string $name  The name of the property.
277
     * @param mixed  $value The value of the property.
278
     *
279
     * @return void
280
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the setting name is invalid.
281
     */
282
    public function __set($name, $value)
48✔
283
    {
284
        if (array_key_exists($name, $this->settings) === false) {
48✔
285
            throw new RuntimeException("Can't __set() $name; setting doesn't exist");
×
286
        }
287

288
        switch ($name) {
16✔
289
        case 'reportWidth' :
48✔
290
            if (is_string($value) === true && $value === 'auto') {
48✔
291
                // Nothing to do. Leave at 'auto'.
292
                break;
48✔
293
            }
294

295
            if (is_int($value) === true) {
39✔
296
                $value = abs($value);
6✔
297
            } else if (is_string($value) === true && preg_match('`^\d+$`', $value) === 1) {
33✔
298
                $value = (int) $value;
15✔
299
            } else {
300
                $value = self::DEFAULT_REPORT_WIDTH;
18✔
301
            }
302
            break;
39✔
303

304
        case 'standards' :
48✔
305
            $cleaned = [];
48✔
306

307
            // Check if the standard name is valid, or if the case is invalid.
308
            $installedStandards = Standards::getInstalledStandards();
48✔
309
            foreach ($value as $standard) {
48✔
310
                foreach ($installedStandards as $validStandard) {
48✔
311
                    if (strtolower($standard) === strtolower($validStandard)) {
48✔
312
                        $standard = $validStandard;
48✔
313
                        break;
48✔
314
                    }
315
                }
316

317
                $cleaned[] = $standard;
48✔
318
            }
319

320
            $value = $cleaned;
48✔
321
            break;
48✔
322

323
        // Only track time when explicitly needed.
324
        case 'verbosity':
48✔
325
            if ($value > 2) {
48✔
326
                $this->settings['trackTime'] = true;
×
327
            }
328
            break;
48✔
329
        case 'reports':
48✔
330
            $reports = array_change_key_case($value, CASE_LOWER);
48✔
331
            if (array_key_exists('performance', $reports) === true) {
48✔
332
                $this->settings['trackTime'] = true;
×
333
            }
334
            break;
48✔
335

336
        default :
337
            // No validation required.
338
            break;
48✔
339
        }//end switch
340

341
        $this->settings[$name] = $value;
48✔
342

343
    }//end __set()
16✔
344

345

346
    /**
347
     * Check if the value of an inaccessible property is set.
348
     *
349
     * @param string $name The name of the property.
350
     *
351
     * @return bool
352
     */
353
    public function __isset($name)
×
354
    {
355
        return isset($this->settings[$name]);
×
356

357
    }//end __isset()
358

359

360
    /**
361
     * Unset the value of an inaccessible property.
362
     *
363
     * @param string $name The name of the property.
364
     *
365
     * @return void
366
     */
367
    public function __unset($name)
×
368
    {
369
        $this->settings[$name] = null;
×
370

371
    }//end __unset()
372

373

374
    /**
375
     * Get the array of all config settings.
376
     *
377
     * @return array<string, mixed>
378
     */
379
    public function getSettings()
×
380
    {
381
        return $this->settings;
×
382

383
    }//end getSettings()
384

385

386
    /**
387
     * Set the array of all config settings.
388
     *
389
     * @param array<string, mixed> $settings The array of config settings.
390
     *
391
     * @return void
392
     */
393
    public function setSettings($settings)
×
394
    {
395
        $this->settings = $settings;
×
396

397
    }//end setSettings()
398

399

400
    /**
401
     * Creates a Config object and populates it with command line values.
402
     *
403
     * @param array $cliArgs         An array of values gathered from CLI args.
404
     * @param bool  $dieOnUnknownArg Whether or not to kill the process when an
405
     *                               unknown command line arg is found.
406
     *
407
     * @return void
408
     */
409
    public function __construct(array $cliArgs=[], $dieOnUnknownArg=true)
×
410
    {
411
        if (defined('PHP_CODESNIFFER_IN_TESTS') === true) {
×
412
            // Let everything through during testing so that we can
413
            // make use of PHPUnit command line arguments as well.
414
            $this->dieOnUnknownArg = false;
×
415
        } else {
416
            $this->dieOnUnknownArg = $dieOnUnknownArg;
×
417
        }
418

419
        if (empty($cliArgs) === true) {
×
420
            $cliArgs = $_SERVER['argv'];
×
421
            array_shift($cliArgs);
×
422
        }
423

424
        $this->restoreDefaults();
×
425
        $this->setCommandLineValues($cliArgs);
×
426

427
        if (isset($this->overriddenDefaults['standards']) === false) {
×
428
            // They did not supply a standard to use.
429
            // Look for a default ruleset in the current directory or higher.
430
            $currentDir = getcwd();
×
431

432
            $defaultFiles = [
433
                '.phpcs.xml',
×
434
                'phpcs.xml',
435
                '.phpcs.xml.dist',
436
                'phpcs.xml.dist',
437
            ];
438

439
            do {
440
                foreach ($defaultFiles as $defaultFilename) {
×
441
                    $default = $currentDir.DIRECTORY_SEPARATOR.$defaultFilename;
×
442
                    if (is_file($default) === true) {
×
443
                        $this->standards = [$default];
×
444
                        break(2);
×
445
                    }
446
                }
447

448
                $lastDir    = $currentDir;
×
449
                $currentDir = dirname($currentDir);
×
450
            } while ($currentDir !== '.' && $currentDir !== $lastDir && Common::isReadable($currentDir) === true);
×
451
        }//end if
452

453
        if (defined('STDIN') === false
×
454
            || PHP_OS_FAMILY === 'Windows'
×
455
        ) {
456
            return;
×
457
        }
458

459
        $handle = fopen('php://stdin', 'r');
×
460

461
        // Check for content on STDIN.
462
        if ($this->stdin === true
×
463
            || (Common::isStdinATTY() === false
×
464
            && feof($handle) === false)
×
465
        ) {
466
            $readStreams = [$handle];
×
467
            $writeSteams = null;
×
468

469
            $fileContents = '';
×
470
            while (is_resource($handle) === true && feof($handle) === false) {
×
471
                // Set a timeout of 200ms.
472
                if (stream_select($readStreams, $writeSteams, $writeSteams, 0, 200000) === 0) {
×
473
                    break;
×
474
                }
475

476
                $fileContents .= fgets($handle);
×
477
            }
478

479
            if (trim($fileContents) !== '') {
×
480
                $this->stdin        = true;
×
481
                $this->stdinContent = $fileContents;
×
482
                $this->overriddenDefaults['stdin']        = true;
×
483
                $this->overriddenDefaults['stdinContent'] = true;
×
484
            }
485
        }//end if
486

487
        fclose($handle);
×
488

489
    }//end __construct()
490

491

492
    /**
493
     * Set the command line values.
494
     *
495
     * @param array $args An array of command line arguments to set.
496
     *
497
     * @return void
498
     */
499
    public function setCommandLineValues($args)
×
500
    {
501
        $this->cliArgs = $args;
×
502
        $numArgs       = count($args);
×
503

504
        for ($i = 0; $i < $numArgs; $i++) {
×
505
            $arg = $this->cliArgs[$i];
×
506
            if ($arg === '') {
×
507
                continue;
×
508
            }
509

510
            if ($arg[0] === '-') {
×
511
                if ($arg === '-') {
×
512
                    // Asking to read from STDIN.
513
                    $this->stdin = true;
×
514
                    $this->overriddenDefaults['stdin'] = true;
×
515
                    continue;
×
516
                }
517

518
                if ($arg === '--') {
×
519
                    // Empty argument, ignore it.
520
                    continue;
×
521
                }
522

523
                if ($arg[1] === '-') {
×
524
                    $this->processLongArgument(substr($arg, 2), $i);
×
525
                } else {
526
                    $switches = str_split($arg);
×
527
                    foreach ($switches as $switch) {
×
528
                        if ($switch === '-') {
×
529
                            continue;
×
530
                        }
531

532
                        $this->processShortArgument($switch, $i);
×
533
                    }
534
                }
535
            } else {
536
                $this->processUnknownArgument($arg, $i);
×
537
            }//end if
538
        }//end for
539

540
    }//end setCommandLineValues()
541

542

543
    /**
544
     * Restore default values for all possible command line arguments.
545
     *
546
     * @return void
547
     */
548
    public function restoreDefaults()
9✔
549
    {
550
        $this->files           = [];
9✔
551
        $this->standards       = ['PSR12'];
9✔
552
        $this->verbosity       = 0;
9✔
553
        $this->interactive     = false;
9✔
554
        $this->cache           = false;
9✔
555
        $this->cacheFile       = null;
9✔
556
        $this->colors          = false;
9✔
557
        $this->explain         = false;
9✔
558
        $this->local           = false;
9✔
559
        $this->showSources     = false;
9✔
560
        $this->showProgress    = false;
9✔
561
        $this->quiet           = false;
9✔
562
        $this->annotations     = true;
9✔
563
        $this->parallel        = 1;
9✔
564
        $this->tabWidth        = 0;
9✔
565
        $this->encoding        = 'utf-8';
9✔
566
        $this->extensions      = [
9✔
567
            'php' => 'PHP',
6✔
568
            'inc' => 'PHP',
6✔
569
        ];
6✔
570
        $this->sniffs          = [];
9✔
571
        $this->exclude         = [];
9✔
572
        $this->ignored         = [];
9✔
573
        $this->reportFile      = null;
9✔
574
        $this->generator       = null;
9✔
575
        $this->filter          = null;
9✔
576
        $this->bootstrap       = [];
9✔
577
        $this->basepath        = null;
9✔
578
        $this->reports         = ['full' => null];
9✔
579
        $this->reportWidth     = 'auto';
9✔
580
        $this->errorSeverity   = 5;
9✔
581
        $this->warningSeverity = 5;
9✔
582
        $this->recordErrors    = true;
9✔
583
        $this->suffix          = '';
9✔
584
        $this->stdin           = false;
9✔
585
        $this->stdinContent    = null;
9✔
586
        $this->stdinPath       = null;
9✔
587
        $this->trackTime       = false;
9✔
588
        $this->unknown         = [];
9✔
589

590
        $standard = self::getConfigData('default_standard');
9✔
591
        if ($standard !== null) {
9✔
592
            $this->standards = explode(',', $standard);
6✔
593
        }
594

595
        $reportFormat = self::getConfigData('report_format');
9✔
596
        if ($reportFormat !== null) {
9✔
597
            $this->reports = [$reportFormat => null];
×
598
        }
599

600
        $tabWidth = self::getConfigData('tab_width');
9✔
601
        if ($tabWidth !== null) {
9✔
602
            $this->tabWidth = (int) $tabWidth;
×
603
        }
604

605
        $encoding = self::getConfigData('encoding');
9✔
606
        if ($encoding !== null) {
9✔
607
            $this->encoding = strtolower($encoding);
×
608
        }
609

610
        $severity = self::getConfigData('severity');
9✔
611
        if ($severity !== null) {
9✔
612
            $this->errorSeverity   = (int) $severity;
×
613
            $this->warningSeverity = (int) $severity;
×
614
        }
615

616
        $severity = self::getConfigData('error_severity');
9✔
617
        if ($severity !== null) {
9✔
618
            $this->errorSeverity = (int) $severity;
×
619
        }
620

621
        $severity = self::getConfigData('warning_severity');
9✔
622
        if ($severity !== null) {
9✔
623
            $this->warningSeverity = (int) $severity;
×
624
        }
625

626
        $showWarnings = self::getConfigData('show_warnings');
9✔
627
        if ($showWarnings !== null) {
9✔
628
            $showWarnings = (bool) $showWarnings;
3✔
629
            if ($showWarnings === false) {
3✔
630
                $this->warningSeverity = 0;
3✔
631
            }
632
        }
633

634
        $reportWidth = self::getConfigData('report_width');
9✔
635
        if ($reportWidth !== null) {
9✔
636
            $this->reportWidth = $reportWidth;
3✔
637
        }
638

639
        $showProgress = self::getConfigData('show_progress');
9✔
640
        if ($showProgress !== null) {
9✔
641
            $this->showProgress = (bool) $showProgress;
×
642
        }
643

644
        $quiet = self::getConfigData('quiet');
9✔
645
        if ($quiet !== null) {
9✔
646
            $this->quiet = (bool) $quiet;
×
647
        }
648

649
        $colors = self::getConfigData('colors');
9✔
650
        if ($colors !== null) {
9✔
651
            $this->colors = (bool) $colors;
×
652
        }
653

654
        $cache = self::getConfigData('cache');
9✔
655
        if ($cache !== null) {
9✔
656
            $this->cache = (bool) $cache;
×
657
        }
658

659
        $parallel = self::getConfigData('parallel');
9✔
660
        if ($parallel !== null) {
9✔
661
            $this->parallel = max((int) $parallel, 1);
×
662
        }
663

664
    }//end restoreDefaults()
3✔
665

666

667
    /**
668
     * Processes a short (-e) command line argument.
669
     *
670
     * @param string $arg The command line argument.
671
     * @param int    $pos The position of the argument on the command line.
672
     *
673
     * @return void
674
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
675
     */
676
    public function processShortArgument($arg, $pos)
30✔
677
    {
678
        switch ($arg) {
9✔
679
        case 'h':
30✔
680
        case '?':
30✔
681
            $this->printUsage();
×
682
            throw new DeepExitException('', ExitCode::OKAY);
×
683
        case 'i' :
30✔
684
            $output = Standards::prepareInstalledStandardsForDisplay().PHP_EOL;
×
685
            throw new DeepExitException($output, ExitCode::OKAY);
×
686
        case 'v' :
30✔
687
            if ($this->quiet === true) {
×
688
                // Ignore when quiet mode is enabled.
689
                break;
×
690
            }
691

692
            $this->verbosity++;
×
693
            $this->overriddenDefaults['verbosity'] = true;
×
694
            break;
×
695
        case 'l' :
30✔
696
            $this->local = true;
×
697
            $this->overriddenDefaults['local'] = true;
×
698
            break;
×
699
        case 's' :
30✔
700
            $this->showSources = true;
×
701
            $this->overriddenDefaults['showSources'] = true;
×
702
            break;
×
703
        case 'a' :
30✔
704
            $this->interactive = true;
×
705
            $this->overriddenDefaults['interactive'] = true;
×
706
            break;
×
707
        case 'e':
30✔
708
            $this->explain = true;
×
709
            $this->overriddenDefaults['explain'] = true;
×
710
            break;
×
711
        case 'p' :
30✔
712
            if ($this->quiet === true) {
×
713
                // Ignore when quiet mode is enabled.
714
                break;
×
715
            }
716

717
            $this->showProgress = true;
×
718
            $this->overriddenDefaults['showProgress'] = true;
×
719
            break;
×
720
        case 'q' :
30✔
721
            // Quiet mode disables a few other settings as well.
722
            $this->quiet        = true;
×
723
            $this->showProgress = false;
×
724
            $this->verbosity    = 0;
×
725

726
            $this->overriddenDefaults['quiet'] = true;
×
727
            break;
×
728
        case 'm' :
30✔
729
            $this->recordErrors = false;
×
730
            $this->overriddenDefaults['recordErrors'] = true;
×
731
            break;
×
732
        case 'd' :
30✔
733
            $ini = explode('=', $this->cliArgs[($pos + 1)]);
30✔
734
            $this->cliArgs[($pos + 1)] = '';
30✔
735
            if (isset($ini[1]) === false) {
30✔
736
                // Set to true.
737
                $ini[1] = '1';
3✔
738
            }
739

740
            $current = ini_get($ini[0]);
30✔
741
            if ($current === false) {
30✔
742
                // Ini setting which doesn't exist, or is from an unavailable extension.
743
                // Silently ignore it.
744
                break;
4✔
745
            }
746

747
            $changed = ini_set($ini[0], $ini[1]);
26✔
748
            if ($changed === false && ini_get($ini[0]) !== $ini[1]) {
26✔
749
                $error  = sprintf('ERROR: Ini option "%s" cannot be changed at runtime.', $ini[0]).PHP_EOL;
12✔
750
                $error .= $this->printShortUsage(true);
12✔
751
                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
12✔
752
            }
753
            break;
14✔
754
        case 'n' :
×
755
            if (isset($this->overriddenDefaults['warningSeverity']) === false) {
×
756
                $this->warningSeverity = 0;
×
757
                $this->overriddenDefaults['warningSeverity'] = true;
×
758
            }
759
            break;
×
760
        case 'w' :
×
761
            if (isset($this->overriddenDefaults['warningSeverity']) === false) {
×
762
                $this->warningSeverity = $this->errorSeverity;
×
763
                $this->overriddenDefaults['warningSeverity'] = true;
×
764
            }
765
            break;
×
766
        default:
767
            if ($this->dieOnUnknownArg === false) {
×
768
                $unknown       = $this->unknown;
×
769
                $unknown[]     = $arg;
×
770
                $this->unknown = $unknown;
×
771
            } else {
772
                $this->processUnknownArgument('-'.$arg, $pos);
×
773
            }
774
        }//end switch
775

776
    }//end processShortArgument()
5✔
777

778

779
    /**
780
     * Processes a long (--example) command-line argument.
781
     *
782
     * @param string $arg The command line argument.
783
     * @param int    $pos The position of the argument on the command line.
784
     *
785
     * @return void
786
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
787
     */
788
    public function processLongArgument($arg, $pos)
189✔
789
    {
790
        switch ($arg) {
63✔
791
        case 'help':
189✔
792
            $this->printUsage();
×
793
            throw new DeepExitException('', ExitCode::OKAY);
×
794
        case 'version':
189✔
795
            $output  = 'PHP_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') ';
×
796
            $output .= 'by Squiz and PHPCSStandards'.PHP_EOL;
×
797
            throw new DeepExitException($output, ExitCode::OKAY);
×
798
        case 'colors':
189✔
799
            if (isset($this->overriddenDefaults['colors']) === true) {
×
800
                break;
×
801
            }
802

803
            $this->colors = true;
×
804
            $this->overriddenDefaults['colors'] = true;
×
805
            break;
×
806
        case 'no-colors':
189✔
807
            if (isset($this->overriddenDefaults['colors']) === true) {
×
808
                break;
×
809
            }
810

811
            $this->colors = false;
×
812
            $this->overriddenDefaults['colors'] = true;
×
813
            break;
×
814
        case 'cache':
189✔
815
            if (isset($this->overriddenDefaults['cache']) === true) {
×
816
                break;
×
817
            }
818

819
            $this->cache = true;
×
820
            $this->overriddenDefaults['cache'] = true;
×
821
            break;
×
822
        case 'no-cache':
189✔
823
            if (isset($this->overriddenDefaults['cache']) === true) {
×
824
                break;
×
825
            }
826

827
            $this->cache = false;
×
828
            $this->overriddenDefaults['cache'] = true;
×
829
            break;
×
830
        case 'ignore-annotations':
189✔
831
            if (isset($this->overriddenDefaults['annotations']) === true) {
×
832
                break;
×
833
            }
834

835
            $this->annotations = false;
×
836
            $this->overriddenDefaults['annotations'] = true;
×
837
            break;
×
838
        case 'config-set':
189✔
839
            if (isset($this->cliArgs[($pos + 1)]) === false
×
840
                || isset($this->cliArgs[($pos + 2)]) === false
×
841
            ) {
842
                $error  = 'ERROR: Setting a config option requires a name and value'.PHP_EOL.PHP_EOL;
×
843
                $error .= $this->printShortUsage(true);
×
844
                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
845
            }
846

847
            $key     = $this->cliArgs[($pos + 1)];
×
848
            $value   = $this->cliArgs[($pos + 2)];
×
849
            $current = self::getConfigData($key);
×
850

851
            try {
852
                $this->setConfigData($key, $value);
×
853
            } catch (Exception $e) {
×
854
                throw new DeepExitException($e->getMessage().PHP_EOL, ExitCode::PROCESS_ERROR);
×
855
            }
856

857
            $output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
×
858

859
            if ($current === null) {
×
860
                $output .= "Config value \"$key\" added successfully".PHP_EOL;
×
861
            } else {
862
                $output .= "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL;
×
863
            }
864
            throw new DeepExitException($output, ExitCode::OKAY);
×
865
        case 'config-delete':
189✔
866
            if (isset($this->cliArgs[($pos + 1)]) === false) {
×
867
                $error  = 'ERROR: Deleting a config option requires the name of the option'.PHP_EOL.PHP_EOL;
×
868
                $error .= $this->printShortUsage(true);
×
869
                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
870
            }
871

872
            $output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
×
873

874
            $key     = $this->cliArgs[($pos + 1)];
×
875
            $current = self::getConfigData($key);
×
876
            if ($current === null) {
×
877
                $output .= "Config value \"$key\" has not been set".PHP_EOL;
×
878
            } else {
879
                try {
880
                    $this->setConfigData($key, null);
×
881
                } catch (Exception $e) {
×
882
                    throw new DeepExitException($e->getMessage().PHP_EOL, ExitCode::PROCESS_ERROR);
×
883
                }
884

885
                $output .= "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL;
×
886
            }
887
            throw new DeepExitException($output, ExitCode::OKAY);
×
888
        case 'config-show':
189✔
889
            $data    = self::getAllConfigData();
×
890
            $output  = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
×
891
            $output .= $this->prepareConfigDataForDisplay($data);
×
892
            throw new DeepExitException($output, ExitCode::OKAY);
×
893
        case 'runtime-set':
189✔
894
            if (isset($this->cliArgs[($pos + 1)]) === false
×
895
                || isset($this->cliArgs[($pos + 2)]) === false
×
896
            ) {
897
                $error  = 'ERROR: Setting a runtime config option requires a name and value'.PHP_EOL.PHP_EOL;
×
898
                $error .= $this->printShortUsage(true);
×
899
                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
900
            }
901

902
            $key   = $this->cliArgs[($pos + 1)];
×
903
            $value = $this->cliArgs[($pos + 2)];
×
904
            $this->cliArgs[($pos + 1)] = '';
×
905
            $this->cliArgs[($pos + 2)] = '';
×
906
            $this->setConfigData($key, $value, true);
×
907
            if (isset($this->overriddenDefaults['runtime-set']) === false) {
×
908
                $this->overriddenDefaults['runtime-set'] = [];
×
909
            }
910

911
            $this->overriddenDefaults['runtime-set'][$key] = true;
×
912
            break;
×
913
        default:
914
            if (substr($arg, 0, 7) === 'sniffs=') {
189✔
915
                if (isset($this->overriddenDefaults['sniffs']) === true) {
57✔
916
                    break;
3✔
917
                }
918

919
                $this->sniffs = $this->parseSniffCodes(substr($arg, 7), 'sniffs');
57✔
920
                $this->overriddenDefaults['sniffs'] = true;
21✔
921
            } else if (substr($arg, 0, 8) === 'exclude=') {
132✔
922
                if (isset($this->overriddenDefaults['exclude']) === true) {
57✔
923
                    break;
3✔
924
                }
925

926
                $this->exclude = $this->parseSniffCodes(substr($arg, 8), 'exclude');
57✔
927
                $this->overriddenDefaults['exclude'] = true;
21✔
928
            } else if (substr($arg, 0, 6) === 'cache=') {
75✔
929
                if ((isset($this->overriddenDefaults['cache']) === true
×
930
                    && $this->cache === false)
×
931
                    || isset($this->overriddenDefaults['cacheFile']) === true
×
932
                ) {
933
                    break;
×
934
                }
935

936
                // Turn caching on.
937
                $this->cache = true;
×
938
                $this->overriddenDefaults['cache'] = true;
×
939

940
                $this->cacheFile = Common::realpath(substr($arg, 6));
×
941

942
                // It may not exist and return false instead.
943
                if ($this->cacheFile === false) {
×
944
                    $this->cacheFile = substr($arg, 6);
×
945

946
                    $dir = dirname($this->cacheFile);
×
947
                    if (is_dir($dir) === false) {
×
948
                        $error  = 'ERROR: The specified cache file path "'.$this->cacheFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
×
949
                        $error .= $this->printShortUsage(true);
×
950
                        throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
951
                    }
952

953
                    if ($dir === '.') {
×
954
                        // Passed cache file is a file in the current directory.
955
                        $this->cacheFile = getcwd().'/'.basename($this->cacheFile);
×
956
                    } else {
957
                        if ($dir[0] === '/') {
×
958
                            // An absolute path.
959
                            $dir = Common::realpath($dir);
×
960
                        } else {
961
                            $dir = Common::realpath(getcwd().'/'.$dir);
×
962
                        }
963

964
                        if ($dir !== false) {
×
965
                            // Cache file path is relative.
966
                            $this->cacheFile = $dir.'/'.basename($this->cacheFile);
×
967
                        }
968
                    }
969
                }//end if
970

971
                $this->overriddenDefaults['cacheFile'] = true;
×
972

973
                if (is_dir($this->cacheFile) === true) {
×
974
                    $error  = 'ERROR: The specified cache file path "'.$this->cacheFile.'" is a directory'.PHP_EOL.PHP_EOL;
×
975
                    $error .= $this->printShortUsage(true);
×
976
                    throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
977
                }
978
            } else if (substr($arg, 0, 10) === 'bootstrap=') {
75✔
979
                $files     = explode(',', substr($arg, 10));
×
980
                $bootstrap = [];
×
981
                foreach ($files as $file) {
×
982
                    $path = Common::realpath($file);
×
983
                    if ($path === false) {
×
984
                        $error  = 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL;
×
985
                        $error .= $this->printShortUsage(true);
×
986
                        throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
987
                    }
988

989
                    $bootstrap[] = $path;
×
990
                }
991

992
                $this->bootstrap = array_merge($this->bootstrap, $bootstrap);
×
993
                $this->overriddenDefaults['bootstrap'] = true;
×
994
            } else if (substr($arg, 0, 10) === 'file-list=') {
75✔
995
                $fileList = substr($arg, 10);
×
996
                $path     = Common::realpath($fileList);
×
997
                if ($path === false) {
×
998
                    $error  = 'ERROR: The specified file list "'.$fileList.'" does not exist'.PHP_EOL.PHP_EOL;
×
999
                    $error .= $this->printShortUsage(true);
×
1000
                    throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1001
                }
1002

1003
                $files = file($path);
×
1004
                foreach ($files as $inputFile) {
×
1005
                    $inputFile = trim($inputFile);
×
1006

1007
                    // Skip empty lines.
1008
                    if ($inputFile === '') {
×
1009
                        continue;
×
1010
                    }
1011

1012
                    $this->processFilePath($inputFile);
×
1013
                }
1014
            } else if (substr($arg, 0, 11) === 'stdin-path=') {
75✔
1015
                if (isset($this->overriddenDefaults['stdinPath']) === true) {
×
1016
                    break;
×
1017
                }
1018

1019
                $this->stdinPath = Common::realpath(substr($arg, 11));
×
1020

1021
                // It may not exist and return false instead, so use whatever they gave us.
1022
                if ($this->stdinPath === false) {
×
1023
                    $this->stdinPath = trim(substr($arg, 11));
×
1024
                }
1025

1026
                $this->overriddenDefaults['stdinPath'] = true;
×
1027
            } else if (substr($arg, 0, 12) === 'report-file=') {
75✔
1028
                if (PHP_CODESNIFFER_CBF === true || isset($this->overriddenDefaults['reportFile']) === true) {
6✔
1029
                    break;
3✔
1030
                }
1031

1032
                $this->reportFile = Common::realpath(substr($arg, 12));
3✔
1033

1034
                // It may not exist and return false instead.
1035
                if ($this->reportFile === false) {
3✔
1036
                    $this->reportFile = substr($arg, 12);
3✔
1037

1038
                    $dir = Common::realpath(dirname($this->reportFile));
3✔
1039
                    if (is_dir($dir) === false) {
3✔
1040
                        $error  = 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
×
1041
                        $error .= $this->printShortUsage(true);
×
1042
                        throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1043
                    }
1044

1045
                    $this->reportFile = $dir.'/'.basename($this->reportFile);
3✔
1046
                }//end if
1047

1048
                $this->overriddenDefaults['reportFile'] = true;
3✔
1049

1050
                if (is_dir($this->reportFile) === true) {
3✔
1051
                    $error  = 'ERROR: The specified report file path "'.$this->reportFile.'" is a directory'.PHP_EOL.PHP_EOL;
×
1052
                    $error .= $this->printShortUsage(true);
×
1053
                    throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
1✔
1054
                }
1055
            } else if (substr($arg, 0, 13) === 'report-width=') {
69✔
1056
                if (isset($this->overriddenDefaults['reportWidth']) === true) {
9✔
1057
                    break;
3✔
1058
                }
1059

1060
                $this->reportWidth = substr($arg, 13);
9✔
1061
                $this->overriddenDefaults['reportWidth'] = true;
9✔
1062
            } else if (substr($arg, 0, 9) === 'basepath=') {
69✔
1063
                if (isset($this->overriddenDefaults['basepath']) === true) {
×
1064
                    break;
×
1065
                }
1066

1067
                $this->overriddenDefaults['basepath'] = true;
×
1068

1069
                if (substr($arg, 9) === '') {
×
1070
                    $this->basepath = null;
×
1071
                    break;
×
1072
                }
1073

1074
                $this->basepath = Common::realpath(substr($arg, 9));
×
1075

1076
                // It may not exist and return false instead.
1077
                if ($this->basepath === false) {
×
1078
                    $this->basepath = substr($arg, 9);
×
1079
                }
1080

1081
                if (is_dir($this->basepath) === false) {
×
1082
                    $error  = 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
×
1083
                    $error .= $this->printShortUsage(true);
×
1084
                    throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1085
                }
1086
            } else if ((substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-')) {
69✔
1087
                $reports = [];
×
1088

1089
                if ($arg[6] === '-') {
×
1090
                    // This is a report with file output.
1091
                    $split = strpos($arg, '=');
×
1092
                    if ($split === false) {
×
1093
                        $report = substr($arg, 7);
×
1094
                        $output = null;
×
1095
                    } else {
1096
                        $report = substr($arg, 7, ($split - 7));
×
1097
                        $output = substr($arg, ($split + 1));
×
1098
                        if ($output === false) {
×
1099
                            $output = null;
×
1100
                        } else {
1101
                            $dir = Common::realpath(dirname($output));
×
1102
                            if (is_dir($dir) === false) {
×
1103
                                $error  = 'ERROR: The specified '.$report.' report file path "'.$output.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
×
1104
                                $error .= $this->printShortUsage(true);
×
1105
                                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1106
                            }
1107

1108
                            $output = $dir.'/'.basename($output);
×
1109

1110
                            if (is_dir($output) === true) {
×
1111
                                $error  = 'ERROR: The specified '.$report.' report file path "'.$output.'" is a directory'.PHP_EOL.PHP_EOL;
×
1112
                                $error .= $this->printShortUsage(true);
×
1113
                                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1114
                            }
1115
                        }//end if
1116
                    }//end if
1117

1118
                    $reports[$report] = $output;
×
1119
                } else {
1120
                    // This is a single report.
1121
                    if (isset($this->overriddenDefaults['reports']) === true) {
×
1122
                        break;
×
1123
                    }
1124

1125
                    $reportNames = explode(',', substr($arg, 7));
×
1126
                    foreach ($reportNames as $report) {
×
1127
                        $reports[$report] = null;
×
1128
                    }
1129
                }//end if
1130

1131
                // Remove the default value so the CLI value overrides it.
1132
                if (isset($this->overriddenDefaults['reports']) === false) {
×
1133
                    $this->reports = $reports;
×
1134
                } else {
1135
                    $this->reports = array_merge($this->reports, $reports);
×
1136
                }
1137

1138
                $this->overriddenDefaults['reports'] = true;
×
1139
            } else if (substr($arg, 0, 7) === 'filter=') {
69✔
1140
                if (isset($this->overriddenDefaults['filter']) === true) {
×
1141
                    break;
×
1142
                }
1143

1144
                $this->filter = substr($arg, 7);
×
1145
                $this->overriddenDefaults['filter'] = true;
×
1146
            } else if (substr($arg, 0, 9) === 'standard=') {
69✔
1147
                $standards = trim(substr($arg, 9));
9✔
1148
                if ($standards !== '') {
9✔
1149
                    $this->standards = explode(',', $standards);
9✔
1150
                }
1151

1152
                $this->overriddenDefaults['standards'] = true;
9✔
1153
            } else if (substr($arg, 0, 11) === 'extensions=') {
60✔
1154
                if (isset($this->overriddenDefaults['extensions']) === true) {
30✔
1155
                    break;
3✔
1156
                }
1157

1158
                $extensionsString = substr($arg, 11);
30✔
1159
                $newExtensions    = [];
30✔
1160
                if (empty($extensionsString) === false) {
30✔
1161
                    $extensions = explode(',', $extensionsString);
27✔
1162
                    foreach ($extensions as $ext) {
27✔
1163
                        if (strpos($ext, '/') !== false) {
27✔
1164
                            // They specified the tokenizer too.
1165
                            list($ext, $tokenizer) = explode('/', $ext);
12✔
1166
                            if (strtoupper($tokenizer) !== 'PHP') {
12✔
1167
                                $error  = 'ERROR: Specifying the tokenizer to use for an extension is no longer supported.'.PHP_EOL;
9✔
1168
                                $error .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
9✔
1169
                                $error .= 'Received: '.substr($arg, 11).PHP_EOL.PHP_EOL;
9✔
1170
                                $error .= $this->printShortUsage(true);
9✔
1171
                                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
9✔
1172
                            }
1173
                        }
1174

1175
                        $newExtensions[$ext] = 'PHP';
21✔
1176
                    }
1177
                }
1178

1179
                $this->extensions = $newExtensions;
21✔
1180
                $this->overriddenDefaults['extensions'] = true;
21✔
1181
            } else if (substr($arg, 0, 7) === 'suffix=') {
30✔
1182
                if (isset($this->overriddenDefaults['suffix']) === true) {
×
1183
                    break;
×
1184
                }
1185

1186
                $this->suffix = substr($arg, 7);
×
1187
                $this->overriddenDefaults['suffix'] = true;
×
1188
            } else if (substr($arg, 0, 9) === 'parallel=') {
30✔
1189
                if (isset($this->overriddenDefaults['parallel']) === true) {
×
1190
                    break;
×
1191
                }
1192

1193
                $this->parallel = max((int) substr($arg, 9), 1);
×
1194
                $this->overriddenDefaults['parallel'] = true;
×
1195
            } else if (substr($arg, 0, 9) === 'severity=') {
30✔
1196
                $this->errorSeverity   = (int) substr($arg, 9);
×
1197
                $this->warningSeverity = $this->errorSeverity;
×
1198
                if (isset($this->overriddenDefaults['errorSeverity']) === false) {
×
1199
                    $this->overriddenDefaults['errorSeverity'] = true;
×
1200
                }
1201

1202
                if (isset($this->overriddenDefaults['warningSeverity']) === false) {
×
1203
                    $this->overriddenDefaults['warningSeverity'] = true;
×
1204
                }
1205
            } else if (substr($arg, 0, 15) === 'error-severity=') {
30✔
1206
                if (isset($this->overriddenDefaults['errorSeverity']) === true) {
×
1207
                    break;
×
1208
                }
1209

1210
                $this->errorSeverity = (int) substr($arg, 15);
×
1211
                $this->overriddenDefaults['errorSeverity'] = true;
×
1212
            } else if (substr($arg, 0, 17) === 'warning-severity=') {
30✔
1213
                if (isset($this->overriddenDefaults['warningSeverity']) === true) {
×
1214
                    break;
×
1215
                }
1216

1217
                $this->warningSeverity = (int) substr($arg, 17);
×
1218
                $this->overriddenDefaults['warningSeverity'] = true;
×
1219
            } else if (substr($arg, 0, 7) === 'ignore=') {
30✔
1220
                if (isset($this->overriddenDefaults['ignored']) === true) {
×
1221
                    break;
×
1222
                }
1223

1224
                // Split the ignore string on commas, unless the comma is escaped
1225
                // using 1 or 3 slashes (\, or \\\,).
1226
                $patterns = preg_split(
×
1227
                    '/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/',
×
1228
                    substr($arg, 7)
×
1229
                );
1230

1231
                $ignored = [];
×
1232
                foreach ($patterns as $pattern) {
×
1233
                    $pattern = trim($pattern);
×
1234
                    if ($pattern === '') {
×
1235
                        continue;
×
1236
                    }
1237

1238
                    $ignored[$pattern] = 'absolute';
×
1239
                }
1240

1241
                $this->ignored = $ignored;
×
1242
                $this->overriddenDefaults['ignored'] = true;
×
1243
            } else if (substr($arg, 0, 10) === 'generator='
30✔
1244
                && PHP_CODESNIFFER_CBF === false
30✔
1245
            ) {
1246
                if (isset($this->overriddenDefaults['generator']) === true) {
30✔
1247
                    break;
3✔
1248
                }
1249

1250
                $generatorName          = substr($arg, 10);
30✔
1251
                $lowerCaseGeneratorName = strtolower($generatorName);
30✔
1252

1253
                if (isset($this->validGenerators[$lowerCaseGeneratorName]) === false) {
30✔
1254
                    $validOptions = implode(', ', $this->validGenerators);
9✔
1255
                    $validOptions = substr_replace($validOptions, ' and', strrpos($validOptions, ','), 1);
9✔
1256
                    $error        = sprintf(
9✔
1257
                        'ERROR: "%s" is not a valid generator. The following generators are supported: %s.'.PHP_EOL.PHP_EOL,
9✔
1258
                        $generatorName,
9✔
1259
                        $validOptions
9✔
1260
                    );
6✔
1261
                    $error       .= $this->printShortUsage(true);
9✔
1262
                    throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
9✔
1263
                }
1264

1265
                $this->generator = $this->validGenerators[$lowerCaseGeneratorName];
21✔
1266
                $this->overriddenDefaults['generator'] = true;
21✔
1267
            } else if (substr($arg, 0, 9) === 'encoding=') {
×
1268
                if (isset($this->overriddenDefaults['encoding']) === true) {
×
1269
                    break;
×
1270
                }
1271

1272
                $this->encoding = strtolower(substr($arg, 9));
×
1273
                $this->overriddenDefaults['encoding'] = true;
×
1274
            } else if (substr($arg, 0, 10) === 'tab-width=') {
×
1275
                if (isset($this->overriddenDefaults['tabWidth']) === true) {
×
1276
                    break;
×
1277
                }
1278

1279
                $this->tabWidth = (int) substr($arg, 10);
×
1280
                $this->overriddenDefaults['tabWidth'] = true;
×
1281
            } else {
1282
                if ($this->dieOnUnknownArg === false) {
×
1283
                    $eqPos = strpos($arg, '=');
×
1284
                    try {
1285
                        $unknown = $this->unknown;
×
1286

1287
                        if ($eqPos === false) {
×
1288
                            $unknown[$arg] = $arg;
×
1289
                        } else {
1290
                            $value         = substr($arg, ($eqPos + 1));
×
1291
                            $arg           = substr($arg, 0, $eqPos);
×
1292
                            $unknown[$arg] = $value;
×
1293
                        }
1294

1295
                        $this->unknown = $unknown;
×
1296
                    } catch (RuntimeException $e) {
×
1297
                        // Value is not valid, so just ignore it.
1298
                    }
1299
                } else {
1300
                    $this->processUnknownArgument('--'.$arg, $pos);
×
1301
                }
1302
            }//end if
1303
            break;
96✔
1304
        }//end switch
1305

1306
    }//end processLongArgument()
33✔
1307

1308

1309
    /**
1310
     * Parse supplied string into a list of validated sniff codes.
1311
     *
1312
     * @param string $input    Comma-separated string of sniff codes.
1313
     * @param string $argument The name of the argument which is being processed.
1314
     *
1315
     * @return array<string>
1316
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException When any of the provided codes are not valid as sniff codes.
1317
     */
1318
    private function parseSniffCodes($input, $argument)
114✔
1319
    {
1320
        $errors = [];
114✔
1321
        $sniffs = [];
114✔
1322

1323
        $possibleSniffs = array_filter(explode(',', $input));
114✔
1324

1325
        if ($possibleSniffs === []) {
114✔
1326
            $errors[] = 'No codes specified / empty argument';
18✔
1327
        }
1328

1329
        foreach ($possibleSniffs as $sniff) {
114✔
1330
            $sniff = trim($sniff);
96✔
1331

1332
            $partCount = substr_count($sniff, '.');
96✔
1333
            if ($partCount === 2) {
96✔
1334
                // Correct number of parts.
1335
                $sniffs[] = $sniff;
54✔
1336
                continue;
54✔
1337
            }
1338

1339
            if ($partCount === 0) {
54✔
1340
                $errors[] = 'Standard codes are not supported: '.$sniff;
12✔
1341
            } else if ($partCount === 1) {
42✔
1342
                $errors[] = 'Category codes are not supported: '.$sniff;
18✔
1343
            } else if ($partCount === 3) {
24✔
1344
                $errors[] = 'Message codes are not supported: '.$sniff;
18✔
1345
            } else {
1346
                $errors[] = 'Too many parts: '.$sniff;
12✔
1347
            }
1348

1349
            if ($partCount > 2) {
54✔
1350
                $parts    = explode('.', $sniff, 4);
24✔
1351
                $sniffs[] = $parts[0].'.'.$parts[1].'.'.$parts[2];
24✔
1352
            }
1353
        }//end foreach
1354

1355
        $sniffs = array_reduce(
114✔
1356
            $sniffs,
114✔
1357
            static function ($carry, $item) {
76✔
1358
                $lower = strtolower($item);
78✔
1359

1360
                foreach ($carry as $found) {
78✔
1361
                    if ($lower === strtolower($found)) {
36✔
1362
                        // This sniff is already in our list.
1363
                        return $carry;
24✔
1364
                    }
1365
                }
1366

1367
                $carry[] = $item;
78✔
1368

1369
                return $carry;
78✔
1370
            },
114✔
1371
            []
114✔
1372
        );
76✔
1373

1374
        if ($errors !== []) {
114✔
1375
            $error  = 'ERROR: The --'.$argument.' option only supports sniff codes.'.PHP_EOL;
72✔
1376
            $error .= 'Sniff codes are in the form "Standard.Category.Sniff".'.PHP_EOL;
72✔
1377
            $error .= PHP_EOL;
72✔
1378
            $error .= 'The following problems were detected:'.PHP_EOL;
72✔
1379
            $error .= '* '.implode(PHP_EOL.'* ', $errors).PHP_EOL;
72✔
1380

1381
            if ($sniffs !== []) {
72✔
1382
                $error .= PHP_EOL;
36✔
1383
                $error .= 'Perhaps try --'.$argument.'="'.implode(',', $sniffs).'" instead.'.PHP_EOL;
36✔
1384
            }
1385

1386
            $error .= PHP_EOL;
72✔
1387
            $error .= $this->printShortUsage(true);
72✔
1388
            throw new DeepExitException(ltrim($error), ExitCode::PROCESS_ERROR);
72✔
1389
        }
1390

1391
        return $sniffs;
42✔
1392

1393
    }//end parseSniffCodes()
1394

1395

1396
    /**
1397
     * Processes an unknown command line argument.
1398
     *
1399
     * Assumes all unknown arguments are files and folders to check.
1400
     *
1401
     * @param string $arg The command line argument.
1402
     * @param int    $pos The position of the argument on the command line.
1403
     *
1404
     * @return void
1405
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
1406
     */
1407
    public function processUnknownArgument($arg, $pos)
×
1408
    {
1409
        // We don't know about any additional switches; just files.
1410
        if ($arg[0] === '-') {
×
1411
            if ($this->dieOnUnknownArg === false) {
×
1412
                return;
×
1413
            }
1414

1415
            $error  = "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL;
×
1416
            $error .= $this->printShortUsage(true);
×
1417
            throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1418
        }
1419

1420
        $this->processFilePath($arg);
×
1421

1422
    }//end processUnknownArgument()
1423

1424

1425
    /**
1426
     * Processes a file path and add it to the file list.
1427
     *
1428
     * @param string $path The path to the file to add.
1429
     *
1430
     * @return void
1431
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
1432
     */
1433
    public function processFilePath($path)
×
1434
    {
1435
        // If we are processing STDIN, don't record any files to check.
1436
        if ($this->stdin === true) {
×
1437
            return;
×
1438
        }
1439

1440
        $file = Common::realpath($path);
×
1441
        if (file_exists($file) === false) {
×
1442
            if ($this->dieOnUnknownArg === false) {
×
1443
                return;
×
1444
            }
1445

1446
            $error  = 'ERROR: The file "'.$path.'" does not exist.'.PHP_EOL.PHP_EOL;
×
1447
            $error .= $this->printShortUsage(true);
×
1448
            throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1449
        } else {
1450
            // Can't modify the files array directly because it's not a real
1451
            // class member, so need to use this little get/modify/set trick.
1452
            $files       = $this->files;
×
1453
            $files[]     = $file;
×
1454
            $this->files = $files;
×
1455
            $this->overriddenDefaults['files'] = true;
×
1456
        }
1457

1458
    }//end processFilePath()
1459

1460

1461
    /**
1462
     * Prints out the usage information for this script.
1463
     *
1464
     * @return void
1465
     */
1466
    public function printUsage()
×
1467
    {
1468
        echo PHP_EOL;
×
1469

1470
        if (PHP_CODESNIFFER_CBF === true) {
×
1471
            $this->printPHPCBFUsage();
×
1472
        } else {
1473
            $this->printPHPCSUsage();
×
1474
        }
1475

1476
        echo PHP_EOL;
×
1477

1478
    }//end printUsage()
1479

1480

1481
    /**
1482
     * Prints out the short usage information for this script.
1483
     *
1484
     * @param bool $return If TRUE, the usage string is returned
1485
     *                     instead of output to screen.
1486
     *
1487
     * @return string|void
1488
     */
1489
    public function printShortUsage($return=false)
×
1490
    {
1491
        if (PHP_CODESNIFFER_CBF === true) {
×
1492
            $usage = 'Run "phpcbf --help" for usage information';
×
1493
        } else {
1494
            $usage = 'Run "phpcs --help" for usage information';
×
1495
        }
1496

1497
        $usage .= PHP_EOL.PHP_EOL;
×
1498

1499
        if ($return === true) {
×
1500
            return $usage;
×
1501
        }
1502

1503
        echo $usage;
×
1504

1505
    }//end printShortUsage()
1506

1507

1508
    /**
1509
     * Prints out the usage information for PHPCS.
1510
     *
1511
     * @return void
1512
     */
1513
    public function printPHPCSUsage()
×
1514
    {
1515
        $longOptions   = explode(',', Help::DEFAULT_LONG_OPTIONS);
×
1516
        $longOptions[] = 'cache';
×
1517
        $longOptions[] = 'no-cache';
×
1518
        $longOptions[] = 'report';
×
1519
        $longOptions[] = 'report-file';
×
1520
        $longOptions[] = 'report-report';
×
1521
        $longOptions[] = 'config-explain';
×
1522
        $longOptions[] = 'config-set';
×
1523
        $longOptions[] = 'config-delete';
×
1524
        $longOptions[] = 'config-show';
×
1525
        $longOptions[] = 'generator';
×
1526

1527
        $shortOptions = Help::DEFAULT_SHORT_OPTIONS.'aems';
×
1528

1529
        (new Help($this, $longOptions, $shortOptions))->display();
×
1530

1531
    }//end printPHPCSUsage()
1532

1533

1534
    /**
1535
     * Prints out the usage information for PHPCBF.
1536
     *
1537
     * @return void
1538
     */
1539
    public function printPHPCBFUsage()
×
1540
    {
1541
        $longOptions   = explode(',', Help::DEFAULT_LONG_OPTIONS);
×
1542
        $longOptions[] = 'suffix';
×
1543
        $shortOptions  = Help::DEFAULT_SHORT_OPTIONS;
×
1544

1545
        (new Help($this, $longOptions, $shortOptions))->display();
×
1546

1547
    }//end printPHPCBFUsage()
1548

1549

1550
    /**
1551
     * Get a single config value.
1552
     *
1553
     * @param string $key The name of the config value.
1554
     *
1555
     * @return string|null
1556
     * @see    setConfigData()
1557
     * @see    getAllConfigData()
1558
     */
1559
    public static function getConfigData($key)
6✔
1560
    {
1561
        $phpCodeSnifferConfig = self::getAllConfigData();
6✔
1562

1563
        if ($phpCodeSnifferConfig === null) {
6✔
1564
            return null;
×
1565
        }
1566

1567
        if (isset($phpCodeSnifferConfig[$key]) === false) {
6✔
1568
            return null;
6✔
1569
        }
1570

1571
        return $phpCodeSnifferConfig[$key];
6✔
1572

1573
    }//end getConfigData()
1574

1575

1576
    /**
1577
     * Get the path to an executable utility.
1578
     *
1579
     * @param string $name The name of the executable utility.
1580
     *
1581
     * @return string|null
1582
     * @see    getConfigData()
1583
     */
1584
    public static function getExecutablePath($name)
×
1585
    {
1586
        $data = self::getConfigData($name.'_path');
×
1587
        if ($data !== null) {
×
1588
            return $data;
×
1589
        }
1590

1591
        if ($name === "php") {
×
1592
            // For php, we know the executable path. There's no need to look it up.
1593
            return PHP_BINARY;
×
1594
        }
1595

1596
        if (array_key_exists($name, self::$executablePaths) === true) {
×
1597
            return self::$executablePaths[$name];
×
1598
        }
1599

1600
        if (PHP_OS_FAMILY === 'Windows') {
×
1601
            $cmd = 'where '.escapeshellarg($name).' 2> nul';
×
1602
        } else {
1603
            $cmd = 'which '.escapeshellarg($name).' 2> /dev/null';
×
1604
        }
1605

1606
        $result = exec($cmd, $output, $retVal);
×
1607
        if ($retVal !== 0) {
×
1608
            $result = null;
×
1609
        }
1610

1611
        self::$executablePaths[$name] = $result;
×
1612
        return $result;
×
1613

1614
    }//end getExecutablePath()
1615

1616

1617
    /**
1618
     * Set a single config value.
1619
     *
1620
     * @param string      $key   The name of the config value.
1621
     * @param string|null $value The value to set. If null, the config
1622
     *                           entry is deleted, reverting it to the
1623
     *                           default value.
1624
     * @param boolean     $temp  Set this config data temporarily for this
1625
     *                           script run. This will not write the config
1626
     *                           data to the config file.
1627
     *
1628
     * @return bool
1629
     * @see    getConfigData()
1630
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the config file can not be written.
1631
     */
1632
    public function setConfigData($key, $value, $temp=false)
×
1633
    {
1634
        if (isset($this->overriddenDefaults['runtime-set']) === true
×
1635
            && isset($this->overriddenDefaults['runtime-set'][$key]) === true
×
1636
        ) {
1637
            return false;
×
1638
        }
1639

1640
        if ($temp === false) {
×
1641
            $path = '';
×
1642
            if (is_callable('\Phar::running') === true) {
×
1643
                $path = Phar::running(false);
×
1644
            }
1645

1646
            if ($path !== '') {
×
1647
                $configFile = dirname($path).DIRECTORY_SEPARATOR.'CodeSniffer.conf';
×
1648
            } else {
1649
                $configFile = dirname(__DIR__).DIRECTORY_SEPARATOR.'CodeSniffer.conf';
×
1650
            }
1651

1652
            if (is_file($configFile) === true
×
1653
                && is_writable($configFile) === false
×
1654
            ) {
1655
                $error = 'ERROR: Config file '.$configFile.' is not writable'.PHP_EOL.PHP_EOL;
×
1656
                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1657
            }
1658
        }//end if
1659

1660
        $phpCodeSnifferConfig = self::getAllConfigData();
×
1661

1662
        if ($value === null) {
×
1663
            if (isset($phpCodeSnifferConfig[$key]) === true) {
×
1664
                unset($phpCodeSnifferConfig[$key]);
×
1665
            }
1666
        } else {
1667
            $phpCodeSnifferConfig[$key] = $value;
×
1668
        }
1669

1670
        if ($temp === false) {
×
1671
            $output  = '<'.'?php'."\n".' $phpCodeSnifferConfig = ';
×
1672
            $output .= var_export($phpCodeSnifferConfig, true);
×
1673
            $output .= ";\n?".'>';
×
1674

1675
            if (file_put_contents($configFile, $output) === false) {
×
1676
                $error = 'ERROR: Config file '.$configFile.' could not be written'.PHP_EOL.PHP_EOL;
×
1677
                throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1678
            }
1679

1680
            self::$configDataFile = $configFile;
×
1681
        }
1682

1683
        self::$configData = $phpCodeSnifferConfig;
×
1684

1685
        // If the installed paths are being set, make sure all known
1686
        // standards paths are added to the autoloader.
1687
        if ($key === 'installed_paths') {
×
1688
            $installedStandards = Standards::getInstalledStandardDetails();
×
1689
            foreach ($installedStandards as $details) {
×
1690
                Autoload::addSearchPath($details['path'], $details['namespace']);
×
1691
            }
1692
        }
1693

1694
        return true;
×
1695

1696
    }//end setConfigData()
1697

1698

1699
    /**
1700
     * Get all config data.
1701
     *
1702
     * @return array<string, string>
1703
     * @see    getConfigData()
1704
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the config file could not be read.
1705
     */
1706
    public static function getAllConfigData()
×
1707
    {
1708
        if (self::$configData !== null) {
×
1709
            return self::$configData;
×
1710
        }
1711

1712
        $path = '';
×
1713
        if (is_callable('\Phar::running') === true) {
×
1714
            $path = Phar::running(false);
×
1715
        }
1716

1717
        if ($path !== '') {
×
1718
            $configFile = dirname($path).DIRECTORY_SEPARATOR.'CodeSniffer.conf';
×
1719
        } else {
1720
            $configFile = dirname(__DIR__).DIRECTORY_SEPARATOR.'CodeSniffer.conf';
×
1721
            if (is_file($configFile) === false
×
1722
                && strpos('@data_dir@', '@data_dir') === false
×
1723
            ) {
1724
                $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
×
1725
            }
1726
        }
1727

1728
        if (is_file($configFile) === false) {
×
1729
            self::$configData = [];
×
1730
            return [];
×
1731
        }
1732

1733
        if (Common::isReadable($configFile) === false) {
×
1734
            $error = 'ERROR: Config file '.$configFile.' is not readable'.PHP_EOL.PHP_EOL;
×
1735
            throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
1736
        }
1737

1738
        include $configFile;
×
1739
        self::$configDataFile = $configFile;
×
1740
        self::$configData     = $phpCodeSnifferConfig;
×
1741
        return self::$configData;
×
1742

1743
    }//end getAllConfigData()
1744

1745

1746
    /**
1747
     * Prepares the gathered config data for display.
1748
     *
1749
     * @param array<string, string> $data The config data to format for display.
1750
     *
1751
     * @return string
1752
     */
1753
    public function prepareConfigDataForDisplay($data)
18✔
1754
    {
1755
        if (empty($data) === true) {
18✔
1756
            return '';
6✔
1757
        }
1758

1759
        $max  = 0;
12✔
1760
        $keys = array_keys($data);
12✔
1761
        foreach ($keys as $key) {
12✔
1762
            $len = strlen($key);
12✔
1763
            if ($len > $max) {
12✔
1764
                $max = $len;
12✔
1765
            }
1766
        }
1767

1768
        $max += 2;
12✔
1769
        ksort($data);
12✔
1770

1771
        $output = '';
12✔
1772
        foreach ($data as $name => $value) {
12✔
1773
            $output .= str_pad($name.': ', $max).$value.PHP_EOL;
12✔
1774
        }
1775

1776
        return $output;
12✔
1777

1778
    }//end prepareConfigDataForDisplay()
1779

1780

1781
    /**
1782
     * Prints out the gathered config data.
1783
     *
1784
     * @param array<string, string> $data The config data to print.
1785
     *
1786
     * @deprecated 4.0.0 Use `echo Config::prepareConfigDataForDisplay()` instead.
1787
     *
1788
     * @return void
1789
     */
1790
    public function printConfigData($data)
3✔
1791
    {
1792
        echo $this->prepareConfigDataForDisplay($data);
3✔
1793

1794
    }//end printConfigData()
1✔
1795

1796

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

© 2025 Coveralls, Inc