Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
17 | class CodeGenerator |
||
18 | { |
||
19 | use \Jaxon\Utils\Traits\Config; |
||
20 | use \Jaxon\Utils\Traits\Cache; |
||
21 | use \Jaxon\Utils\Traits\Minifier; |
||
22 | use \Jaxon\Utils\Traits\Template; |
||
23 | |||
24 | /** |
||
25 | * The response type. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | const RESPONSE_TYPE = 'JSON'; |
||
30 | |||
31 | /** |
||
32 | * The plugin manager |
||
33 | * |
||
34 | * @var Manager |
||
35 | */ |
||
36 | protected $xPluginManager; |
||
37 | |||
38 | /** |
||
39 | * Generated CSS code |
||
40 | * |
||
41 | * @var string|null |
||
42 | */ |
||
43 | protected $sCssCode = null; |
||
44 | |||
45 | /** |
||
46 | * Generated Javascript code |
||
47 | * |
||
48 | * @var string|null |
||
49 | */ |
||
50 | protected $sJsCode = null; |
||
51 | |||
52 | /** |
||
53 | * Generated Javascript ready script |
||
54 | * |
||
55 | * @var string|null |
||
56 | */ |
||
57 | protected $sJsReady = null; |
||
58 | |||
59 | /** |
||
60 | * The constructor |
||
61 | * |
||
62 | * @param Manager $xPluginManager |
||
63 | */ |
||
64 | public function __construct(Manager $xPluginManager) |
||
68 | |||
69 | /** |
||
70 | * Get the base URI of the Jaxon library javascript files |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | private function getJsLibUri() |
||
84 | |||
85 | /** |
||
86 | * Get the extension of the Jaxon library javascript files |
||
87 | * |
||
88 | * The returned string is '.min.js' if the files are minified. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | private function getJsLibExt() |
||
105 | |||
106 | /** |
||
107 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
108 | * |
||
109 | * @return boolean |
||
110 | */ |
||
111 | public function canExportJavascript() |
||
131 | |||
132 | /** |
||
133 | * Set the cache directory for the template engine |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | private function setTemplateCacheDir() |
||
144 | |||
145 | /** |
||
146 | * Generate a hash for all the javascript code generated by the library |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | private function generateHash() |
||
163 | |||
164 | /** |
||
165 | * Get the HTML tags to include Jaxon javascript files into the page |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | private function makePluginsCode() |
||
220 | |||
221 | /** |
||
222 | * Get the HTML tags to include Jaxon javascript files into the page |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getJs() |
||
256 | |||
257 | /** |
||
258 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getCss() |
||
270 | |||
271 | /** |
||
272 | * Get the correspondances between previous and current config options |
||
273 | * |
||
274 | * They are used to keep the deprecated config options working. |
||
275 | * They will be removed when the deprecated options will lot be supported anymore. |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | private function getOptionVars() |
||
299 | |||
300 | /** |
||
301 | * Get the javascript code to be sent to the browser |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | private function _getScript() |
||
315 | |||
316 | /** |
||
317 | * Get the javascript code to be sent to the browser |
||
318 | * |
||
319 | * Also call each of the request plugins giving them the opportunity |
||
320 | * to output some javascript to the page being generated. |
||
321 | * This is called only when the page is being loaded initially. |
||
322 | * This is not called when processing a request. |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getScript() |
||
370 | } |
||
371 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.