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 | * @var string |
||
22 | */ |
||
23 | const VERSION = '3.0.0'; |
||
24 | |||
25 | /** |
||
26 | * An array of settings that PHPCS and PHPCBF accept. |
||
27 | * |
||
28 | * This array is not meant to be accessed directly. Instead, use the settings |
||
29 | * as if they are class member vars so the __get() and __set() magic methods |
||
30 | * can be used to validate the values. For example, to set the verbosity level to |
||
31 | * level 2, use $this->verbosity = 2; insteas of accessing this property directly. |
||
32 | * |
||
33 | * The list of settings are: |
||
34 | * |
||
35 | * string[] files The files and directories to check. |
||
36 | * string[] standards The standards being used for checking. |
||
37 | * int verbosity How verbose the output should be. |
||
38 | * 0: no unnecessary output |
||
39 | * 1: basic output for files being checked |
||
40 | * 2: ruleset and file parsing output |
||
41 | * 3: sniff execution output |
||
42 | * bool interactive Enable interactive checking mode. |
||
43 | * bool cache Enable the use of the file cache. |
||
44 | * bool explain Explain the coding standards. |
||
45 | * bool local Process local files in directories only (no recursion). |
||
46 | * bool showSources Show sniff source codes in report output. |
||
47 | * bool showProgress Show basic progress information while running. |
||
48 | * int tabWidth How many spaces each tab is worth. |
||
49 | * string[] sniffs The sniffs that should be used for checking. |
||
50 | * If empty, all sniffs in the supplied standards will be used. |
||
51 | * string[] ignored Regular expressions used to ignore files and folders during checking. |
||
52 | * string reportFile A file where the report output should be written. |
||
53 | * string filter The filter to use for the run. |
||
54 | * string[] bootstrap One of more files to include before the run begins. |
||
55 | * int reportWidth The maximum number of columns that reports should use for output. |
||
56 | * Set to "auto" for have this value changed to the width of the terminal. |
||
57 | * int errorSeverity The minimum severity an error must have to be displayed. |
||
58 | * int warningSeverity The minimum severity a warning must have to be displayed. |
||
59 | * bool recordErrors Record the content of error messages as well as error counts. |
||
60 | * string suffix A suffix to add to fixed files. |
||
61 | * string basepath A file system location to strip from the paths of files shown in reports. |
||
62 | * bool stdin Read content from STDIN instead of supplied files. |
||
63 | * string stdinContent Content passed directly to PHPCS on STDIN. |
||
64 | * string stdinPath The path to use for content passed on STDIN. |
||
65 | * |
||
66 | * array<string, string> extensions File extensions that should be checked, and what tokenizer to use. |
||
67 | * E.g., array('inc' => 'PHP'); |
||
68 | * array<string, string|null> reports The reports to use for printing output after the run. |
||
69 | * The format of the array is: |
||
70 | * array( |
||
71 | * 'reportName1' => 'outputFile', |
||
72 | * 'reportName2' => null, |
||
73 | * ); |
||
74 | * If the array value is NULL, the report will be written to the screen. |
||
75 | * |
||
76 | * @var array<string, mixed> |
||
77 | */ |
||
78 | private $settings = array( |
||
79 | 'files' => null, |
||
80 | 'standards' => null, |
||
81 | 'verbosity' => null, |
||
82 | 'interactive' => null, |
||
83 | 'explain' => null, |
||
84 | 'local' => null, |
||
85 | 'showSources' => null, |
||
86 | 'showProgress' => null, |
||
87 | 'tabWidth' => null, |
||
88 | 'extensions' => null, |
||
89 | 'sniffs' => null, |
||
90 | 'ignored' => null, |
||
91 | 'reportFile' => null, |
||
92 | 'filter' => null, |
||
93 | 'bootstrap' => null, |
||
94 | 'reports' => null, |
||
95 | 'basepath' => null, |
||
96 | 'reportWidth' => null, |
||
97 | 'errorSeverity' => null, |
||
98 | 'warningSeverity' => null, |
||
99 | 'recordErrors' => null, |
||
100 | 'suffix' => null, |
||
101 | 'stdin' => null, |
||
102 | 'stdinContent' => null, |
||
103 | 'stdinPath' => null, |
||
104 | ); |
||
105 | |||
106 | /** |
||
107 | * Whether or not to kill the process when an unknown command line arg is found. |
||
108 | * |
||
109 | * If FALSE, arguments that are not command line options or file/directory paths |
||
110 | * will be ignored and execution will continue. |
||
111 | * |
||
112 | * @var boolean |
||
113 | */ |
||
114 | public $dieOnUnknownArg; |
||
115 | |||
116 | /** |
||
117 | * The current command line arguments we are processing. |
||
118 | * |
||
119 | * @var string[] |
||
120 | */ |
||
121 | private $cliArgs = array(); |
||
122 | |||
123 | /** |
||
124 | * Command line values that the user has supplied directly. |
||
125 | * |
||
126 | * @var array<string, TRUE> |
||
127 | */ |
||
128 | private $overriddenDefaults = array(); |
||
129 | |||
130 | /** |
||
131 | * Unknown arguments |
||
132 | * |
||
133 | * @var array<mixed> |
||
134 | */ |
||
135 | private $values = array(); |
||
136 | |||
137 | /** |
||
138 | * Config file data that has been loaded for the run. |
||
139 | * |
||
140 | * @var array<string, string> |
||
141 | */ |
||
142 | private static $configData = null; |
||
|
|||
143 | |||
144 | /** |
||
145 | * Automatically discovered executable utility paths. |
||
146 | * |
||
147 | * @var array<string, string> |
||
148 | */ |
||
149 | private static $executablePaths = array(); |
||
150 | |||
151 | |||
152 | /** |
||
153 | * Get the value of an inaccessible property. |
||
154 | * |
||
155 | * @param string $name The name of the property. |
||
156 | * |
||
157 | * @return mixed |
||
158 | * @throws RuntimeException If the setting name is invalid. |
||
159 | */ |
||
160 | public function __get($name) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Set the value of an inaccessible property. |
||
173 | * |
||
174 | * @param string $name The name of the property. |
||
175 | * @param mixed $value The value of the property. |
||
176 | * |
||
177 | * @return void |
||
178 | * @throws RuntimeException If the setting name is invalid. |
||
179 | */ |
||
180 | 12 | public function __set($name, $value) |
|
181 | { |
||
182 | 12 | if (array_key_exists($name, $this->settings) === false) { |
|
183 | throw new RuntimeException("Can't __set() $name; setting doesn't exist"); |
||
184 | } |
||
185 | |||
186 | switch ($name) { |
||
187 | 12 | case 'reportWidth' : |
|
188 | // Support auto terminal width. |
||
189 | if ($value === 'auto' && preg_match('|\d+ (\d+)|', shell_exec('stty size 2>&1'), $matches) === 1) { |
||
190 | $value = (int) $matches[1]; |
||
191 | } else { |
||
192 | $value = (int) $value; |
||
193 | } |
||
194 | break; |
||
195 | 12 | case 'standards' : |
|
196 | 12 | $cleaned = array(); |
|
197 | |||
198 | // Check if the standard name is valid, or if the case is invalid. |
||
199 | 12 | $installedStandards = Util\Standards::getInstalledStandards(); |
|
200 | foreach ($value as $standard) { |
||
201 | foreach ($installedStandards as $validStandard) { |
||
202 | if (strtolower($standard) === strtolower($validStandard)) { |
||
203 | $standard = $validStandard; |
||
204 | break; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | $cleaned[] = $standard; |
||
209 | } |
||
210 | |||
211 | $value = $cleaned; |
||
212 | break; |
||
213 | default : |
||
214 | // No validation required. |
||
215 | 12 | break; |
|
216 | }//end switch |
||
217 | |||
218 | 12 | $this->settings[$name] = $value; |
|
219 | |||
220 | 12 | }//end __set() |
|
221 | |||
222 | |||
223 | /** |
||
224 | * Check if the value of an inaccessible property is set. |
||
225 | * |
||
226 | * @param string $name The name of the property. |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function __isset($name) |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Unset the value of an inaccessible property. |
||
239 | * |
||
240 | * @param string $name The name of the property. |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | public function __unset($name) |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Creates a Config object and populates it with command line values. |
||
253 | * |
||
254 | * @param array $cliArgs An array of values gathered from CLI args. |
||
255 | * @param bool $dieOnUnknownArg Whether or not to kill the process when an |
||
256 | * unknown command line arg is found. |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | 12 | public function __construct(array $cliArgs=array(), $dieOnUnknownArg=true) |
|
325 | |||
326 | |||
327 | /** |
||
328 | * Set the command line values. |
||
329 | * |
||
330 | * @param array $args An array of command line arguments to set. |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | public function setCommandLineValues($args) |
||
376 | |||
377 | |||
378 | /** |
||
379 | * Restore default values for all possible command line arguments. |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | 12 | public function restoreDefaults() |
|
384 | { |
||
385 | 12 | $this->files = array(); |
|
386 | 12 | $this->standards = array('PEAR'); |
|
387 | $this->verbosity = 0; |
||
388 | $this->colors = true; |
||
389 | $this->explain = false; |
||
390 | $this->local = false; |
||
391 | $this->showSources = false; |
||
392 | $this->showProgress = false; |
||
393 | $this->tabWidth = 0; |
||
394 | $this->extensions = array( |
||
395 | 'php' => 'PHP', |
||
396 | 'inc' => 'PHP', |
||
397 | 'js' => 'JS', |
||
398 | 'css' => 'CSS', |
||
399 | ); |
||
400 | $this->sniffs = array(); |
||
401 | $this->ignored = array(); |
||
402 | $this->filter = null; |
||
403 | $this->reports = array('full' => null); |
||
404 | $this->errorSeverity = 5; |
||
405 | $this->warningSeverity = 5; |
||
406 | $this->recordErrors = true; |
||
407 | $this->suffix = ''; |
||
408 | $this->stdin = false; |
||
409 | $this->stdinContent = null; |
||
410 | $this->stdinPath = null; |
||
411 | |||
412 | $standard = self::getConfigData('default_standard'); |
||
413 | if ($standard !== null) { |
||
414 | $this->standards = explode(',', $standard); |
||
415 | } |
||
416 | |||
417 | $tabWidth = self::getConfigData('tab_width'); |
||
418 | if ($tabWidth !== null) { |
||
419 | $this->tabWidth = (int) $tabWidth; |
||
420 | } |
||
421 | |||
422 | $severity = self::getConfigData('severity'); |
||
423 | if ($severity !== null) { |
||
424 | $this->errorSeverity = (int) $severity; |
||
425 | $this->warningSeverity = (int) $severity; |
||
426 | } |
||
427 | |||
428 | $severity = self::getConfigData('error_severity'); |
||
429 | if ($severity !== null) { |
||
430 | $this->errorSeverity = (int) $severity; |
||
431 | } |
||
432 | |||
433 | $severity = self::getConfigData('warning_severity'); |
||
434 | if ($severity !== null) { |
||
435 | $this->warningSeverity = (int) $severity; |
||
436 | } |
||
437 | |||
438 | $showWarnings = self::getConfigData('show_warnings'); |
||
439 | if ($showWarnings !== null) { |
||
440 | $showWarnings = (bool) $showWarnings; |
||
441 | if ($showWarnings === false) { |
||
442 | $this->warningSeverity = 0; |
||
443 | } |
||
444 | } |
||
445 | |||
446 | $showProgress = self::getConfigData('show_progress'); |
||
447 | if ($showProgress !== null) { |
||
448 | $this->showProgress = (bool) $showProgress; |
||
449 | } |
||
450 | |||
451 | if (defined('Symplify\PHP7_CodeSniffer_IN_TESTS') === false) { |
||
452 | $cache = self::getConfigData('cache'); |
||
453 | if ($cache !== null) { |
||
454 | $this->cache = (bool) $cache; |
||
455 | } |
||
456 | } |
||
457 | |||
458 | }//end restoreDefaults() |
||
459 | |||
460 | |||
461 | /** |
||
462 | * Processes a short (-e) command line argument. |
||
463 | * |
||
464 | * @param string $arg The command line argument. |
||
465 | * @param int $pos The position of the argument on the command line. |
||
466 | * |
||
467 | * @return void |
||
468 | */ |
||
469 | public function processShortArgument($arg, $pos) |
||
533 | |||
534 | |||
535 | /** |
||
536 | * Processes a long (--example) command line argument. |
||
537 | * |
||
538 | * @param string $arg The command line argument. |
||
539 | * @param int $pos The position of the argument on the command line. |
||
540 | * |
||
541 | * @return void |
||
542 | */ |
||
543 | public function processLongArgument($arg, $pos) |
||
694 | |||
695 | |||
696 | /** |
||
697 | * Processes an unknown command line argument. |
||
698 | * |
||
699 | * Assumes all unknown arguments are files and folders to check. |
||
700 | * |
||
701 | * @param string $arg The command line argument. |
||
702 | * @param int $pos The position of the argument on the command line. |
||
703 | * |
||
704 | * @return void |
||
705 | */ |
||
706 | public function processUnknownArgument($arg, $pos) |
||
741 | |||
742 | |||
743 | /** |
||
744 | * Prints out the usage information for this script. |
||
745 | * |
||
746 | * @return void |
||
747 | */ |
||
748 | public function printUsage() |
||
757 | |||
758 | |||
759 | /** |
||
760 | * Prints out the usage information for PHPCS. |
||
761 | * |
||
762 | * @return void |
||
763 | */ |
||
764 | public function printPHPCSUsage() |
||
799 | |||
800 | |||
801 | /** |
||
802 | * Prints out the usage information for PHPCBF. |
||
803 | * |
||
804 | * @return void |
||
805 | */ |
||
806 | public function printPHPCBFUsage() |
||
836 | |||
837 | |||
838 | /** |
||
839 | * Get a single config value. |
||
840 | * |
||
841 | * @param string $key The name of the config value. |
||
842 | * |
||
843 | * @return string|null |
||
844 | * @see setConfigData() |
||
845 | * @see getAllConfigData() |
||
846 | */ |
||
847 | 12 | public static function getConfigData($key) |
|
848 | { |
||
849 | 12 | $phpCodeSnifferConfig = self::getAllConfigData(); |
|
850 | |||
851 | if ($phpCodeSnifferConfig === null) { |
||
852 | return null; |
||
853 | } |
||
854 | |||
855 | if (isset($phpCodeSnifferConfig[$key]) === false) { |
||
856 | return null; |
||
857 | } |
||
858 | |||
859 | return $phpCodeSnifferConfig[$key]; |
||
860 | |||
861 | }//end getConfigData() |
||
862 | |||
863 | |||
864 | /** |
||
865 | * Get the path to an executable utility. |
||
866 | * |
||
867 | * @param string $name The name of the executable utility. |
||
868 | * |
||
869 | * @return string|null |
||
870 | * @see getConfigData() |
||
871 | */ |
||
872 | public static function getExecutablePath($name) |
||
898 | |||
899 | |||
900 | }//end class |
||
901 |
This check marks private properties in classes that are never used. Those properties can be removed.