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 | * Javascript code to include in HTML code and execute after page load |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $sJsInlineScript = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Code already generated |
||
| 85 | * |
||
| 86 | * @var boolean |
||
| 87 | */ |
||
| 88 | protected $sCodeGenerated = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Default library URL |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $sJsLibraryUrl = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The constructor |
||
| 99 | * |
||
| 100 | * @param TemplateEngine $xTemplateEngine The template engine |
||
| 101 | */ |
||
| 102 | public function __construct(TemplateEngine $xTemplateEngine) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add a new generator to the list |
||
| 109 | * |
||
| 110 | * @param GeneratorContract $xGenerator The code generator |
||
| 111 | * @param integer $nPriority The desired priority, used to order the plugins |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function addGenerator(GeneratorContract $xGenerator, $nPriority) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Generate a hash for all the javascript code generated by the library |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | private function getHash() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Render a template in the 'plugins' subdir |
||
| 143 | * |
||
| 144 | * @param string $sTemplate The template filename |
||
| 145 | * @param array $aVars The template variables |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | private function render($sTemplate, array $aVars = []) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | private function generateCode() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the base URI of the Jaxon library javascript files |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | private function getJsLibUri() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get the extension of the Jaxon library javascript files |
||
| 218 | * |
||
| 219 | * The returned string is '.min.js' if the files are minified. |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | private function getJsLibExt() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
| 234 | * |
||
| 235 | * @return boolean |
||
| 236 | */ |
||
| 237 | public function canExportJavascript() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getJs() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getCss() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the correspondances between previous and current config options |
||
| 305 | * |
||
| 306 | * They are used to keep the deprecated config options working. |
||
| 307 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | private function getOptionVars() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get the javascript code to be sent to the browser |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | private function _getScript() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Write javascript files and return the corresponding URI |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | private function _writeFiles() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the javascript code to be sent to the browser |
||
| 402 | * |
||
| 403 | * Also call each of the request plugins giving them the opportunity |
||
| 404 | * to output some javascript to the page being generated. |
||
| 405 | * This is called only when the page is being loaded initially. |
||
| 406 | * This is not called when processing a request. |
||
| 407 | * |
||
| 408 | * @param boolean $bIncludeJs Also get the JS files |
||
| 409 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getScript($bIncludeJs, $bIncludeCss) |
||
| 449 | } |
||
| 450 |