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 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 |
||
19 | class Generator |
||
20 | { |
||
21 | use \Jaxon\Utils\Traits\Config; |
||
22 | use \Jaxon\Utils\Traits\Cache; |
||
23 | use \Jaxon\Utils\Traits\Minifier; |
||
24 | use \Jaxon\Utils\Traits\Template; |
||
25 | |||
26 | /** |
||
27 | * The response type. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | const RESPONSE_TYPE = 'JSON'; |
||
32 | |||
33 | /** |
||
34 | * The plugin manager |
||
35 | * |
||
36 | * @var Jaxon\Plugin\Manager |
||
37 | */ |
||
38 | protected $xPluginManager; |
||
39 | |||
40 | /** |
||
41 | * Generated CSS code |
||
42 | * |
||
43 | * @var string|null |
||
44 | */ |
||
45 | protected $sCssCode = null; |
||
46 | |||
47 | /** |
||
48 | * Generated Javascript code |
||
49 | * |
||
50 | * @var string|null |
||
51 | */ |
||
52 | protected $sJsCode = null; |
||
53 | |||
54 | /** |
||
55 | * Generated Javascript ready script |
||
56 | * |
||
57 | * @var string|null |
||
58 | */ |
||
59 | protected $sJsReady = null; |
||
60 | |||
61 | /** |
||
62 | * The constructor |
||
63 | * |
||
64 | * @param Jaxon\Plugin\Manager $xPluginManager |
||
65 | */ |
||
66 | public function __construct(Manager $xPluginManager) |
||
70 | |||
71 | /** |
||
72 | * Get the base URI of the Jaxon library javascript files |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | private function getJsLibUri() |
||
86 | |||
87 | /** |
||
88 | * Get the extension of the Jaxon library javascript files |
||
89 | * |
||
90 | * The returned string is '.min.js' if the files are minified. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | private function getJsLibExt() |
||
107 | |||
108 | /** |
||
109 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
110 | * |
||
111 | * @return boolean |
||
112 | */ |
||
113 | public function canExportJavascript() |
||
133 | |||
134 | /** |
||
135 | * Set the cache directory for the template engine |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | private function setTemplateCacheDir() |
||
146 | |||
147 | /** |
||
148 | * Generate a hash for all the javascript code generated by the library |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | private function generateHash() |
||
165 | |||
166 | /** |
||
167 | * Get the HTML tags to include Jaxon javascript files into the page |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | private function makePluginsCode() |
||
222 | |||
223 | /** |
||
224 | * Get the HTML tags to include Jaxon javascript files into the page |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getJs() |
||
258 | |||
259 | /** |
||
260 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getCss() |
||
272 | |||
273 | /** |
||
274 | * Get the correspondances between previous and current config options |
||
275 | * |
||
276 | * They are used to keep the deprecated config options working. |
||
277 | * They will be removed when the deprecated options will lot be supported anymore. |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | private function getOptionVars() |
||
301 | |||
302 | /** |
||
303 | * Get the javascript code to be sent to the browser |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | private function _getScript() |
||
317 | |||
318 | /** |
||
319 | * Get the javascript code to be sent to the browser |
||
320 | * |
||
321 | * Also call each of the request plugins giving them the opportunity |
||
322 | * to output some javascript to the page being generated. |
||
323 | * This is called only when the page is being loaded initially. |
||
324 | * This is not called when processing a request. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function getScript() |
||
372 | } |
||
373 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..