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 Theme 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 Theme, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Theme extends \Theme |
||
28 | { |
||
29 | /** |
||
30 | * This is the only template included in the sources. |
||
31 | */ |
||
32 | public function template_rawdata() |
||
38 | |||
39 | /** |
||
40 | * The header template |
||
41 | */ |
||
42 | public function template_header() |
||
43 | { |
||
44 | global $context, $settings; |
||
45 | |||
46 | doSecurityChecks(); |
||
47 | |||
48 | $this->setupThemeContext(); |
||
49 | |||
50 | // Print stuff to prevent caching of pages (except on attachment errors, etc.) |
||
51 | if (empty($context['no_last_modified'])) |
||
52 | { |
||
53 | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
||
54 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
||
55 | |||
56 | if ($this->headerSent('Content-Type') === false && (!isset($_REQUEST['xml']) && !isset($_REQUEST['api']))) |
||
57 | { |
||
58 | header('Content-Type: text/html; charset=UTF-8'); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | if ($this->headerSent('Content-Type') === false) |
||
63 | { |
||
64 | // Probably temporary ($_REQUEST['xml'] should be replaced by $_REQUEST['api']) |
||
65 | if (isset($_REQUEST['api']) && $_REQUEST['api'] === 'json') |
||
66 | { |
||
67 | header('Content-Type: application/json; charset=UTF-8'); |
||
68 | } |
||
69 | elseif (isset($_REQUEST['xml']) || isset($_REQUEST['api'])) |
||
70 | { |
||
71 | header('Content-Type: text/xml; charset=UTF-8'); |
||
72 | } |
||
73 | else |
||
74 | { |
||
75 | header('Content-Type: text/html; charset=UTF-8'); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | foreach ($this->layers->prepareContext() as $layer) |
||
80 | { |
||
81 | loadSubTemplate($layer . '_above', 'ignore'); |
||
82 | } |
||
83 | |||
84 | View Code Duplication | if (isset($settings['use_default_images']) && $settings['use_default_images'] === 'defaults' && isset($settings['default_template'])) |
|
85 | { |
||
86 | $settings['theme_url'] = $settings['default_theme_url']; |
||
87 | $settings['images_url'] = $settings['default_images_url']; |
||
88 | $settings['theme_dir'] = $settings['default_theme_dir']; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Checks if a header of a given type has already been sent |
||
94 | * |
||
95 | * @param string $type |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | protected function headerSent($type) |
||
100 | { |
||
101 | $sent = headers_list(); |
||
102 | foreach ($sent as $header) |
||
103 | { |
||
104 | if (substr($header, 0, strlen($type)) === $type) |
||
105 | { |
||
106 | return true; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | return false; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Show the copyright. |
||
115 | */ |
||
116 | public function theme_copyright() |
||
117 | { |
||
118 | global $forum_copyright; |
||
119 | |||
120 | // Don't display copyright for things like SSI. |
||
121 | if (!defined('FORUM_VERSION')) |
||
122 | { |
||
123 | return; |
||
124 | } |
||
125 | |||
126 | // Put in the version... |
||
127 | $forum_copyright = replaceBasicActionUrl(sprintf($forum_copyright, FORUM_VERSION)); |
||
128 | |||
129 | echo ' |
||
130 | ', $forum_copyright; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * The template footer |
||
135 | */ |
||
136 | public function template_footer() |
||
137 | { |
||
138 | global $context, $settings, $modSettings, $time_start; |
||
139 | |||
140 | $db = database(); |
||
141 | |||
142 | // Show the load time? (only makes sense for the footer.) |
||
143 | $context['show_load_time'] = !empty($modSettings['timeLoadPageEnable']); |
||
144 | $context['load_time'] = round(microtime(true) - $time_start, 3); |
||
145 | $context['load_queries'] = $db->num_queries(); |
||
146 | |||
147 | View Code Duplication | if (isset($settings['use_default_images']) && $settings['use_default_images'] === 'defaults' && isset($settings['default_template'])) |
|
148 | { |
||
149 | $settings['theme_url'] = $settings['actual_theme_url']; |
||
150 | $settings['images_url'] = $settings['actual_images_url']; |
||
151 | $settings['theme_dir'] = $settings['actual_theme_dir']; |
||
152 | } |
||
153 | |||
154 | foreach ($this->layers->reverseLayers() as $layer) |
||
155 | { |
||
156 | $this->templates->loadSubTemplate($layer . '_below', 'ignore'); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Loads the required jQuery files for the system |
||
162 | * |
||
163 | * - Determines the correct script tags to add based on CDN/Local/Auto |
||
164 | */ |
||
165 | protected function templateJquery() |
||
166 | { |
||
167 | global $modSettings, $settings; |
||
168 | |||
169 | // Using a specified version of jquery or what was shipped 3.1.1 / 1.12.1 |
||
170 | $jquery_version = (!empty($modSettings['jquery_default']) && !empty($modSettings['jquery_version'])) ? $modSettings['jquery_version'] : '3.1.1'; |
||
171 | $jqueryui_version = (!empty($modSettings['jqueryui_default']) && !empty($modSettings['jqueryui_version'])) ? $modSettings['jqueryui_version'] : '1.12.1'; |
||
172 | |||
173 | switch ($modSettings['jquery_source']) |
||
174 | { |
||
175 | // Only getting the files from the CDN? |
||
176 | case 'cdn': |
||
177 | echo ' |
||
178 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/' . $jquery_version . '/jquery.min.js" id="jquery"></script>', |
||
179 | (!empty($modSettings['jquery_include_ui']) ? ' |
||
180 | <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/' . $jqueryui_version . '/jquery-ui.min.js" id="jqueryui"></script>' : ''); |
||
181 | break; |
||
182 | // Just use the local file |
||
183 | case 'local': |
||
184 | echo ' |
||
185 | <script src="', $settings['default_theme_url'], '/scripts/jquery-' . $jquery_version . '.min.js" id="jquery"></script>', |
||
186 | (!empty($modSettings['jquery_include_ui']) ? ' |
||
187 | <script src="' . $settings['default_theme_url'] . '/scripts/jquery-ui-' . $jqueryui_version . '.min.js" id="jqueryui"></script>' : ''); |
||
188 | break; |
||
189 | // CDN with local fallback |
||
190 | case 'auto': |
||
191 | echo ' |
||
192 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/' . $jquery_version . '/jquery.min.js" id="jquery"></script>', |
||
193 | (!empty($modSettings['jquery_include_ui']) ? ' |
||
194 | <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/' . $jqueryui_version . '/jquery-ui.min.js" id="jqueryui"></script>' : ''); |
||
195 | echo ' |
||
196 | <script> |
||
197 | window.jQuery || document.write(\'<script src="', $settings['default_theme_url'], '/scripts/jquery-' . $jquery_version . '.min.js"><\/script>\');', |
||
198 | (!empty($modSettings['jquery_include_ui']) ? ' |
||
199 | window.jQuery.ui || document.write(\'<script src="' . $settings['default_theme_url'] . '/scripts/jquery-ui-' . $jqueryui_version . '.min.js"><\/script>\')' : ''), ' |
||
200 | </script>'; |
||
201 | break; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Loads the JS files that have been requested |
||
207 | * |
||
208 | * - Will combine / minify the files it the option is set. |
||
209 | * - Handles both above and below (deferred) files |
||
210 | * |
||
211 | * @param bool $do_deferred |
||
212 | */ |
||
213 | protected function templateJavascriptFiles($do_deferred) |
||
214 | { |
||
215 | global $modSettings, $settings; |
||
216 | |||
217 | // Combine and minify javascript source files to save bandwidth and requests |
||
218 | if (!empty($modSettings['minify_css_js'])) |
||
219 | { |
||
220 | $combiner = new \Site_Combiner($settings['default_theme_cache_dir'], $settings['default_theme_cache_url']); |
||
221 | $combine_name = $combiner->site_js_combine($this->js_files, $do_deferred); |
||
222 | |||
223 | call_integration_hook('post_javascript_combine', array(&$combine_name, $combiner)); |
||
224 | |||
225 | if (!empty($combine_name)) |
||
226 | { |
||
227 | echo ' |
||
228 | <script src="', $combine_name, '" id="jscombined', $do_deferred ? 'bottom' : 'top', '"></script>'; |
||
229 | } |
||
230 | // While we have Javascript files to place in the template |
||
231 | foreach ($combiner->getSpares() as $id => $js_file) |
||
232 | { |
||
233 | if ((!$do_deferred && empty($js_file['options']['defer'])) || ($do_deferred && !empty($js_file['options']['defer']))) |
||
234 | { |
||
235 | echo ' |
||
236 | <script src="', $js_file['filename'], '" id="', $id, '"', !empty($js_file['options']['async']) ? ' async="async"' : '', '></script>'; |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | // Just give them the full load then |
||
241 | else |
||
242 | { |
||
243 | // While we have Javascript files to place in the template |
||
244 | foreach ($this->js_files as $id => $js_file) |
||
245 | { |
||
246 | if ((!$do_deferred && empty($js_file['options']['defer'])) || ($do_deferred && !empty($js_file['options']['defer']))) |
||
247 | { |
||
248 | echo ' |
||
249 | <script src="', $js_file['filename'], '" id="', $id, '"', !empty($js_file['options']['async']) ? ' async="async"' : '', '></script>'; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Deletes the hives (aggregated CSS and JS files) previously created. |
||
257 | * |
||
258 | * @param string $type = 'all' Filters the types of hives (valid values: |
||
259 | * * 'all' |
||
260 | * * 'css' |
||
261 | * * 'js' |
||
262 | */ |
||
263 | public function cleanHives($type = 'all') |
||
264 | { |
||
265 | global $settings; |
||
266 | |||
267 | $combiner = new \Site_Combiner($settings['default_theme_cache_dir'], $settings['default_theme_cache_url']); |
||
268 | $result = true; |
||
269 | |||
270 | if ($type === 'all' || $type === 'css') |
||
271 | { |
||
272 | $result &= $combiner->removeCssHives(); |
||
273 | } |
||
274 | |||
275 | if ($type === 'all' || $type === 'js') |
||
276 | { |
||
277 | $result &= $combiner->removeJsHives(); |
||
278 | } |
||
279 | |||
280 | return $result; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Output the Javascript files |
||
285 | * |
||
286 | * What it does: |
||
287 | * |
||
288 | * - Tabbing in this function is to make the HTML source look proper |
||
289 | * - Outputs jQuery/jQueryUI from the proper source (local/CDN) |
||
290 | * - If deferred is set function will output all JS (source & inline) set to load at page end |
||
291 | * - If the admin option to combine files is set, will use Combiner.class |
||
292 | * |
||
293 | * @param bool $do_deferred = false |
||
294 | */ |
||
295 | public function template_javascript($do_deferred = false) |
||
296 | { |
||
297 | global $modSettings; |
||
298 | |||
299 | // First up, load jQuery and jQuery UI |
||
300 | if (isset($modSettings['jquery_source']) && !$do_deferred) |
||
301 | { |
||
302 | $this->templateJquery(); |
||
303 | } |
||
304 | |||
305 | // Use this hook to work with Javascript files and vars pre output |
||
306 | call_integration_hook('pre_javascript_output', array($do_deferred)); |
||
307 | |||
308 | // Load in the JS files |
||
309 | if (!empty($this->js_files)) |
||
310 | { |
||
311 | $this->templateJavascriptFiles($do_deferred); |
||
312 | } |
||
313 | |||
314 | // Build the declared Javascript variables script |
||
315 | $js_vars = array(); |
||
316 | if (!empty($this->js_vars) && !$do_deferred) |
||
317 | { |
||
318 | foreach ($this->js_vars as $var => $value) |
||
319 | $js_vars[] = $var . ' = ' . $value; |
||
320 | |||
321 | // Newlines and tabs are here to make it look nice in the page source view, stripped if minimized though |
||
322 | $this->js_inline['standard'][] = 'var ' . implode(",\n\t\t\t", $js_vars) . ';'; |
||
323 | } |
||
324 | |||
325 | // Inline JavaScript - Actually useful some times! |
||
326 | if (!empty($this->js_inline)) |
||
327 | { |
||
328 | // Deferred output waits until we are deferring ! |
||
329 | View Code Duplication | if (!empty($this->js_inline['defer']) && $do_deferred) |
|
330 | { |
||
331 | // Combine them all in to one output |
||
332 | $this->js_inline['defer'] = array_map('trim', $this->js_inline['defer']); |
||
333 | $inline_defered_code = implode("\n\t\t", $this->js_inline['defer']); |
||
334 | |||
335 | // Output the deferred script |
||
336 | echo ' |
||
337 | <script> |
||
338 | ', $inline_defered_code, ' |
||
339 | </script>'; |
||
340 | } |
||
341 | |||
342 | // Standard output, and our javascript vars, get output when we are not on a defered call |
||
343 | View Code Duplication | if (!empty($this->js_inline['standard']) && !$do_deferred) |
|
344 | { |
||
345 | $this->js_inline['standard'] = array_map('trim', $this->js_inline['standard']); |
||
346 | |||
347 | // And output the js vars and standard scripts to the page |
||
348 | echo ' |
||
349 | <script> |
||
350 | ', implode("\n\t\t", $this->js_inline['standard']), ' |
||
351 | </script>'; |
||
352 | } |
||
353 | } |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Output the CSS files |
||
358 | * |
||
359 | * What it does: |
||
360 | * - If the admin option to combine files is set, will use Combiner.class |
||
361 | */ |
||
362 | public function template_css() |
||
363 | { |
||
364 | global $modSettings, $settings; |
||
365 | |||
366 | // Use this hook to work with CSS files pre output |
||
367 | call_integration_hook('pre_css_output'); |
||
368 | |||
369 | // Combine and minify the CSS files to save bandwidth and requests? |
||
370 | if (!empty($this->css_files)) |
||
371 | { |
||
372 | if (!empty($modSettings['minify_css_js'])) |
||
373 | { |
||
374 | $combiner = new \Site_Combiner($settings['default_theme_cache_dir'], $settings['default_theme_cache_url']); |
||
375 | $combine_name = $combiner->site_css_combine($this->css_files); |
||
376 | |||
377 | call_integration_hook('post_css_combine', array(&$combine_name, $combiner)); |
||
378 | |||
379 | if (!empty($combine_name)) |
||
380 | { |
||
381 | echo ' |
||
382 | <link rel="stylesheet" href="', $combine_name, '" id="csscombined" />'; |
||
383 | } |
||
384 | |||
385 | foreach ($combiner->getSpares() as $id => $file) |
||
386 | echo ' |
||
387 | <link rel="stylesheet" href="', $file['filename'], '" id="', $id, '" />'; |
||
388 | } |
||
389 | else |
||
390 | { |
||
391 | foreach ($this->css_files as $id => $file) |
||
392 | echo ' |
||
393 | <link rel="stylesheet" href="', $file['filename'], '" id="', $id, '" />'; |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Output the inline-CSS in a style tag |
||
400 | */ |
||
401 | public function template_inlinecss() |
||
402 | { |
||
403 | $style_tag = ''; |
||
404 | |||
405 | // Combine and minify the CSS files to save bandwidth and requests? |
||
406 | if (!empty($this->css_rules)) |
||
407 | { |
||
408 | if (!empty($this->css_rules['all'])) |
||
409 | { |
||
410 | $style_tag .= ' |
||
411 | ' . $this->css_rules['all']; |
||
412 | } |
||
413 | if (!empty($this->css_rules['media'])) |
||
414 | { |
||
415 | foreach ($this->css_rules['media'] as $key => $val) |
||
416 | { |
||
417 | $style_tag .= ' |
||
418 | @media ' . $key . '{ |
||
419 | ' . $val . ' |
||
420 | }'; |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 | |||
425 | if (!empty($style_tag)) |
||
426 | { |
||
427 | echo ' |
||
428 | <style>' . $style_tag . ' |
||
429 | </style>'; |
||
430 | } |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Calls on template_show_error from index.template.php to show warnings |
||
435 | * and security errors for admins |
||
436 | */ |
||
437 | public function template_admin_warning_above() |
||
438 | { |
||
439 | global $context, $txt; |
||
440 | |||
441 | View Code Duplication | if (!empty($context['security_controls_files'])) |
|
442 | { |
||
443 | $context['security_controls_files']['type'] = 'serious'; |
||
444 | template_show_error('security_controls_files'); |
||
445 | } |
||
446 | |||
447 | View Code Duplication | if (!empty($context['security_controls_query'])) |
|
448 | { |
||
449 | $context['security_controls_query']['type'] = 'serious'; |
||
450 | template_show_error('security_controls_query'); |
||
451 | } |
||
452 | |||
453 | View Code Duplication | if (!empty($context['security_controls_ban'])) |
|
454 | { |
||
455 | $context['security_controls_ban']['type'] = 'serious'; |
||
456 | template_show_error('security_controls_ban'); |
||
457 | } |
||
458 | |||
459 | if (!empty($context['new_version_updates'])) |
||
460 | { |
||
461 | template_show_error('new_version_updates'); |
||
462 | } |
||
463 | |||
464 | if (!empty($context['accepted_agreement'])) |
||
465 | { |
||
466 | template_show_error('accepted_agreement'); |
||
467 | } |
||
468 | |||
469 | // Any special notices to remind the admin about? |
||
470 | if (!empty($context['warning_controls'])) |
||
471 | { |
||
472 | $context['warning_controls']['errors'] = $context['warning_controls']; |
||
473 | $context['warning_controls']['title'] = $txt['admin_warning_title']; |
||
474 | $context['warning_controls']['type'] = 'warning'; |
||
475 | template_show_error('warning_controls'); |
||
476 | } |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * If the option to pretty output code is on, this loads the JS and CSS |
||
481 | */ |
||
482 | public function addCodePrettify() |
||
497 | |||
498 | /** |
||
499 | * If video embedding is enabled, this loads the needed JS and vars |
||
500 | */ |
||
501 | public function autoEmbedVideo() |
||
520 | |||
521 | /** |
||
522 | * Ensures we kick the mail queue from time to time so that it gets |
||
523 | * checked as often as possible. |
||
524 | */ |
||
525 | public function doScheduledSendMail() |
||
558 | |||
559 | /** |
||
560 | * Relative times require a few variables be set in the JS |
||
561 | */ |
||
562 | public function relativeTimes() |
||
591 | |||
592 | /** |
||
593 | * Sets up the basic theme context stuff. |
||
594 | * |
||
595 | * @param bool $forceload = false |
||
596 | */ |
||
597 | public function setupThemeContext($forceload = false) |
||
598 | { |
||
599 | global $modSettings, $user_info, $scripturl, $context, $settings, $options, $txt; |
||
600 | |||
601 | static $loaded = false; |
||
602 | |||
785 | |||
786 | /** |
||
787 | * Adds required support CSS files. |
||
788 | */ |
||
789 | public function loadSupportCSS() |
||
808 | |||
809 | /** |
||
810 | * Sets up all of the top menu buttons |
||
811 | * |
||
812 | * What it does: |
||
813 | * |
||
814 | * - Defines every master item in the menu, as well as any sub-items |
||
815 | * - Ensures the chosen action is set so the menu is highlighted |
||
816 | * - Saves them in the cache if it is available and on |
||
817 | * - Places the results in $context |
||
818 | */ |
||
819 | public function setupMenuContext() |
||
994 | |||
995 | /** |
||
996 | * Load the base JS that gives Elkarte a nice rack |
||
997 | */ |
||
998 | public function loadThemeJavascript() |
||
1039 | |||
1040 | /** |
||
1041 | * Makes the default layers and languages available |
||
1042 | * |
||
1043 | * - Loads index and addon language files as needed |
||
1044 | * - Loads xml, index or no templates as needed |
||
1045 | * - Loads templates as defined by $settings['theme_templates'] |
||
1046 | */ |
||
1047 | public function loadDefaultLayers() |
||
1114 | |||
1115 | /** |
||
1116 | * If a variant CSS is needed, this loads it |
||
1117 | */ |
||
1118 | public function loadThemeVariant() |
||
1162 | } |
||
1163 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: