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 extends Model |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Name of folder that contains module controllers |
||
| 23 | */ |
||
| 24 | const FOLDER_CONTROLLER = 'controllers'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Name of folder that contains module helpers |
||
| 28 | */ |
||
| 29 | const FOLDER_HELPER = 'helpers'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Name of folder that contains module models |
||
| 33 | */ |
||
| 34 | const FOLDER_MODEL = 'models'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Name of folder that contains module handlers |
||
| 38 | */ |
||
| 39 | const FOLDER_HANDLER = 'handlers'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Name of folder that contains module templates |
||
| 43 | */ |
||
| 44 | const FOLDER_TEMPLATE = 'templates'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Name of folder that contains module overrides |
||
| 48 | */ |
||
| 49 | const FOLDER_OVERRIDE = 'override'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Name of folder that contains JS assets |
||
| 53 | */ |
||
| 54 | const FOLDER_JS = 'js'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Name of folder that contains CSS assets |
||
| 58 | */ |
||
| 59 | const FOLDER_CSS = 'css'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Name of folder that contains images |
||
| 63 | */ |
||
| 64 | const FOLDER_IMAGE = 'image'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Zip class instance |
||
| 68 | * @var \gplcart\core\helpers\Zip $zip |
||
| 69 | */ |
||
| 70 | protected $zip; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Full path to a ZIP file containing generated module |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $file; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The current module folder |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $folder; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * An array of module data |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $data = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Constructor |
||
| 92 | * @param ZipHelper $zip |
||
| 93 | */ |
||
| 94 | public function __construct(ZipHelper $zip) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns an array of licenses and their URLs |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function getLicenses() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Generates module files and folders |
||
| 117 | * @param array $data |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function generate(array $data) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Create module manifest file |
||
| 136 | */ |
||
| 137 | protected function createManifest() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Creates various structure elements |
||
| 159 | */ |
||
| 160 | protected function createStructure() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Creates asset structure |
||
| 196 | */ |
||
| 197 | protected function createStructureAsset() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Creates a controller class |
||
| 211 | */ |
||
| 212 | protected function createStructureController() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Cretates module main class |
||
| 223 | */ |
||
| 224 | protected function createMainClass() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Creates a model class |
||
| 233 | */ |
||
| 234 | protected function createStructureModel() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Creates a helper class |
||
| 245 | */ |
||
| 246 | protected function createStructureHelper() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Creates a handler class |
||
| 257 | */ |
||
| 258 | protected function createStructureHandler() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Creates module overrides |
||
| 269 | */ |
||
| 270 | protected function createStructureOverride() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Creates a template sample |
||
| 281 | */ |
||
| 282 | protected function createStructureTemplate() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Pack module folder into zip file |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | protected function createZip() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns a path to zip file |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getZip() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Recursively creates folders |
||
| 319 | * @param string $folder |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | protected function prepareFolder($folder) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Prepares an array of data before rendering |
||
| 329 | * @param array $data |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | protected function setData(array &$data) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Renders a template |
||
| 346 | * @param string $template |
||
| 347 | * @return string|null |
||
| 348 | */ |
||
| 349 | protected function render($template) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Writes to a file using a template as an source |
||
| 365 | * @param string $file |
||
| 366 | * @param string $template |
||
| 367 | * @return null |
||
| 368 | */ |
||
| 369 | protected function write($file, $template) |
||
| 383 | |||
| 384 | } |
||
| 385 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.