Complex classes like AbstractMakeCommand 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 AbstractMakeCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | abstract class AbstractMakeCommand extends SilverstripeCommand |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * @config |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $default_dirs = [ |
||
15 | 'Command' => 'mysite/code/commands', |
||
16 | 'Controller' => 'mysite/code/controllers', |
||
17 | 'ControllerExtension' => 'mysite/code/extensions', |
||
18 | 'DataObject' => 'mysite/code/dataobjects', |
||
19 | 'DataExtension' => 'mysite/code/extensions', |
||
20 | 'Extension' => 'mysite/code/extensions', |
||
21 | 'Form' => 'mysite/code/forms', |
||
22 | 'Page' => 'mysite/code/pages', |
||
23 | 'Test' => 'mysite/tests', |
||
24 | 'Theme' => 'themes', |
||
25 | ]; |
||
26 | |||
27 | public function fire() |
||
47 | |||
48 | /** |
||
49 | * @param string $target |
||
50 | * @param string $class |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | protected function classOrFileExists($target, $class) |
||
68 | |||
69 | /** |
||
70 | * @param string $target |
||
71 | * @param string $class |
||
72 | */ |
||
73 | protected function writeFile($target, $class) |
||
81 | |||
82 | protected function clearCache() |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getPhpStub() |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getTemplateStub() |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getTargetDirectory() |
||
129 | |||
130 | /** |
||
131 | * @param string $customDir |
||
132 | * @param array $defaultDirs |
||
133 | * |
||
134 | * @return string|array |
||
135 | */ |
||
136 | protected function setTargetDirectoriesByString($customDir, $defaultDirs) |
||
150 | |||
151 | /** |
||
152 | * @param array $customDirs |
||
153 | * @param array $defaultDirs |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | protected function mergeCustomDirectoriesWithDefault($customDirs, $defaultDirs) |
||
167 | |||
168 | /** |
||
169 | * The absolute file path. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getTargetFile($class) |
||
177 | |||
178 | /** |
||
179 | * Gets the Classname to generate from the called class like |
||
180 | * MakeDataObjectCommand => DataObject class |
||
181 | * MakeFormCommand => Form class. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getCommandClass() |
||
193 | |||
194 | /** |
||
195 | * @return string|array|int|float|bool |
||
196 | */ |
||
197 | protected function getTargetDirectoryByOptionOrConfig() |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | protected function getStubFilePath($stubName) |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | protected function getConsoleStubPath($stubName) |
||
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function getMySiteStubPath($stubName) |
||
240 | |||
241 | /** |
||
242 | * @return string |
||
243 | */ |
||
244 | protected function getCustomStubPath($stubName) |
||
254 | |||
255 | /** |
||
256 | * @param $class |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | protected function fileExists($class) |
||
264 | |||
265 | /** |
||
266 | * @param $class |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | protected function classExists($class) |
||
278 | |||
279 | /** |
||
280 | * Build the directory for the class if necessary. |
||
281 | */ |
||
282 | protected function makeDirectory() |
||
292 | |||
293 | /** |
||
294 | * Replace the class name for the given stub. |
||
295 | * |
||
296 | * @param string $class |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | protected function buildClass($class) |
||
304 | |||
305 | /** |
||
306 | * Get the desired class name from the input. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | protected function getNameInput() |
||
314 | |||
315 | protected function getOptions() |
||
322 | |||
323 | /** |
||
324 | * Get the console command arguments. |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | protected function getArguments() |
||
334 | } |
||
335 |
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.