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 |
||
| 22 | class Generator |
||
| 23 | { |
||
| 24 | use \Jaxon\Features\Config; |
||
| 25 | use \Jaxon\Features\Minifier; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The response type. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const RESPONSE_TYPE = 'JSON'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The objects that generate code |
||
| 36 | * |
||
| 37 | * @var array<GeneratorContract> |
||
| 38 | */ |
||
| 39 | protected $aGenerators = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The Jaxon template engine |
||
| 43 | * |
||
| 44 | * @var TemplateEngine |
||
| 45 | */ |
||
| 46 | protected $xTemplateEngine; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * HTML tags to include CSS code and files into the page |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $sCssCode = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * HTML tags to include javascript code and files into the page |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $sJsCode = ''; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Javascript code to include into the page |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $sJsScript = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Javascript code to execute after page load |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $sJsReadyScript = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Code already generated |
||
| 78 | * |
||
| 79 | * @var boolean |
||
| 80 | */ |
||
| 81 | protected $sCodeGenerated = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Default library URL |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $sJsLibraryUrl = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The constructor |
||
| 92 | * |
||
| 93 | * @param TemplateEngine $xTemplateEngine The template engine |
||
| 94 | */ |
||
| 95 | public function __construct(TemplateEngine $xTemplateEngine) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Add a new generator to the list |
||
| 102 | * |
||
| 103 | * @param GeneratorContract $xGenerator The code generator |
||
| 104 | * @param integer $nPriority The desired priority, used to order the plugins |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function addGenerator(GeneratorContract $xGenerator, $nPriority) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Generate a hash for all the javascript code generated by the library |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | private function getHash() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | private function generateCode() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the base URI of the Jaxon library javascript files |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | private function getJsLibUri() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the extension of the Jaxon library javascript files |
||
| 180 | * |
||
| 181 | * The returned string is '.min.js' if the files are minified. |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | private function getJsLibExt() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
| 196 | * |
||
| 197 | * @return boolean |
||
| 198 | */ |
||
| 199 | public function canExportJavascript() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getJs() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function getCss() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the correspondances between previous and current config options |
||
| 270 | * |
||
| 271 | * They are used to keep the deprecated config options working. |
||
| 272 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | private function getOptionVars() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the javascript code to be sent to the browser |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | private function _getScript() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Write javascript files and return the corresponding URI |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | private function _writeFiles() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the javascript code to be sent to the browser |
||
| 373 | * |
||
| 374 | * Also call each of the request plugins giving them the opportunity |
||
| 375 | * to output some javascript to the page being generated. |
||
| 376 | * This is called only when the page is being loaded initially. |
||
| 377 | * This is not called when processing a request. |
||
| 378 | * |
||
| 379 | * @param boolean $bIncludeJs Also get the JS files |
||
| 380 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function getScript($bIncludeJs, $bIncludeCss) |
||
| 419 | } |
||
| 420 |