Complex classes like CodeGenerator 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 CodeGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class CodeGenerator |
||
| 21 | { |
||
| 22 | use \Jaxon\Features\Config; |
||
| 23 | use \Jaxon\Features\Minifier; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The response type. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | const RESPONSE_TYPE = 'JSON'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The plugin manager |
||
| 34 | * |
||
| 35 | * @var Manager |
||
| 36 | */ |
||
| 37 | protected $xPluginManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The Jaxon template engine |
||
| 41 | * |
||
| 42 | * @var TemplateEngine |
||
| 43 | */ |
||
| 44 | protected $xTemplate; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Generated CSS code |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $sCssCode = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Generated Javascript code |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $sJsCode = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Generated Javascript ready script |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $sJsScript = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Code already generated |
||
| 69 | * |
||
| 70 | * @var boolean |
||
| 71 | */ |
||
| 72 | protected $sCodeGenerated = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Default library URL |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $sJsLibraryUrl = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The constructor |
||
| 83 | * |
||
| 84 | * @param Manager $xPluginManager |
||
| 85 | */ |
||
| 86 | public function __construct(Manager $xPluginManager, TemplateEngine $xTemplate) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the base URI of the Jaxon library javascript files |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | private function getJsLibUri() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the extension of the Jaxon library javascript files |
||
| 104 | * |
||
| 105 | * The returned string is '.min.js' if the files are minified. |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | private function getJsLibExt() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
| 120 | * |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | public function canExportJavascript() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Generate a hash for all the javascript code generated by the library |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | private function generateHash() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 165 | * |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | private function makePluginsCode() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getJs() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getCss() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the correspondances between previous and current config options |
||
| 263 | * |
||
| 264 | * They are used to keep the deprecated config options working. |
||
| 265 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | private function getOptionVars() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the javascript code to be sent to the browser |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | private function _getScript() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Write javascript files and return the corresponding URI |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function _writeFiles() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the javascript code to be sent to the browser |
||
| 363 | * |
||
| 364 | * Also call each of the request plugins giving them the opportunity |
||
| 365 | * to output some javascript to the page being generated. |
||
| 366 | * This is called only when the page is being loaded initially. |
||
| 367 | * This is not called when processing a request. |
||
| 368 | * |
||
| 369 | * @param boolean $bIncludeJs Also get the JS files |
||
| 370 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function getScript($bIncludeJs = false, $bIncludeCss = false) |
||
| 409 | } |
||
| 410 |