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 assetManager 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 assetManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class assetManager |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @var assetManager |
||
23 | */ |
||
24 | protected static $_BehaviorInstance; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $callMapp; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * @var MY_Controller |
||
34 | */ |
||
35 | protected $ci; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $module_js = 'jsLangs'; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var Template |
||
45 | */ |
||
46 | protected $template; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $useCompress = false; |
||
52 | |||
53 | /** |
||
54 | * assetManager constructor. |
||
55 | */ |
||
56 | private function __construct() { |
||
57 | |||
58 | } |
||
59 | |||
60 | private function __clone() { |
||
63 | |||
64 | /** |
||
65 | * @param string|array $item |
||
66 | * @param string|integer|float $value |
||
67 | * @return assetManager |
||
68 | * @access public |
||
69 | * @copyright ImageCMS (c) 2013, Roman <[email protected]> |
||
70 | */ |
||
71 | public function appendData($item, $value) { |
||
72 | $this->setData($item, CI_Controller::get_instance()->template->get_var($item) . $value); |
||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string|array $item |
||
78 | * @param string|integer|float|array|boolean $value |
||
79 | * @return assetManager |
||
80 | * @access public |
||
81 | * @author Kaero |
||
82 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
83 | */ |
||
84 | public function setData($item, $value = null) { |
||
85 | if ($value != null AND !is_array($item)) { |
||
86 | $data[$item] = $value; |
||
87 | } else { |
||
88 | $data = $item; |
||
89 | } |
||
90 | (empty($data)) OR CI_Controller::get_instance()->template->add_array((array) $data); |
||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return assetManager |
||
96 | * @access public |
||
97 | * @author Kaero |
||
98 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
99 | */ |
||
100 | public static function create() { |
||
101 | (null !== self::$_BehaviorInstance) OR self::$_BehaviorInstance = new self(); |
||
102 | self::$_BehaviorInstance->callMapp = debug_backtrace(); |
||
103 | return self::$_BehaviorInstance; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * fetch admin view |
||
108 | * @param string $tpl Template file name |
||
109 | * @param boolean $fetchLangsTpl |
||
110 | * @return string |
||
111 | * @access public |
||
112 | * @author Kaero |
||
113 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
114 | */ |
||
115 | public function fetchAdminTemplate($tpl, $fetchLangsTpl = TRUE) { |
||
116 | try { |
||
117 | |||
118 | if ($fetchLangsTpl) { |
||
119 | /** Start. Load template file */ |
||
120 | $view = CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($this->module_js)); |
||
121 | } |
||
122 | |||
123 | if (isset($view)) { |
||
124 | return $view . CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($tpl, null, true)); |
||
125 | } else { |
||
126 | return CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($tpl, null, true)); |
||
127 | } |
||
128 | /** Start. Return template file */ |
||
129 | } catch (Exception $exc) { |
||
130 | log_message('error', $exc->getMessage()); |
||
131 | show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * fetch public view |
||
137 | * @param string $tpl Template file name |
||
138 | * @param string $moduleName |
||
139 | * @return string |
||
140 | * @access public |
||
141 | * @author Kaero |
||
142 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
143 | */ |
||
144 | View Code Duplication | public function fetchTemplate($tpl, $moduleName = null) { |
|
145 | try { |
||
146 | /** Start. Return template file */ |
||
147 | return CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($tpl, $moduleName)); |
||
148 | } catch (Exception $exc) { |
||
149 | log_message('error', $exc->getMessage()); |
||
150 | show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param string $item |
||
156 | * @return string|integer|float|array|boolean |
||
157 | * @access public |
||
158 | * @author |
||
159 | * @copyright |
||
160 | */ |
||
161 | public function getData($item) { |
||
162 | return CI_Controller::get_instance()->template->get_var($item); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * |
||
167 | * @param string $path |
||
168 | * @param string $position |
||
169 | * @return assetManager |
||
170 | */ |
||
171 | public function registerJsFullpath($path, $position = 'after') { |
||
172 | CI_Controller::get_instance()->template->registerJsFile($path, $position, false); |
||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $message |
||
178 | * @param string $title |
||
179 | * @param string $class |
||
180 | */ |
||
181 | public function registerJsMessage($message, $title, $class = '') { |
||
182 | $script = showMessage($message, $title, $class, true, true); |
||
183 | CI_Controller::get_instance()->template->registerJsScript($script, 'after'); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @access public |
||
188 | * @author a.gula |
||
189 | * @param string $script |
||
190 | * @param boolean $useCompress |
||
191 | * @param string $position after|before |
||
192 | * @param string $type |
||
193 | * @return assetManager |
||
194 | * @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
||
195 | */ |
||
196 | public function registerJsScript($script, $useCompress = FALSE, $position = 'after', $type = 'text/javascript') { |
||
197 | /** Start. Load JS script into template */ |
||
198 | if ($useCompress) { |
||
199 | CI_Controller::get_instance()->template->registerJsScript("<script type='$type'>" . $this->compressJs($script) . '</script>', $position); |
||
200 | } else { |
||
201 | CI_Controller::get_instance()->template->registerJsScript("<script type='$type'>" . $script . '</script>', $position); |
||
202 | } |
||
203 | |||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @return assetManager |
||
209 | * @access public |
||
210 | * @author Kaero |
||
211 | * @param string $name |
||
212 | * @param boolean $useCompress |
||
213 | * @param string $position |
||
214 | * @return assetManager |
||
215 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
216 | */ |
||
217 | public function registerScript($name, $useCompress = FALSE, $position = 'after') { |
||
227 | |||
228 | /** |
||
229 | * @param string $js |
||
230 | * @return string |
||
231 | * @todo compress and cache |
||
232 | */ |
||
233 | private function compressJs($js) { |
||
234 | return $js; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Return formated path for JS - script files |
||
239 | * @param string $fileName |
||
240 | * @return string |
||
241 | * @access private |
||
242 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
243 | */ |
||
244 | View Code Duplication | private function buildScriptPath($fileName) { |
|
245 | $this->template = CI_Controller::get_instance()->config->item('template'); |
||
246 | |||
247 | $moduleName = $this->getTrace(); |
||
248 | $path = sprintf('templates/%s/%s/js/%s.js', $this->template, $moduleName, $fileName); |
||
249 | if (file_exists($path)) { |
||
250 | $url = $path; |
||
251 | } else { |
||
252 | $url = $this->getModuleFilePath( |
||
253 | [ |
||
254 | sprintf('%s/assets/js/%s.js', $moduleName, $fileName), |
||
255 | sprintf('%s/assets/js/%s.js', CI::$APP->uri->segment(4), $fileName), |
||
256 | ], |
||
257 | false |
||
258 | ); |
||
259 | } |
||
260 | |||
261 | return str_replace(MAINSITE, '', $url); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param string $list |
||
266 | * @return array |
||
267 | * @access public |
||
268 | * @author cutter |
||
269 | */ |
||
270 | private function getTrace($list = 'first_file') { |
||
271 | if ($list == 'first_file') { |
||
272 | $paths = explode(DIRECTORY_SEPARATOR, $this->callMapp[0]['file']); |
||
273 | return $paths[count($paths) - 2]; |
||
274 | } |
||
275 | |||
276 | if ($list == 'first') { |
||
277 | return $this->callMapp[0]; |
||
278 | } |
||
279 | |||
280 | if ($list == 'all') { |
||
281 | return $this->callMapp; |
||
282 | } |
||
283 | if (is_numeric($list)) { |
||
284 | return $this->callMapp[$list]; |
||
285 | } |
||
286 | return false; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Checks if file exists in any of modules dirs. If exists returns its path |
||
291 | * @param string|array $files example: ['menu/assets/css/style.css'] |
||
292 | * @param bool $noExt |
||
293 | * @return bool|string returns file path or FALSE |
||
294 | */ |
||
295 | private function getModuleFilePath($files, $noExt = true) { |
||
296 | |||
297 | if (is_string($files)) { |
||
298 | $files = [$files]; |
||
299 | } |
||
300 | |||
301 | foreach (Modules::$locations as $path => $relPath) { |
||
302 | foreach ($files as $fp) { |
||
303 | $absPath = $path . ltrim($fp, '/'); |
||
304 | if (file_exists($absPath)) { |
||
305 | if ($noExt == true) { |
||
306 | $absPath = explode('.', $absPath); |
||
307 | array_pop($absPath); |
||
308 | return implode('.', $absPath); |
||
309 | } else { |
||
310 | return $absPath; |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | return false; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param string $name |
||
320 | */ |
||
321 | public function registerScriptWithoutTemplate($name) { |
||
325 | |||
326 | /** |
||
327 | * @return assetManager |
||
328 | * @access public |
||
329 | * @author Kaero |
||
330 | * @param string $name |
||
331 | * @param boolean $useCompress |
||
332 | * @return assetManager |
||
333 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
334 | */ |
||
335 | public function registerStyle($name, $useCompress = FALSE) { |
||
351 | |||
352 | /** |
||
353 | * @param string $path |
||
354 | * @param string $position |
||
355 | */ |
||
356 | public function assetTemplateFiles($path, $position = 'before') { |
||
372 | |||
373 | /** |
||
374 | * Compressing css file |
||
375 | * @param string $css text of css file |
||
376 | * @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
||
377 | * @return string |
||
378 | */ |
||
379 | private function compressCss($css) { |
||
384 | |||
385 | /** |
||
386 | * Put css string into template |
||
387 | * @return assetManager |
||
388 | * @access public |
||
389 | * @author a.gula |
||
390 | * @param string $css |
||
391 | * @param boolean $useCompress |
||
392 | * @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
||
393 | */ |
||
394 | public function registerStyleStr($css, $useCompress = FALSE) { |
||
404 | |||
405 | /** |
||
406 | * @param string $name |
||
407 | */ |
||
408 | public function registerStyleWithoutTemplate($name) { |
||
412 | |||
413 | /** |
||
414 | * Return formated path for css |
||
415 | * @param string $fileName |
||
416 | * @return string |
||
417 | * @access private |
||
418 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
419 | */ |
||
420 | View Code Duplication | private function buildStylePath($fileName) { |
|
440 | |||
441 | /** |
||
442 | * Render public view |
||
443 | * @param string $tpl Template file name |
||
444 | * @param bool $ignoreWrap |
||
445 | * @param bool $fetchJsTpl |
||
446 | * @access public |
||
447 | * @author Kaero |
||
448 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
449 | */ |
||
450 | public function render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
||
453 | |||
454 | /** |
||
455 | * Render Admin view |
||
456 | * @param string $tpl Template file name |
||
457 | * @param bool $ignoreWrap |
||
458 | * @param bool $fetchJsTpl |
||
459 | * @access public |
||
460 | * @author Kaero |
||
461 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
462 | */ |
||
463 | public function renderAdmin($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
||
466 | |||
467 | /** |
||
468 | * |
||
469 | * @param string $tpl |
||
470 | * @param boolean $ignoreWrap |
||
471 | * @param boolean $fetchJsTpl |
||
472 | * @param boolean $admin |
||
473 | */ |
||
474 | private function _render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE, $admin = FALSE) { |
||
514 | |||
515 | /** |
||
516 | * Return formatted path |
||
517 | * @access private |
||
518 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
519 | * @param string $tpl |
||
520 | * @param string $moduleName |
||
521 | * @return string |
||
522 | */ |
||
523 | private function buildTemplatePath($tpl, $moduleName = null) { |
||
542 | |||
543 | /** |
||
544 | * |
||
545 | * @param string $path |
||
546 | * @return string |
||
547 | */ |
||
548 | protected function makePath($path) { |
||
567 | |||
568 | /** |
||
569 | * |
||
570 | * @param string $tpl |
||
571 | * @param string $moduleName |
||
572 | * @param boolean $admin |
||
573 | * @return string |
||
574 | * @throws Exception |
||
575 | */ |
||
576 | private function _buildTemplatePath($tpl, $moduleName = null, $admin = FALSE) { |
||
588 | |||
589 | /** |
||
590 | * Return formatted path |
||
591 | * @param string $fileName |
||
592 | * @return string |
||
593 | * @access private |
||
594 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
595 | */ |
||
596 | private function buildAdminTemplatePath($fileName) { |
||
605 | |||
606 | /** |
||
607 | * Changing main layout file |
||
608 | * @param string $mainLayout |
||
609 | * @return assetManager |
||
610 | */ |
||
611 | View Code Duplication | public function setMainLayout($mainLayout) { |
|
620 | |||
621 | /** |
||
622 | * Changing main layout file by full path |
||
623 | * @param string $mainLayout |
||
624 | * @return assetManager |
||
625 | */ |
||
626 | View Code Duplication | public function setMainLayoutByFullPath($mainLayout) { |
|
635 | |||
636 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: