Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Generator |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Module class instance |
||
| 23 | * @var \gplcart\core\Module $module |
||
| 24 | */ |
||
| 25 | protected $module; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Zip class instance |
||
| 29 | * @var \gplcart\core\helpers\Zip $zip |
||
| 30 | */ |
||
| 31 | protected $zip; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Full path to a ZIP file containing generated module |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $file; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The current module directory |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $directory; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * An array of module data |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $data = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param Module $module |
||
| 53 | * @param ZipHelper $zip |
||
| 54 | */ |
||
| 55 | public function __construct(Module $module, ZipHelper $zip) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns an array of licenses and their URLs |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getLicenses() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Generates module files and folders |
||
| 77 | * @param array $data |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function generate(array $data) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create module manifest file |
||
| 93 | */ |
||
| 94 | protected function createManifest() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Creates various structure elements |
||
| 116 | */ |
||
| 117 | protected function createStructure() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Creates asset structure |
||
| 166 | */ |
||
| 167 | protected function createStructureAsset() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Generate an image sample |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | protected function generateImage() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Creates translations structure |
||
| 201 | */ |
||
| 202 | protected function createStructureTranslation() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Creates a controller class |
||
| 214 | */ |
||
| 215 | protected function createStructureController() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Creates module main class |
||
| 226 | */ |
||
| 227 | protected function createMainClass() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Creates a model class |
||
| 236 | */ |
||
| 237 | protected function createStructureModel() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Creates a helper class |
||
| 248 | */ |
||
| 249 | protected function createStructureHelper() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Creates a handler class |
||
| 260 | */ |
||
| 261 | protected function createStructureHandler() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Creates module overrides |
||
| 272 | */ |
||
| 273 | protected function createStructureOverride() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Creates a template sample |
||
| 284 | */ |
||
| 285 | protected function createStructureTemplate() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Creates files for unit testing |
||
| 296 | */ |
||
| 297 | protected function createStructureTests() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Creates config files structure |
||
| 311 | */ |
||
| 312 | protected function createStructureConfigFiles() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Pack module folder into zip file |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | protected function createZip() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns a path to zip file |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getZip() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Recursively creates folders |
||
| 349 | * @param string $folder |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | protected function prepareFolder($folder) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Prepares an array of data before rendering |
||
| 359 | * @param array $data |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | protected function setData(array &$data) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Renders a template |
||
| 377 | * @param string $template |
||
| 378 | * @return string|null |
||
| 379 | */ |
||
| 380 | protected function render($template) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Writes to a file using a template as an source |
||
| 396 | * @param string $file |
||
| 397 | * @param string $template |
||
| 398 | * @return null|bool |
||
| 399 | */ |
||
| 400 | protected function write($file, $template) |
||
| 414 | |||
| 415 | } |
||
| 416 |