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 string |
||
| 167 | */ |
||
| 168 | private function makePluginsCode() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getJs() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getCss() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the correspondances between previous and current config options |
||
| 262 | * |
||
| 263 | * They are used to keep the deprecated config options working. |
||
| 264 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | private function getOptionVars() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the javascript code to be sent to the browser |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private function _getScript() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the javascript code to be sent to the browser |
||
| 313 | * |
||
| 314 | * Also call each of the request plugins giving them the opportunity |
||
| 315 | * to output some javascript to the page being generated. |
||
| 316 | * This is called only when the page is being loaded initially. |
||
| 317 | * This is not called when processing a request. |
||
| 318 | * |
||
| 319 | * @param boolean $bIncludeJs Also get the JS files |
||
| 320 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getScript($bIncludeJs = false, $bIncludeCss = false) |
||
| 404 | } |
||
| 405 |