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 | * Config class instance |
||
| 23 | * @var \gplcart\core\Config $config |
||
| 24 | */ |
||
| 25 | protected $config; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Name of folder that contains module controllers |
||
| 29 | */ |
||
| 30 | const FOLDER_CONTROLLER = 'controllers'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Name of folder that contains module helpers |
||
| 34 | */ |
||
| 35 | const FOLDER_HELPER = 'helpers'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Name of folder that contains module models |
||
| 39 | */ |
||
| 40 | const FOLDER_MODEL = 'models'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Name of folder that contains module handlers |
||
| 44 | */ |
||
| 45 | const FOLDER_HANDLER = 'handlers'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Name of folder that contains module templates |
||
| 49 | */ |
||
| 50 | const FOLDER_TEMPLATE = 'templates'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Name of folder that contains module overrides |
||
| 54 | */ |
||
| 55 | const FOLDER_OVERRIDE = 'override'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Name of folder that contains module translations |
||
| 59 | */ |
||
| 60 | const FOLDER_LOCALE = 'translations'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Name of folder that contains module tests |
||
| 64 | */ |
||
| 65 | const FOLDER_TEST = 'tests'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Name of folder that contains JS assets |
||
| 69 | */ |
||
| 70 | const FOLDER_JS = 'js'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Name of folder that contains CSS assets |
||
| 74 | */ |
||
| 75 | const FOLDER_CSS = 'css'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Name of folder that contains images |
||
| 79 | */ |
||
| 80 | const FOLDER_IMAGE = 'image'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Zip class instance |
||
| 84 | * @var \gplcart\core\helpers\Zip $zip |
||
| 85 | */ |
||
| 86 | protected $zip; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Full path to a ZIP file containing generated module |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $file; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The current module directory |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $directory; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * An array of module data |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $data = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param Config $config |
||
| 108 | * @param ZipHelper $zip |
||
| 109 | */ |
||
| 110 | public function __construct(Config $config, ZipHelper $zip) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns an array of licenses and their URLs |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function getLicenses() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Generates module files and folders |
||
| 132 | * @param array $data |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function generate(array $data) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create module manifest file |
||
| 148 | */ |
||
| 149 | protected function createManifest() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Creates various structure elements |
||
| 171 | */ |
||
| 172 | protected function createStructure() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Creates asset structure |
||
| 218 | */ |
||
| 219 | protected function createStructureAsset() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Generate an image sample |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | protected function generateImage() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Creates locale structure |
||
| 253 | */ |
||
| 254 | protected function createStructureLocale() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Creates a controller class |
||
| 266 | */ |
||
| 267 | protected function createStructureController() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Creates module main class |
||
| 278 | */ |
||
| 279 | protected function createMainClass() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Creates a model class |
||
| 288 | */ |
||
| 289 | protected function createStructureModel() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Creates a helper class |
||
| 300 | */ |
||
| 301 | protected function createStructureHelper() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Creates a handler class |
||
| 312 | */ |
||
| 313 | protected function createStructureHandler() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Creates module overrides |
||
| 324 | */ |
||
| 325 | protected function createStructureOverride() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Creates a template sample |
||
| 336 | */ |
||
| 337 | protected function createStructureTemplate() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Creates files for unit testing |
||
| 348 | */ |
||
| 349 | protected function createStructureTests() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Pack module folder into zip file |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | protected function createZip() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns a path to zip file |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getZip() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Recursively creates folders |
||
| 389 | * @param string $folder |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | protected function prepareFolder($folder) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Prepares an array of data before rendering |
||
| 399 | * @param array $data |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | protected function setData(array &$data) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Renders a template |
||
| 416 | * @param string $template |
||
| 417 | * @return string|null |
||
| 418 | */ |
||
| 419 | protected function render($template) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Writes to a file using a template as an source |
||
| 435 | * @param string $file |
||
| 436 | * @param string $template |
||
| 437 | * @return null|bool |
||
| 438 | */ |
||
| 439 | protected function write($file, $template) |
||
| 453 | |||
| 454 | } |
||
| 455 |