Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Config |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The current version. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | const VERSION = '3.0.0'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Package stability; either stable, beta or alpha. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const STABILITY = 'alpha'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * An array of settings that PHPCS and PHPCBF accept. |
||
| 36 | * |
||
| 37 | * This array is not meant to be accessed directly. Instead, use the settings |
||
| 38 | * as if they are class member vars so the __get() and __set() magic methods |
||
| 39 | * can be used to validate the values. For example, to set the verbosity level to |
||
| 40 | * level 2, use $this->verbosity = 2; insteas of accessing this property directly. |
||
| 41 | * |
||
| 42 | * The list of settings are: |
||
| 43 | * |
||
| 44 | * string[] files The files and directories to check. |
||
| 45 | * string[] standards The standards being used for checking. |
||
| 46 | * int verbosity How verbose the output should be. |
||
| 47 | * 0: no unnecessary output |
||
| 48 | * 1: basic output for files being checked |
||
| 49 | * 2: ruleset and file parsing output |
||
| 50 | * 3: sniff execution output |
||
| 51 | * bool interactive Enable interactive checking mode. |
||
| 52 | * bool parallel Check files in parallel. |
||
| 53 | * bool cache Enable the use of the file cache. |
||
| 54 | * bool cacheFile A file where the cache data should be written |
||
| 55 | * bool colors Display colours in output. |
||
| 56 | * bool explain Explain the coding standards. |
||
| 57 | * bool local Process local files in directories only (no recursion). |
||
| 58 | * bool showSources Show sniff source codes in report output. |
||
| 59 | * bool showProgress Show basic progress information while running. |
||
| 60 | * int tabWidth How many spaces each tab is worth. |
||
| 61 | * string encoding The encoding of the files being checked. |
||
| 62 | * string[] sniffs The sniffs that should be used for checking. |
||
| 63 | * If empty, all sniffs in the supplied standards will be used. |
||
| 64 | * string[] ignored Regular expressions used to ignore files and folders during checking. |
||
| 65 | * string reportFile A file where the report output should be written. |
||
| 66 | * string generator The documentation generator to use. |
||
| 67 | * string filter The filter to use for the run. |
||
| 68 | * string[] bootstrap One of more files to include before the run begins. |
||
| 69 | * int reportWidth The maximum number of columns that reports should use for output. |
||
| 70 | * Set to "auto" for have this value changed to the width of the terminal. |
||
| 71 | * int errorSeverity The minimum severity an error must have to be displayed. |
||
| 72 | * int warningSeverity The minimum severity a warning must have to be displayed. |
||
| 73 | * bool recordErrors Record the content of error messages as well as error counts. |
||
| 74 | * string suffix A suffix to add to fixed files. |
||
| 75 | * string basepath A file system location to strip from the paths of files shown in reports. |
||
| 76 | * bool stdin Read content from STDIN instead of supplied files. |
||
| 77 | * string stdinContent Content passed directly to PHPCS on STDIN. |
||
| 78 | * string stdinPath The path to use for content passed on STDIN. |
||
| 79 | * |
||
| 80 | * array<string, string> extensions File extensions that should be checked, and what tokenizer to use. |
||
| 81 | * E.g., array('inc' => 'PHP'); |
||
| 82 | * array<string, string|null> reports The reports to use for printing output after the run. |
||
| 83 | * The format of the array is: |
||
| 84 | * array( |
||
| 85 | * 'reportName1' => 'outputFile', |
||
| 86 | * 'reportName2' => null, |
||
| 87 | * ); |
||
| 88 | * If the array value is NULL, the report will be written to the screen. |
||
| 89 | * |
||
| 90 | * @var array<string, mixed> |
||
| 91 | */ |
||
| 92 | private $settings = array( |
||
| 93 | 'files' => null, |
||
| 94 | 'standards' => null, |
||
| 95 | 'verbosity' => null, |
||
| 96 | 'interactive' => null, |
||
| 97 | 'parallel' => null, |
||
| 98 | 'cache' => null, |
||
| 99 | 'cacheFile' => null, |
||
| 100 | 'colors' => null, |
||
| 101 | 'explain' => null, |
||
| 102 | 'local' => null, |
||
| 103 | 'showSources' => null, |
||
| 104 | 'showProgress' => null, |
||
| 105 | 'tabWidth' => null, |
||
| 106 | 'encoding' => null, |
||
| 107 | 'extensions' => null, |
||
| 108 | 'sniffs' => null, |
||
| 109 | 'ignored' => null, |
||
| 110 | 'reportFile' => null, |
||
| 111 | 'generator' => null, |
||
| 112 | 'filter' => null, |
||
| 113 | 'bootstrap' => null, |
||
| 114 | 'reports' => null, |
||
| 115 | 'basepath' => null, |
||
| 116 | 'reportWidth' => null, |
||
| 117 | 'errorSeverity' => null, |
||
| 118 | 'warningSeverity' => null, |
||
| 119 | 'recordErrors' => null, |
||
| 120 | 'suffix' => null, |
||
| 121 | 'stdin' => null, |
||
| 122 | 'stdinContent' => null, |
||
| 123 | 'stdinPath' => null, |
||
| 124 | ); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Whether or not to kill the process when an unknown command line arg is found. |
||
| 128 | * |
||
| 129 | * If FALSE, arguments that are not command line options or file/directory paths |
||
| 130 | * will be ignored and execution will continue. |
||
| 131 | * |
||
| 132 | * @var boolean |
||
| 133 | */ |
||
| 134 | public $dieOnUnknownArg; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The current command line arguments we are processing. |
||
| 138 | * |
||
| 139 | * @var string[] |
||
| 140 | */ |
||
| 141 | private $cliArgs = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Command line values that the user has supplied directly. |
||
| 145 | * |
||
| 146 | * @var array<string, TRUE> |
||
| 147 | */ |
||
| 148 | private $overriddenDefaults = array(); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Unknown arguments |
||
| 152 | * |
||
| 153 | * @var array<mixed> |
||
| 154 | */ |
||
| 155 | private $values = array(); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Config file data that has been loaded for the run. |
||
| 159 | * |
||
| 160 | * @var array<string, string> |
||
| 161 | */ |
||
| 162 | private static $configData = null; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Automatically discovered executable utility paths. |
||
| 166 | * |
||
| 167 | * @var array<string, string> |
||
| 168 | */ |
||
| 169 | private static $executablePaths = array(); |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the value of an inaccessible property. |
||
| 174 | * |
||
| 175 | * @param string $name The name of the property. |
||
| 176 | * |
||
| 177 | * @return mixed |
||
| 178 | * @throws RuntimeException If the setting name is invalid. |
||
| 179 | */ |
||
| 180 | public function __get($name) |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Set the value of an inaccessible property. |
||
| 193 | * |
||
| 194 | * @param string $name The name of the property. |
||
| 195 | * @param mixed $value The value of the property. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | * @throws RuntimeException If the setting name is invalid. |
||
| 199 | */ |
||
| 200 | public function __set($name, $value) |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Check if the value of an inaccessible property is set. |
||
| 245 | * |
||
| 246 | * @param string $name The name of the property. |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function __isset($name) |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Unset the value of an inaccessible property. |
||
| 259 | * |
||
| 260 | * @param string $name The name of the property. |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function __unset($name) |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Creates a Config object and populates it with command line values. |
||
| 273 | * |
||
| 274 | * @param array $cliArgs An array of values gathered from CLI args. |
||
| 275 | * @param bool $dieOnUnknownArg Whether or not to kill the process when an |
||
| 276 | * unknown command line arg is found. |
||
| 277 | * |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | public function __construct(array $cliArgs=array(), $dieOnUnknownArg=true) |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Set the command line values. |
||
| 349 | * |
||
| 350 | * @param array $args An array of command line arguments to set. |
||
| 351 | * |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | public function setCommandLineValues($args) |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * Restore default values for all possible command line arguments. |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function restoreDefaults() |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Processes a short (-e) command line argument. |
||
| 517 | * |
||
| 518 | * @param string $arg The command line argument. |
||
| 519 | * @param int $pos The position of the argument on the command line. |
||
| 520 | * |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | public function processShortArgument($arg, $pos) |
||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * Processes a long (--example) command line argument. |
||
| 595 | * |
||
| 596 | * @param string $arg The command line argument. |
||
| 597 | * @param int $pos The position of the argument on the command line. |
||
| 598 | * |
||
| 599 | * @return void |
||
| 600 | */ |
||
| 601 | public function processLongArgument($arg, $pos) |
||
| 602 | { |
||
| 603 | switch ($arg) { |
||
| 604 | case 'help': |
||
| 605 | $this->printUsage(); |
||
| 606 | exit(0); |
||
| 607 | case 'version': |
||
| 608 | echo 'Symplify\PHP7_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') '; |
||
| 609 | echo 'by Squiz (http://www.squiz.net)'.PHP_EOL; |
||
| 610 | exit(0); |
||
| 611 | case 'colors': |
||
| 612 | $this->colors = true; |
||
| 613 | $this->overriddenDefaults['colors'] = true; |
||
| 614 | break; |
||
| 615 | case 'no-colors': |
||
| 616 | $this->colors = false; |
||
| 617 | $this->overriddenDefaults['colors'] = true; |
||
| 618 | break; |
||
| 619 | case 'cache': |
||
| 620 | if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
||
| 621 | $this->cache = true; |
||
| 622 | $this->overriddenDefaults['cache'] = true; |
||
| 623 | } |
||
| 624 | break; |
||
| 625 | case 'no-cache': |
||
| 626 | $this->cache = false; |
||
| 627 | $this->overriddenDefaults['cache'] = true; |
||
| 628 | break; |
||
| 629 | case 'config-set': |
||
| 630 | View Code Duplication | if (isset($this->cliArgs[($pos + 1)]) === false |
|
| 631 | || isset($this->cliArgs[($pos + 2)]) === false |
||
| 632 | ) { |
||
| 633 | echo 'ERROR: Setting a config option requires a name and value'.PHP_EOL.PHP_EOL; |
||
| 634 | $this->printUsage(); |
||
| 635 | exit(0); |
||
| 636 | } |
||
| 637 | |||
| 638 | $key = $this->cliArgs[($pos + 1)]; |
||
| 639 | $value = $this->cliArgs[($pos + 2)]; |
||
| 640 | $current = self::getConfigData($key); |
||
| 641 | |||
| 642 | try { |
||
| 643 | $this->setConfigData($key, $value); |
||
| 644 | } catch (Exception $e) { |
||
| 645 | echo $e->getMessage().PHP_EOL; |
||
| 646 | exit(2); |
||
| 647 | } |
||
| 648 | |||
| 649 | if ($current === null) { |
||
| 650 | echo "Config value \"$key\" added successfully".PHP_EOL; |
||
| 651 | } else { |
||
| 652 | echo "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL; |
||
| 653 | } |
||
| 654 | exit(0); |
||
| 655 | case 'config-delete': |
||
| 656 | if (isset($this->cliArgs[($pos + 1)]) === false) { |
||
| 657 | echo 'ERROR: Deleting a config option requires the name of the option'.PHP_EOL.PHP_EOL; |
||
| 658 | $this->printUsage(); |
||
| 659 | exit(0); |
||
| 660 | } |
||
| 661 | |||
| 662 | $key = $this->cliArgs[($pos + 1)]; |
||
| 663 | $current = self::getConfigData($key); |
||
| 664 | if ($current === null) { |
||
| 665 | echo "Config value \"$key\" has not been set".PHP_EOL; |
||
| 666 | } else { |
||
| 667 | try { |
||
| 668 | $this->setConfigData($key, null); |
||
| 669 | } catch (Exception $e) { |
||
| 670 | echo $e->getMessage().PHP_EOL; |
||
| 671 | exit(2); |
||
| 672 | } |
||
| 673 | |||
| 674 | echo "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL; |
||
| 675 | } |
||
| 676 | exit(0); |
||
| 677 | case 'config-show': |
||
| 678 | $data = self::getAllConfigData(); |
||
| 679 | $this->printConfigData($data); |
||
| 680 | exit(0); |
||
| 681 | case 'runtime-set': |
||
| 682 | View Code Duplication | if (isset($this->cliArgs[($pos + 1)]) === false |
|
| 683 | || isset($this->cliArgs[($pos + 2)]) === false |
||
| 684 | ) { |
||
| 685 | echo 'ERROR: Setting a runtime config option requires a name and value'.PHP_EOL.PHP_EOL; |
||
| 686 | $this->printUsage(); |
||
| 687 | exit(0); |
||
| 688 | } |
||
| 689 | |||
| 690 | $key = $this->cliArgs[($pos + 1)]; |
||
| 691 | $value = $this->cliArgs[($pos + 2)]; |
||
| 692 | $this->cliArgs[($pos + 1)] = ''; |
||
| 693 | $this->cliArgs[($pos + 2)] = ''; |
||
| 694 | self::setConfigData($key, $value, true); |
||
| 695 | break; |
||
| 696 | default: |
||
| 697 | if (substr($arg, 0, 7) === 'sniffs=') { |
||
| 698 | $sniffs = explode(',', substr($arg, 7)); |
||
| 699 | foreach ($sniffs as $sniff) { |
||
| 700 | if (substr_count($sniff, '.') !== 2) { |
||
| 701 | echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL; |
||
| 702 | $this->printUsage(); |
||
| 703 | exit(2); |
||
| 704 | } |
||
| 705 | } |
||
| 706 | |||
| 707 | $this->sniffs = $sniffs; |
||
| 708 | $this->overriddenDefaults['sniffs'] = true; |
||
| 709 | } else if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false |
||
| 710 | && substr($arg, 0, 6) === 'cache=' |
||
| 711 | ) { |
||
| 712 | // Turn caching on. |
||
| 713 | $this->cache = true; |
||
| 714 | $this->overriddenDefaults['cache'] = true; |
||
| 715 | |||
| 716 | $this->cacheFile = Util\Common::realpath(substr($arg, 6)); |
||
| 717 | |||
| 718 | // It may not exist and return false instead. |
||
| 719 | View Code Duplication | if ($this->cacheFile === false) { |
|
| 720 | $this->cacheFile = substr($arg, 6); |
||
| 721 | |||
| 722 | $dir = dirname($this->cacheFile); |
||
| 723 | if (is_dir($dir) === false) { |
||
| 724 | echo 'ERROR: The specified cache file path "'.$this->cacheFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
||
| 725 | $this->printUsage(); |
||
| 726 | exit(2); |
||
| 727 | } |
||
| 728 | |||
| 729 | if ($dir === '.') { |
||
| 730 | // Passed report file is a file in the current directory. |
||
| 731 | $this->cacheFile = getcwd().'/'.basename($this->cacheFile); |
||
| 732 | } else { |
||
| 733 | $dir = Util\Common::realpath(getcwd().'/'.$dir); |
||
| 734 | if ($dir !== false) { |
||
| 735 | // Report file path is relative. |
||
| 736 | $this->cacheFile = $dir.'/'.basename($this->cacheFile); |
||
| 737 | } |
||
| 738 | } |
||
| 739 | }//end if |
||
| 740 | |||
| 741 | $this->overriddenDefaults['cacheFile'] = true; |
||
| 742 | |||
| 743 | if (is_dir($this->cacheFile) === true) { |
||
| 744 | echo 'ERROR: The specified cache file path "'.$this->cacheFile.'" is a directory'.PHP_EOL.PHP_EOL; |
||
| 745 | $this->printUsage(); |
||
| 746 | exit(2); |
||
| 747 | } |
||
| 748 | } else if (substr($arg, 0, 10) === 'bootstrap=') { |
||
| 749 | $files = explode(',', substr($arg, 10)); |
||
| 750 | $bootstrap = array(); |
||
| 751 | foreach ($files as $file) { |
||
| 752 | $path = Util\Common::realpath($file); |
||
| 753 | if ($path === false) { |
||
| 754 | echo 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL; |
||
| 755 | $this->printUsage(); |
||
| 756 | exit(2); |
||
| 757 | } |
||
| 758 | |||
| 759 | $bootstrap[] = $path; |
||
| 760 | } |
||
| 761 | |||
| 762 | $this->bootstrap = array_merge($this->bootstrap, $bootstrap); |
||
| 763 | $this->overriddenDefaults['bootstrap'] = true; |
||
| 764 | } else if (substr($arg, 0, 11) === 'stdin-path=') { |
||
| 765 | $this->stdinPath = Util\Common::realpath(substr($arg, 11)); |
||
| 766 | |||
| 767 | // It may not exist and return false instead, so use whatever they gave us. |
||
| 768 | if ($this->stdinPath === false) { |
||
| 769 | $this->stdinPath = trim(substr($arg, 11)); |
||
| 770 | } |
||
| 771 | |||
| 772 | $this->overriddenDefaults['stdinPath'] = true; |
||
| 773 | } else if (PHP_CodeSniffer_CBF === false && substr($arg, 0, 12) === 'report-file=') { |
||
| 774 | $this->reportFile = Util\Common::realpath(substr($arg, 12)); |
||
| 775 | |||
| 776 | // It may not exist and return false instead. |
||
| 777 | View Code Duplication | if ($this->reportFile === false) { |
|
| 778 | $this->reportFile = substr($arg, 12); |
||
| 779 | |||
| 780 | $dir = dirname($this->reportFile); |
||
| 781 | if (is_dir($dir) === false) { |
||
| 782 | echo 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
||
| 783 | $this->printUsage(); |
||
| 784 | exit(2); |
||
| 785 | } |
||
| 786 | |||
| 787 | if ($dir === '.') { |
||
| 788 | // Passed report file is a file in the current directory. |
||
| 789 | $this->reportFile = getcwd().'/'.basename($this->reportFile); |
||
| 790 | } else { |
||
| 791 | $dir = Util\Common::realpath(getcwd().'/'.$dir); |
||
| 792 | if ($dir !== false) { |
||
| 793 | // Report file path is relative. |
||
| 794 | $this->reportFile = $dir.'/'.basename($this->reportFile); |
||
| 795 | } |
||
| 796 | } |
||
| 797 | }//end if |
||
| 798 | |||
| 799 | $this->overriddenDefaults['reportFile'] = true; |
||
| 800 | |||
| 801 | if (is_dir($this->reportFile) === true) { |
||
| 802 | echo 'ERROR: The specified report file path "'.$this->reportFile.'" is a directory'.PHP_EOL.PHP_EOL; |
||
| 803 | $this->printUsage(); |
||
| 804 | exit(2); |
||
| 805 | } |
||
| 806 | } else if (substr($arg, 0, 13) === 'report-width=') { |
||
| 807 | if (isset($this->overriddenDefaults['reportWidth']) === true) { |
||
| 808 | break; |
||
| 809 | } |
||
| 810 | |||
| 811 | $this->reportWidth = substr($arg, 13); |
||
| 812 | $this->overriddenDefaults['reportWidth'] = true; |
||
| 813 | } else if (substr($arg, 0, 9) === 'basepath=') { |
||
| 814 | if (isset($this->overriddenDefaults['basepath']) === true) { |
||
| 815 | break; |
||
| 816 | } |
||
| 817 | |||
| 818 | $this->basepath = Util\Common::realpath(substr($arg, 9)); |
||
| 819 | |||
| 820 | // It may not exist and return false instead. |
||
| 821 | if ($this->basepath === false) { |
||
| 822 | $this->basepath = substr($arg, 9); |
||
| 823 | } |
||
| 824 | |||
| 825 | $this->overriddenDefaults['basepath'] = true; |
||
| 826 | |||
| 827 | if (is_dir($this->basepath) === false) { |
||
| 828 | echo 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL; |
||
| 829 | $this->printUsage(); |
||
| 830 | exit(2); |
||
| 831 | } |
||
| 832 | } else if ((substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-')) { |
||
| 833 | $reports = array(); |
||
| 834 | |||
| 835 | if ($arg[6] === '-') { |
||
| 836 | // This is a report with file output. |
||
| 837 | $split = strpos($arg, '='); |
||
| 838 | if ($split === false) { |
||
| 839 | $report = substr($arg, 7); |
||
| 840 | $output = null; |
||
| 841 | } else { |
||
| 842 | $report = substr($arg, 7, ($split - 7)); |
||
| 843 | $output = substr($arg, ($split + 1)); |
||
| 844 | if ($output === false) { |
||
| 845 | $output = null; |
||
| 846 | } else { |
||
| 847 | $dir = dirname($output); |
||
| 848 | if ($dir === '.') { |
||
| 849 | // Passed report file is a filename in the current directory. |
||
| 850 | $output = getcwd().'/'.basename($output); |
||
| 851 | } else { |
||
| 852 | $dir = Util\Common::realpath(getcwd().'/'.$dir); |
||
| 853 | if ($dir !== false) { |
||
| 854 | // Report file path is relative. |
||
| 855 | $output = $dir.'/'.basename($output); |
||
| 856 | } |
||
| 857 | } |
||
| 858 | }//end if |
||
| 859 | }//end if |
||
| 860 | |||
| 861 | $reports[$report] = $output; |
||
| 862 | } else { |
||
| 863 | // This is a single report. |
||
| 864 | if (isset($this->overriddenDefaults['reports']) === true) { |
||
| 865 | break; |
||
| 866 | } |
||
| 867 | |||
| 868 | $reportNames = explode(',', substr($arg, 7)); |
||
| 869 | foreach ($reportNames as $report) { |
||
| 870 | $reports[$report] = null; |
||
| 871 | } |
||
| 872 | }//end if |
||
| 873 | |||
| 874 | // Remove the default value so the CLI value overrides it. |
||
| 875 | if (isset($this->overriddenDefaults['reports']) === false) { |
||
| 876 | $this->reports = $reports; |
||
| 877 | } else { |
||
| 878 | $this->reports = array_merge($this->reports, $reports); |
||
| 879 | } |
||
| 880 | |||
| 881 | $this->overriddenDefaults['reports'] = true; |
||
| 882 | } else if (substr($arg, 0, 7) === 'filter=') { |
||
| 883 | if (isset($this->overriddenDefaults['filter']) === true) { |
||
| 884 | break; |
||
| 885 | } |
||
| 886 | |||
| 887 | $this->filter = substr($arg, 7); |
||
| 888 | $this->overriddenDefaults['filter'] = true; |
||
| 889 | } else if (substr($arg, 0, 9) === 'standard=') { |
||
| 890 | $standards = trim(substr($arg, 9)); |
||
| 891 | if ($standards !== '') { |
||
| 892 | $this->standards = explode(',', $standards); |
||
| 893 | } |
||
| 894 | |||
| 895 | $this->overriddenDefaults['standards'] = true; |
||
| 896 | } else if (substr($arg, 0, 11) === 'extensions=') { |
||
| 897 | $extensions = explode(',', substr($arg, 11)); |
||
| 898 | $newExtensions = array(); |
||
| 899 | foreach ($extensions as $ext) { |
||
| 900 | $slash = strpos($ext, '/'); |
||
| 901 | if ($slash !== false) { |
||
| 902 | // They specified the tokenizer too. |
||
| 903 | list($ext, $tokenizer) = explode('/', $ext); |
||
| 904 | $newExtensions[$ext] = strtoupper($tokenizer); |
||
| 905 | continue; |
||
| 906 | } |
||
| 907 | |||
| 908 | if (isset($this->extensions[$ext]) === true) { |
||
| 909 | $newExtensions[$ext] = $this->extensions[$ext]; |
||
| 910 | } else { |
||
| 911 | $newExtensions[$ext] = 'PHP'; |
||
| 912 | } |
||
| 913 | } |
||
| 914 | |||
| 915 | $this->extensions = $newExtensions; |
||
| 916 | $this->overriddenDefaults['extensions'] = true; |
||
| 917 | } else if (substr($arg, 0, 7) === 'suffix=') { |
||
| 918 | $this->suffix = explode(',', substr($arg, 7)); |
||
| 919 | $this->overriddenDefaults['suffix'] = true; |
||
| 920 | } else if (substr($arg, 0, 9) === 'parallel=') { |
||
| 921 | $this->parallel = max((int) substr($arg, 9), 1); |
||
| 922 | $this->overriddenDefaults['parallel'] = true; |
||
| 923 | } else if (substr($arg, 0, 9) === 'severity=') { |
||
| 924 | $this->errorSeverity = (int) substr($arg, 9); |
||
| 925 | $this->warningSeverity = $this->errorSeverity; |
||
| 926 | $this->overriddenDefaults['errorSeverity'] = true; |
||
| 927 | $this->overriddenDefaults['warningSeverity'] = true; |
||
| 928 | } else if (substr($arg, 0, 15) === 'error-severity=') { |
||
| 929 | $this->errorSeverity = (int) substr($arg, 15); |
||
| 930 | $this->overriddenDefaults['errorSeverity'] = true; |
||
| 931 | } else if (substr($arg, 0, 17) === 'warning-severity=') { |
||
| 932 | $this->warningSeverity = (int) substr($arg, 17); |
||
| 933 | $this->overriddenDefaults['warningSeverity'] = true; |
||
| 934 | } else if (substr($arg, 0, 7) === 'ignore=') { |
||
| 935 | // Split the ignore string on commas, unless the comma is escaped |
||
| 936 | // using 1 or 3 slashes (\, or \\\,). |
||
| 937 | $patterns = preg_split( |
||
| 938 | '/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', |
||
| 939 | substr($arg, 7) |
||
| 940 | ); |
||
| 941 | |||
| 942 | $ignored = array(); |
||
| 943 | foreach ($patterns as $pattern) { |
||
| 944 | $pattern = trim($pattern); |
||
| 945 | if ($pattern === '') { |
||
| 946 | continue; |
||
| 947 | } |
||
| 948 | |||
| 949 | $ignored[$pattern] = 'absolute'; |
||
| 950 | } |
||
| 951 | |||
| 952 | $this->ignored = $ignored; |
||
| 953 | $this->overriddenDefaults['ignored'] = true; |
||
| 954 | } else if (substr($arg, 0, 10) === 'generator=' |
||
| 955 | && PHP_CodeSniffer_CBF === false |
||
| 956 | ) { |
||
| 957 | $this->generator = substr($arg, 10); |
||
| 958 | $this->overriddenDefaults['generator'] = true; |
||
| 959 | } else if (substr($arg, 0, 9) === 'encoding=') { |
||
| 960 | $this->encoding = strtolower(substr($arg, 9)); |
||
| 961 | $this->overriddenDefaults['encoding'] = true; |
||
| 962 | } else if (substr($arg, 0, 10) === 'tab-width=') { |
||
| 963 | $this->tabWidth = (int) substr($arg, 10); |
||
| 964 | $this->overriddenDefaults['tabWidth'] = true; |
||
| 965 | } else { |
||
| 966 | if ($this->dieOnUnknownArg === false) { |
||
| 967 | $eqPos = strpos($arg, '='); |
||
| 968 | if ($eqPos === false) { |
||
| 969 | $this->values[$arg] = $arg; |
||
| 970 | } else { |
||
| 971 | $value = substr($arg, ($eqPos + 1)); |
||
| 972 | $arg = substr($arg, 0, $eqPos); |
||
| 973 | $this->values[$arg] = $value; |
||
| 974 | } |
||
| 975 | } else { |
||
| 976 | $this->processUnknownArgument('--'.$arg, $pos); |
||
| 977 | } |
||
| 978 | }//end if |
||
| 979 | |||
| 980 | break; |
||
| 981 | }//end switch |
||
| 982 | |||
| 983 | }//end processLongArgument() |
||
| 984 | |||
| 985 | |||
| 986 | /** |
||
| 987 | * Processes an unknown command line argument. |
||
| 988 | * |
||
| 989 | * Assumes all unknown arguments are files and folders to check. |
||
| 990 | * |
||
| 991 | * @param string $arg The command line argument. |
||
| 992 | * @param int $pos The position of the argument on the command line. |
||
| 993 | * |
||
| 994 | * @return void |
||
| 995 | */ |
||
| 996 | public function processUnknownArgument($arg, $pos) |
||
| 1031 | |||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Prints out the usage information for this script. |
||
| 1035 | * |
||
| 1036 | * @return void |
||
| 1037 | */ |
||
| 1038 | public function printUsage() |
||
| 1047 | |||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Prints out the usage information for PHPCS. |
||
| 1051 | * |
||
| 1052 | * @return void |
||
| 1053 | */ |
||
| 1054 | public function printPHPCSUsage() |
||
| 1108 | |||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Prints out the usage information for PHPCBF. |
||
| 1112 | * |
||
| 1113 | * @return void |
||
| 1114 | */ |
||
| 1115 | public function printPHPCBFUsage() |
||
| 1148 | |||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Get a single config value. |
||
| 1152 | * |
||
| 1153 | * @param string $key The name of the config value. |
||
| 1154 | * |
||
| 1155 | * @return string|null |
||
| 1156 | * @see setConfigData() |
||
| 1157 | * @see getAllConfigData() |
||
| 1158 | */ |
||
| 1159 | public static function getConfigData($key) |
||
| 1174 | |||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Get the path to an executable utility. |
||
| 1178 | * |
||
| 1179 | * @param string $name The name of the executable utility. |
||
| 1180 | * |
||
| 1181 | * @return string|null |
||
| 1182 | * @see getConfigData() |
||
| 1183 | */ |
||
| 1184 | public static function getExecutablePath($name) |
||
| 1210 | |||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Set a single config value. |
||
| 1214 | * |
||
| 1215 | * @param string $key The name of the config value. |
||
| 1216 | * @param string|null $value The value to set. If null, the config |
||
| 1217 | * entry is deleted, reverting it to the |
||
| 1218 | * default value. |
||
| 1219 | * @param boolean $temp Set this config data temporarily for this |
||
| 1220 | * script run. This will not write the config |
||
| 1221 | * data to the config file. |
||
| 1222 | * |
||
| 1223 | * @return bool |
||
| 1224 | * @see getConfigData() |
||
| 1225 | * @throws RuntimeException If the config file can not be written. |
||
| 1226 | */ |
||
| 1227 | public static function setConfigData($key, $value, $temp=false) |
||
| 1272 | |||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Get all config data. |
||
| 1276 | * |
||
| 1277 | * @return array<string, string> |
||
| 1278 | * @see getConfigData() |
||
| 1279 | */ |
||
| 1280 | public static function getAllConfigData() |
||
| 1301 | |||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Prints out the gathered config data. |
||
| 1305 | * |
||
| 1306 | * @param array $data The config data to print. |
||
| 1307 | * |
||
| 1308 | * @return void |
||
| 1309 | */ |
||
| 1310 | public function printConfigData($data) |
||
| 1332 | |||
| 1333 | |||
| 1334 | }//end class |
||
| 1335 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.