Complex classes like Cmd 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 Cmd, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Cmd |
||
49 | { |
||
50 | const EXIT_SUCCESS = 0; |
||
51 | const EXIT_FAILURE = 1; |
||
52 | const EXIT_EXCEPTION = 2; |
||
53 | |||
54 | /** |
||
55 | * Ascii-Art app logo |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private static $logo = ' __ __ |
||
60 | ____ / /_ ____ / /_ __ __ |
||
61 | / __ \/ __ \/ __ \/ __ \/ / / / |
||
62 | / /_/ / / / / /_/ / /_/ / /_/ / |
||
63 | / .___/_/ /_/ .___/_.___/\__,_/ |
||
64 | /_/ /_/ |
||
65 | '; |
||
66 | |||
67 | /** |
||
68 | * Is cmd executed from phar. |
||
69 | * |
||
70 | * @var boolean |
||
71 | */ |
||
72 | private $isPhar; |
||
73 | |||
74 | /** |
||
75 | * Is version string printed already. |
||
76 | * |
||
77 | * @var boolean |
||
78 | */ |
||
79 | private $isVersionStringPrinted = false; |
||
80 | |||
81 | /** |
||
82 | * List of given arguments |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | private $arguments; |
||
87 | |||
88 | /** |
||
89 | * Runs the application. |
||
90 | * |
||
91 | * @param array $args |
||
92 | */ |
||
93 | public function run(array $args) |
||
94 | { |
||
95 | $this->isPhar = defined('__PHPBU_PHAR__'); |
||
96 | $this->handleOpt($args); |
||
97 | $this->findConfiguration(); |
||
98 | |||
99 | $ret = self::EXIT_FAILURE; |
||
100 | $runner = new Runner(new Factory()); |
||
101 | |||
102 | try { |
||
103 | $result = $runner->run($this->createConfiguration()); |
||
104 | |||
105 | if ($result->wasSuccessful()) { |
||
106 | $ret = self::EXIT_SUCCESS; |
||
107 | } elseif ($result->errorCount() > 0) { |
||
108 | $ret = self::EXIT_EXCEPTION; |
||
109 | } |
||
110 | } catch (\Exception $e) { |
||
111 | echo $e->getMessage() . PHP_EOL; |
||
112 | $ret = self::EXIT_EXCEPTION; |
||
113 | } |
||
114 | |||
115 | exit($ret); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Check arguments and load configuration file. |
||
120 | * |
||
121 | * @param array $args |
||
122 | */ |
||
123 | protected function handleOpt(array $args) |
||
124 | { |
||
125 | try { |
||
126 | $parser = new Args($this->isPhar); |
||
127 | $options = $parser->getOptions($args); |
||
128 | $this->handleArgs($options); |
||
129 | } catch (Exception $e) { |
||
130 | $this->printError($e->getMessage(), true); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Handle the parsed command line options |
||
136 | * |
||
137 | * @param array $options |
||
138 | * @return void |
||
139 | */ |
||
140 | protected function handleArgs(array $options) |
||
141 | { |
||
142 | foreach ($options as $option => $argument) { |
||
143 | switch ($option) { |
||
144 | case '--bootstrap': |
||
145 | $this->arguments['bootstrap'] = $argument; |
||
146 | break; |
||
147 | case '--colors': |
||
148 | $this->arguments['colors'] = $argument; |
||
149 | break; |
||
150 | case '--configuration': |
||
151 | $this->arguments['configuration'] = $argument; |
||
152 | break; |
||
153 | case '--debug': |
||
154 | $this->arguments['debug'] = $argument; |
||
155 | break; |
||
156 | case '-h': |
||
157 | case '--help': |
||
158 | $this->printHelp(); |
||
159 | exit(self::EXIT_SUCCESS); |
||
160 | case 'include-path': |
||
161 | $this->arguments['include-path'] = $argument; |
||
162 | break; |
||
163 | case '--selfupdate': |
||
164 | case '--self-update': |
||
165 | $this->handleSelfUpdate(); |
||
166 | break; |
||
167 | case '--simulate': |
||
168 | $this->arguments['simulate'] = $argument; |
||
169 | break; |
||
170 | case '-v': |
||
171 | case '--verbose': |
||
172 | $this->arguments['verbose'] = true; |
||
173 | break; |
||
174 | case '-V': |
||
175 | case '--version': |
||
176 | $this->printVersionString(); |
||
177 | exit(self::EXIT_SUCCESS); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Try to find the configuration file. |
||
184 | */ |
||
185 | protected function findConfiguration() |
||
206 | |||
207 | /** |
||
208 | * Check directory for default configuration files phpbu.xml, phpbu.xml.dist. |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | protected function findConfigurationInDir() |
||
222 | |||
223 | /** |
||
224 | * Check default configuration files phpbu.xml, phpbu.xml.dist in current working directory. |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | protected function findConfigurationDefault() |
||
236 | |||
237 | /** |
||
238 | * Create a application configuration. |
||
239 | * |
||
240 | * @return \phpbu\App\Configuration |
||
241 | */ |
||
242 | protected function createConfiguration() |
||
269 | |||
270 | /** |
||
271 | * Override configuration settings with command line arguments. |
||
272 | * |
||
273 | * @param \phpbu\App\Configuration $configuration |
||
274 | * @param string $value |
||
275 | */ |
||
276 | protected function overrideConfigWithArgument(Configuration $configuration, $value) |
||
283 | |||
284 | /** |
||
285 | * Handle the phar self-update. |
||
286 | */ |
||
287 | protected function handleSelfUpdate() |
||
324 | |||
325 | /** |
||
326 | * Shows the current application version. |
||
327 | */ |
||
328 | private function printVersionString() |
||
337 | |||
338 | /** |
||
339 | * Show the phpbu logo |
||
340 | */ |
||
341 | protected function printLogo() |
||
345 | |||
346 | /** |
||
347 | * Show the help message. |
||
348 | */ |
||
349 | protected function printHelp() |
||
369 | |||
370 | /** |
||
371 | * Shows some given error message. |
||
372 | * |
||
373 | * @param string $message |
||
374 | * @param bool $hint |
||
375 | */ |
||
376 | private function printError($message, $hint = false) |
||
383 | |||
384 | /** |
||
385 | * Main method, is called by phpbu command and the phar file. |
||
386 | */ |
||
387 | public static function main() |
||
392 | } |
||
393 |