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) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Creates various structure elements |
||
| 139 | */ |
||
| 140 | protected function createStructure() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Creates asset structure |
||
| 176 | */ |
||
| 177 | protected function createStructureAsset() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Creates a controller class |
||
| 191 | */ |
||
| 192 | protected function createStructureController() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Cretates module main class |
||
| 203 | */ |
||
| 204 | protected function createMainClass() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Creates a model class |
||
| 213 | */ |
||
| 214 | protected function createStructureModel() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Creates a helper class |
||
| 225 | */ |
||
| 226 | protected function createStructureHelper() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Creates a handler class |
||
| 237 | */ |
||
| 238 | protected function createStructureHandler() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Creates module overrides |
||
| 249 | */ |
||
| 250 | protected function createStructureOverride() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Creates a template sample |
||
| 261 | */ |
||
| 262 | protected function createStructureTemplate() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Pack module folder into zip file |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | protected function createZip() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns a path to zip file |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getZip() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set a full path to the module folder |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | protected function setModuleFolder() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Recursively creates folders |
||
| 310 | * @param string $folder |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | protected function prepareFolder($folder) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Prepares an array of data before rendering |
||
| 320 | * @param array $data |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | protected function setData(array &$data) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Renders a template |
||
| 336 | * @param string $template |
||
| 337 | * @return string|null |
||
| 338 | */ |
||
| 339 | protected function render($template) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Writes to a file using a template as an source |
||
| 355 | * @param string $file |
||
| 356 | * @param string $template |
||
| 357 | * @return null |
||
| 358 | */ |
||
| 359 | protected function write($file, $template) |
||
| 373 | |||
| 374 | } |
||
| 375 |
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.