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 | * Generator constructor. |
||
| 53 | * @param Module $module |
||
| 54 | * @param Zip $zip |
||
| 55 | */ |
||
| 56 | public function __construct(Module $module, Zip $zip) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns an array of licenses and their URLs |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getLicenses() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Generates module files and folders |
||
| 78 | * @param array $data |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function generate(array $data) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Create module manifest file |
||
| 94 | */ |
||
| 95 | protected function createManifest() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Creates various structure elements |
||
| 117 | */ |
||
| 118 | protected function createStructure() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Creates asset structure |
||
| 167 | */ |
||
| 168 | protected function createStructureAsset() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Generate an image sample |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | protected function generateImage() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Creates translations structure |
||
| 202 | */ |
||
| 203 | protected function createStructureTranslation() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Creates a controller class |
||
| 215 | */ |
||
| 216 | protected function createStructureController() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Creates module main class |
||
| 227 | */ |
||
| 228 | protected function createMainClass() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Creates a model class |
||
| 237 | */ |
||
| 238 | protected function createStructureModel() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Creates a helper class |
||
| 249 | */ |
||
| 250 | protected function createStructureHelper() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Creates a handler class |
||
| 261 | */ |
||
| 262 | protected function createStructureHandler() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Creates module overrides |
||
| 273 | */ |
||
| 274 | protected function createStructureOverride() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Creates a template sample |
||
| 285 | */ |
||
| 286 | protected function createStructureTemplate() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Creates files for unit testing |
||
| 297 | */ |
||
| 298 | protected function createStructureTests() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Creates config files structure |
||
| 312 | */ |
||
| 313 | protected function createStructureConfigFiles() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Pack module folder into zip file |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | protected function createZip() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns a path to zip file |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function getZip() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Recursively creates folders |
||
| 350 | * @param string $folder |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | protected function prepareFolder($folder) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Prepares an array of data before rendering |
||
| 360 | * @param array $data |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | protected function setData(array &$data) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Renders a template |
||
| 378 | * @param string $template |
||
| 379 | * @return string|null |
||
| 380 | */ |
||
| 381 | protected function render($template) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Writes to a file using a template as an source |
||
| 397 | * @param string $file |
||
| 398 | * @param string $template |
||
| 399 | * @return null|bool |
||
| 400 | */ |
||
| 401 | protected function write($file, $template) |
||
| 415 | |||
| 416 | } |
||
| 417 |