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 module translations |
||
| 53 | */ |
||
| 54 | const FOLDER_LOCALE = 'locale'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Name of folder that contains JS assets |
||
| 58 | */ |
||
| 59 | const FOLDER_JS = 'js'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Name of folder that contains CSS assets |
||
| 63 | */ |
||
| 64 | const FOLDER_CSS = 'css'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Name of folder that contains images |
||
| 68 | */ |
||
| 69 | const FOLDER_IMAGE = 'image'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Zip class instance |
||
| 73 | * @var \gplcart\core\helpers\Zip $zip |
||
| 74 | */ |
||
| 75 | protected $zip; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Full path to a ZIP file containing generated module |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $file; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The current module directory |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $directory; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * An array of module data |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $data = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param ZipHelper $zip |
||
| 97 | */ |
||
| 98 | public function __construct(ZipHelper $zip) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns an array of licenses and their URLs |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function getLicenses() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Generates module files and folders |
||
| 121 | * @param array $data |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function generate(array $data) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Create module manifest file |
||
| 137 | */ |
||
| 138 | protected function createManifest() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Creates various structure elements |
||
| 160 | */ |
||
| 161 | protected function createStructure() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Creates asset structure |
||
| 204 | */ |
||
| 205 | protected function createStructureAsset() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Generate an image sample |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | protected function generateImage() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Creates locale structure |
||
| 239 | */ |
||
| 240 | protected function createStructureLocale() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Creates a controller class |
||
| 252 | */ |
||
| 253 | protected function createStructureController() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Creates module main class |
||
| 264 | */ |
||
| 265 | protected function createMainClass() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Creates a model class |
||
| 274 | */ |
||
| 275 | protected function createStructureModel() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Creates a helper class |
||
| 286 | */ |
||
| 287 | protected function createStructureHelper() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Creates a handler class |
||
| 298 | */ |
||
| 299 | protected function createStructureHandler() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Creates module overrides |
||
| 310 | */ |
||
| 311 | protected function createStructureOverride() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Creates a template sample |
||
| 322 | */ |
||
| 323 | protected function createStructureTemplate() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Pack module folder into zip file |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | protected function createZip() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns a path to zip file |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getZip() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Recursively creates folders |
||
| 360 | * @param string $folder |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | protected function prepareFolder($folder) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Prepares an array of data before rendering |
||
| 370 | * @param array $data |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | protected function setData(array &$data) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Renders a template |
||
| 387 | * @param string $template |
||
| 388 | * @return string|null |
||
| 389 | */ |
||
| 390 | protected function render($template) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Writes to a file using a template as an source |
||
| 406 | * @param string $file |
||
| 407 | * @param string $template |
||
| 408 | * @return null|bool |
||
| 409 | */ |
||
| 410 | protected function write($file, $template) |
||
| 424 | |||
| 425 | } |
||
| 426 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.