Complex classes like MakeCommand 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 MakeCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | abstract class MakeCommand extends SilverstripeCommand |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * @config |
||
11 | * @var array |
||
12 | */ |
||
13 | private static $default_dirs = [ |
||
14 | 'Command' => 'mysite/code/commands', |
||
15 | 'Controller' => 'mysite/code/controllers', |
||
16 | 'ControllerExtension' => 'mysite/code/extensions', |
||
17 | 'DataObject' => 'mysite/code/dataobjects', |
||
18 | 'DataExtension' => 'mysite/code/extensions', |
||
19 | 'Extension' => 'mysite/code/extensions', |
||
20 | 'Form' => 'mysite/code/forms', |
||
21 | 'Page' => 'mysite/code/pages', |
||
22 | 'Test' => 'mysite/tests', |
||
23 | 'Theme' => 'themes' |
||
24 | ]; |
||
25 | |||
26 | public function fire() |
||
45 | |||
46 | /** |
||
47 | * @param string $target |
||
48 | * @param string $class |
||
49 | * @return bool |
||
50 | */ |
||
51 | protected function classOrFileExists($target, $class) |
||
62 | |||
63 | /** |
||
64 | * @param string $target |
||
65 | * @param string $class |
||
66 | */ |
||
67 | protected function writeFile($target, $class) |
||
75 | |||
76 | protected function clearCache() |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getPhpStub() |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getTemplateStub() |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getTargetDirectory() |
||
123 | |||
124 | /** |
||
125 | * @param string $customDir |
||
126 | * @param array $defaultDirs |
||
127 | * @return string|array |
||
128 | */ |
||
129 | protected function setTargetDirectoriesByString($customDir, $defaultDirs) |
||
143 | |||
144 | /** |
||
145 | * @param array $customDirs |
||
146 | * @param array $defaultDirs |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function mergeCustomDirectoriesWithDefault($customDirs, $defaultDirs) |
||
158 | |||
159 | /** |
||
160 | * The absolute file path |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getTargetFile($class) |
||
168 | |||
169 | /** |
||
170 | * Gets the Classname to generate from the called class like |
||
171 | * MakeDataObjectCommand => DataObject class |
||
172 | * MakeFormCommand => Form class |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getCommandClass() |
||
184 | |||
185 | /** |
||
186 | * @throws InvalidArgumentException |
||
187 | * @return array |
||
188 | */ |
||
189 | protected function getTargetDirectoryByOptionOrConfig() |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | protected function getStubFilePath($commandClass) |
||
216 | |||
217 | /** |
||
218 | * @return string |
||
219 | */ |
||
220 | protected function getConsoleStubPath($commandClass) |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | protected function getMySiteStubPath($commandClass) |
||
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function getCustomStubPath($commandClass) |
||
245 | |||
246 | /** |
||
247 | * @param $class |
||
248 | * @return bool |
||
249 | */ |
||
250 | protected function fileExists($class) |
||
254 | |||
255 | /** |
||
256 | * @param $class |
||
257 | * @return bool |
||
258 | */ |
||
259 | protected function classExists($class) |
||
263 | |||
264 | /** |
||
265 | * Build the directory for the class if necessary. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | protected function makeDirectory() |
||
279 | |||
280 | /** |
||
281 | * Replace the class name for the given stub. |
||
282 | * |
||
283 | * @param string $class |
||
284 | * @return string |
||
285 | */ |
||
286 | protected function buildClass($class) |
||
290 | |||
291 | /** |
||
292 | * Get the desired class name from the input. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected function getNameInput() |
||
300 | |||
301 | protected function getOptions() |
||
308 | |||
309 | /** |
||
310 | * Get the console command arguments. |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getArguments() |
||
320 | } |
||
321 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.