Complex classes like BaseCommand 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 BaseCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class BaseCommand |
||
34 | { |
||
35 | /** |
||
36 | * the command name |
||
37 | * |
||
38 | * @var string|null |
||
39 | */ |
||
40 | private $commandName = null; |
||
41 | |||
42 | /** |
||
43 | * config options |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $configs = []; |
||
48 | |||
49 | /** |
||
50 | * global configs |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $globalConfigs = []; |
||
55 | |||
56 | /** |
||
57 | * global options |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $globalOptions = []; |
||
62 | |||
63 | /** |
||
64 | * the command arguments |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | private $commandArguments = []; |
||
69 | |||
70 | /** |
||
71 | * the global command arguments |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $globalCommandArguments = []; |
||
76 | |||
77 | /** |
||
78 | * the command subject |
||
79 | * |
||
80 | * @var string|SubCommandCommand|null |
||
81 | */ |
||
82 | private $commandSubject = null; |
||
83 | |||
84 | /** |
||
85 | * the command second subject (i.e. for branch) |
||
86 | * |
||
87 | * @var string|SubCommandCommand|null |
||
88 | */ |
||
89 | private $commandSubject2 = null; |
||
90 | |||
91 | /** |
||
92 | * the path |
||
93 | * |
||
94 | * @var string|null |
||
95 | */ |
||
96 | private $path = null; |
||
97 | |||
98 | /** |
||
99 | * @var string|null |
||
100 | */ |
||
101 | private $binaryVersion; |
||
102 | |||
103 | /** |
||
104 | * @var Repository|null |
||
105 | */ |
||
106 | private $repo; |
||
107 | |||
108 | /** |
||
109 | * constructor |
||
110 | * |
||
111 | * should be called by all child classes' constructors to permit use of |
||
112 | * global configs, options and command arguments |
||
113 | * |
||
114 | * @param null|\GitElephant\Repository $repo The repo object to read |
||
115 | */ |
||
116 | 130 | public function __construct(Repository $repo = null) |
|
131 | |||
132 | /** |
||
133 | * Clear all previous variables |
||
134 | */ |
||
135 | 116 | public function clearAll(): void |
|
145 | |||
146 | /** |
||
147 | * Get a new instance of this command |
||
148 | * |
||
149 | * @param Repository $repo |
||
150 | * @return static |
||
151 | */ |
||
152 | 121 | public static function getInstance(Repository $repo = null) |
|
156 | |||
157 | /** |
||
158 | * Add the command name |
||
159 | * |
||
160 | * @param string $commandName the command name |
||
161 | */ |
||
162 | 117 | protected function addCommandName(string $commandName): void |
|
166 | |||
167 | /** |
||
168 | * Get command name |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 11 | protected function getCommandName(): string |
|
176 | |||
177 | /** |
||
178 | * Set Configs |
||
179 | * |
||
180 | * @param array|Map $configs the config variable. i.e. { "color.status" => "false", "color.diff" => "true" } |
||
181 | */ |
||
182 | 3 | public function addConfigs($configs): void |
|
188 | |||
189 | /** |
||
190 | * Set global configs |
||
191 | * |
||
192 | * @param array|Map $configs the config variable. i.e. { "color.status" => "false", "color.diff" => "true" } |
||
193 | */ |
||
194 | 100 | protected function addGlobalConfigs($configs): void |
|
202 | |||
203 | /** |
||
204 | * Set global option |
||
205 | * |
||
206 | * @param array|Map $options a global option |
||
207 | */ |
||
208 | 100 | protected function addGlobalOptions($options): void |
|
216 | |||
217 | /** |
||
218 | * Get Configs |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getConfigs(): array |
||
226 | |||
227 | /** |
||
228 | * Add a command argument |
||
229 | * |
||
230 | * @param string $commandArgument the command argument |
||
231 | */ |
||
232 | 108 | protected function addCommandArgument($commandArgument): void |
|
236 | |||
237 | /** |
||
238 | * Add a global command argument |
||
239 | * |
||
240 | * @param string $commandArgument the command argument |
||
241 | */ |
||
242 | 1 | protected function addGlobalCommandArgument($commandArgument): void |
|
248 | |||
249 | /** |
||
250 | * Get all added command arguments |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | 11 | protected function getCommandArguments(): array |
|
258 | |||
259 | /** |
||
260 | * Add a command subject |
||
261 | * |
||
262 | * @param SubCommandCommand|array|string $commandSubject the command subject |
||
263 | */ |
||
264 | 106 | protected function addCommandSubject($commandSubject): void |
|
268 | |||
269 | /** |
||
270 | * Add a second command subject |
||
271 | * |
||
272 | * @param SubCommandCommand|array|string $commandSubject2 the second command subject |
||
273 | */ |
||
274 | 22 | protected function addCommandSubject2($commandSubject2): void |
|
278 | |||
279 | /** |
||
280 | * Add a path to the git command |
||
281 | * |
||
282 | * @param string $path path |
||
283 | */ |
||
284 | 19 | protected function addPath($path): void |
|
288 | |||
289 | /** |
||
290 | * Normalize any valid option to its long name |
||
291 | * an provide a structure that can be more intelligently |
||
292 | * handled by other routines |
||
293 | * |
||
294 | * @param array $options command options |
||
295 | * @param array $switchOptions list of valid options that are switch like |
||
296 | * @param array $valueOptions list of valid options that must have a value assignment |
||
297 | * |
||
298 | * @return array Associative array of valid, normalized command options |
||
299 | */ |
||
300 | 15 | public function normalizeOptions( |
|
324 | |||
325 | /** |
||
326 | * Get the current command |
||
327 | * |
||
328 | * @return string |
||
329 | * @throws \RuntimeException |
||
330 | */ |
||
331 | 118 | public function getCommand(): string |
|
349 | |||
350 | /** |
||
351 | * get a string of CLI-formatted command arguments |
||
352 | * |
||
353 | * @return string The command argument string |
||
354 | */ |
||
355 | 118 | private function getCLICommandArguments(): string |
|
365 | |||
366 | /** |
||
367 | * get a string of CLI-formatted command name |
||
368 | * |
||
369 | * @return string The command name string |
||
370 | */ |
||
371 | 118 | private function getCLICommandName(): string |
|
375 | |||
376 | /** |
||
377 | * get a string of CLI-formatted configs |
||
378 | * |
||
379 | * @return string The config string |
||
380 | */ |
||
381 | 118 | private function getCLIConfigs(): string |
|
398 | |||
399 | /** |
||
400 | * get a string of CLI-formatted global options |
||
401 | * |
||
402 | * @return string The global options string |
||
403 | */ |
||
404 | 118 | private function getCLIGlobalOptions(): string |
|
415 | |||
416 | /** |
||
417 | * get a string of CLI-formatted path |
||
418 | * |
||
419 | * @return string The path string |
||
420 | */ |
||
421 | 118 | private function getCLIPath(): string |
|
430 | |||
431 | /** |
||
432 | * get a string of CLI-formatted subjects |
||
433 | * |
||
434 | * @throws \RuntimeException |
||
435 | * @return string The subjects string |
||
436 | */ |
||
437 | 118 | private function getCLISubjects(): string |
|
463 | |||
464 | /** |
||
465 | * Get the version of the git binary |
||
466 | * |
||
467 | * @return string|null |
||
468 | */ |
||
469 | 4 | public function getBinaryVersion(): ?string |
|
477 | } |
||
478 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.