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 |
||
| 21 | class Generator |
||
| 22 | { |
||
| 23 | use \Jaxon\Features\Config; |
||
| 24 | use \Jaxon\Features\Minifier; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Default library URL |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The objects that generate code |
||
| 35 | * |
||
| 36 | * @var array<GeneratorContract> |
||
| 37 | */ |
||
| 38 | protected $aGenerators = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The Jaxon template engine |
||
| 42 | * |
||
| 43 | * @var TemplateEngine |
||
| 44 | */ |
||
| 45 | protected $xTemplateEngine; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The constructor |
||
| 49 | * |
||
| 50 | * @param TemplateEngine $xTemplateEngine The template engine |
||
| 51 | */ |
||
| 52 | public function __construct(TemplateEngine $xTemplateEngine) |
||
| 53 | { |
||
| 54 | $this->xTemplateEngine = $xTemplateEngine; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get the correspondances between previous and current config options |
||
| 59 | * |
||
| 60 | * They are used to keep the deprecated config options working. |
||
| 61 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | private function getOptionVars() |
||
| 66 | { |
||
| 67 | return [ |
||
| 68 | 'sResponseType' => 'JSON', |
||
| 69 | 'sVersion' => $this->getOption('core.version'), |
||
| 70 | 'sLanguage' => $this->getOption('core.language'), |
||
| 71 | 'bLanguage' => $this->hasOption('core.language') ? true : false, |
||
| 72 | 'sRequestURI' => $this->getOption('core.request.uri'), |
||
| 73 | 'sDefaultMode' => $this->getOption('core.request.mode'), |
||
| 74 | 'sDefaultMethod' => $this->getOption('core.request.method'), |
||
| 75 | 'sCsrfMetaName' => $this->getOption('core.request.csrf_meta'), |
||
| 76 | 'bDebug' => $this->getOption('core.debug.on'), |
||
| 77 | 'bVerboseDebug' => $this->getOption('core.debug.verbose'), |
||
| 78 | 'sDebugOutputID' => $this->getOption('core.debug.output_id'), |
||
| 79 | 'nResponseQueueSize' => $this->getOption('js.lib.queue_size'), |
||
| 80 | 'sStatusMessages' => $this->getOption('js.lib.show_status') ? 'true' : 'false', |
||
| 81 | 'sWaitCursor' => $this->getOption('js.lib.show_cursor') ? 'true' : 'false', |
||
| 82 | 'sDefer' => $this->getOption('js.app.options', ''), |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Render a template in the 'plugins' subdir |
||
| 88 | * |
||
| 89 | * @param string $sTemplate The template filename |
||
| 90 | * @param array $aVars The template variables |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | private function _render($sTemplate, array $aVars = []) |
||
| 95 | { |
||
| 96 | $aVars['sJsOptions'] = $this->getOption('js.app.options', ''); |
||
| 97 | return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
||
| 98 | } |
||
| 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 CSS code and files into the page |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getCss() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getJs() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the javascript code to be sent to the browser |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | private function _getScript() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the javascript code to include directly in HTML |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | private function _getInlineScript() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | private function getJsFileName() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Write javascript files and return the corresponding URI |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function createFiles($sJsDirectory, $sJsFileName) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the javascript code to be sent to the browser |
||
| 293 | * |
||
| 294 | * @param boolean $bIncludeJs Also get the JS files |
||
| 295 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function getScript($bIncludeJs, $bIncludeCss) |
||
| 328 | } |
||
| 329 |