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 View 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 View, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class View extends \Module { |
||
14 | |||
15 | public $title = 'No title'; |
||
16 | |||
17 | /** |
||
18 | * @var View\Template |
||
19 | */ |
||
20 | public $template = null; |
||
21 | public $libAssets = ['css' => [], 'js' => []]; |
||
22 | public $dynAssets = ['css' => [], 'js' => []]; |
||
23 | public $dynMetas = []; |
||
24 | public $viewedContent = ''; |
||
25 | public $contentData = []; |
||
26 | public $templatesPath = ''; |
||
27 | public $loadedCss = []; |
||
28 | public $loadedJs = []; |
||
29 | |||
30 | public function init() { |
||
31 | if (!empty($this->app->config['site']['name'])) { |
||
32 | $this->title = $this->app->config['site']['name']; |
||
33 | } |
||
34 | $this->resolveTemplate(); |
||
35 | } |
||
36 | |||
37 | public function resolveTemplate() { |
||
38 | $templateName = 'default'; |
||
39 | if (!empty($this->config[$this->app->type]['current'])) { |
||
40 | $templateName = $this->config[$this->app->type]['current']; |
||
41 | if (!empty($this->config[$this->app->type]['installed'][$templateName]['location'])) { |
||
42 | $this->templatesPath = App::$primary->path . "/templates"; |
||
43 | } |
||
44 | } |
||
45 | if (!$this->templatesPath) { |
||
46 | $this->templatesPath = $this->app->path . "/templates"; |
||
47 | } |
||
48 | $this->template = \View\Template::get($templateName, $this->app, $this->templatesPath); |
||
49 | if (!$this->template) { |
||
50 | $this->template = new \View\Template([ |
||
51 | 'name' => 'default', |
||
52 | 'path' => $this->templatesPath . '/default', |
||
53 | 'app' => $this->app |
||
54 | ]); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | public function page($params = []) { |
||
59 | $this->paramsParse($params); |
||
60 | if (file_exists($this->template->pagePath)) { |
||
61 | $source = file_get_contents($this->template->pagePath); |
||
62 | if (strpos($source, 'BODYEND') === false) { |
||
63 | $source = str_replace('</body>', '{BODYEND}</body>', $source); |
||
64 | } |
||
65 | $this->parseSource($source); |
||
66 | } else { |
||
67 | $this->content(); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | public function paramsParse($params) { |
||
72 | // set template |
||
73 | if (!empty($params['template']) && $params['template'] != 'current') { |
||
74 | $this->template = \View\Template::get($params['template']); |
||
75 | } |
||
76 | //set page |
||
77 | if (!empty($params['page']) && $params['page'] != 'current') { |
||
78 | $this->template->setPage($params['page']); |
||
79 | } |
||
80 | //set module |
||
81 | if (!empty($params['module'])) { |
||
82 | $this->template->setModule($params['module']); |
||
83 | } |
||
84 | //set content |
||
85 | if (!empty($params['content'])) { |
||
86 | $this->template->setContent($params['content']); |
||
87 | } elseif (!$this->template->contentPath) { |
||
88 | $this->template->setContent(); |
||
89 | } |
||
90 | //set data |
||
91 | if (!empty($params['data'])) { |
||
92 | $this->contentData = array_merge($this->contentData, $params['data']); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function content($params = []) { |
||
97 | |||
98 | $this->paramsParse($params); |
||
99 | |||
100 | if (empty($this->template->config['noSysMsgAutoShow'])) { |
||
101 | Msg::show(); |
||
102 | } |
||
103 | if (!file_exists($this->template->contentPath)) { |
||
104 | echo 'Content not found'; |
||
105 | } else { |
||
106 | extract($this->contentData); |
||
107 | include $this->template->contentPath; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | public function parentContent($contentName = '') { |
||
112 | if (!$contentName) { |
||
113 | $contentName = $this->template->content; |
||
114 | } |
||
115 | |||
116 | $paths = $this->template->getContentPaths($contentName); |
||
117 | |||
118 | $data = []; |
||
119 | $exist = false; |
||
120 | foreach ($paths as $type => $path) { |
||
121 | if (substr($path, 0, strrpos($path, '/')) == substr($this->template->contentPath, 0, strrpos($this->template->contentPath, '/'))) { |
||
122 | $exist = true; |
||
123 | continue; |
||
124 | } |
||
125 | if (file_exists($path) && $exist) { |
||
126 | $data['contentPath'] = $path; |
||
127 | $data['content'] = $contentName; |
||
128 | break; |
||
129 | } |
||
130 | } |
||
131 | if (!$data) { |
||
132 | echo 'Content not found'; |
||
133 | } else { |
||
134 | extract($this->contentData); |
||
135 | include $data['contentPath']; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | private function parseRaw($source) { |
||
140 | if (!$source) { |
||
141 | return []; |
||
142 | } |
||
143 | |||
144 | preg_match_all("|{([^}]+)}|", $source, $result); |
||
145 | return $result[1]; |
||
146 | } |
||
147 | |||
148 | public function parseSource($source) { |
||
149 | $tags = $this->parseRaw($source); |
||
150 | foreach ($tags as $rawTag) { |
||
151 | $tag = explode(':', $rawTag); |
||
152 | switch ($tag[0]) { |
||
153 | case 'CONTENT': |
||
154 | $source = $this->cutTag($source, $rawTag); |
||
155 | $this->content(); |
||
156 | break; |
||
157 | case 'TITLE': |
||
158 | $source = $this->cutTag($source, $rawTag); |
||
159 | echo $this->title; |
||
160 | break; |
||
161 | case 'WIDGET': |
||
162 | $source = $this->cutTag($source, $rawTag); |
||
163 | $params = array_slice($tag, 2); |
||
164 | $this->widget($tag[1], ['params' => $params], ':' . implode(':', $params)); |
||
165 | break; |
||
166 | case 'HEAD': |
||
167 | $source = $this->cutTag($source, $rawTag); |
||
168 | $this->head(); |
||
169 | break; |
||
170 | case 'PAGE': |
||
171 | $source = $this->cutTag($source, $rawTag); |
||
172 | $this->page(['page' => $tag[1]]); |
||
173 | break; |
||
174 | case 'BODYEND': |
||
175 | $source = $this->cutTag($source, $rawTag); |
||
176 | $this->bodyEnd(); |
||
177 | break; |
||
178 | } |
||
179 | } |
||
180 | echo $source; |
||
181 | } |
||
182 | |||
183 | public function cutTag($source, $rawTag) { |
||
184 | $pos = strpos($source, $rawTag) - 1; |
||
185 | echo substr($source, 0, $pos); |
||
186 | return substr($source, ($pos + strlen($rawTag) + 2)); |
||
187 | } |
||
188 | |||
189 | public function getHref($type, $params) { |
||
190 | $href = ''; |
||
191 | if (is_string($params)) { |
||
192 | $href = $params; |
||
193 | } elseif (empty($params['template']) && !empty($params['file'])) { |
||
194 | $href = ($this->app->type != 'app' ? '/' . $this->app->name : '') . $params['file']; |
||
195 | } elseif (!empty($params['template']) && !empty($params['file'])) { |
||
196 | $href = $this->app->templatesPath . "/{$this->template->name}/{$type}/{$params['file']}"; |
||
197 | } |
||
198 | return $href; |
||
199 | } |
||
200 | |||
201 | public function checkNeedLibs() { |
||
202 | if (!empty($this->template->config['libs'])) { |
||
203 | foreach ($this->template->config['libs'] as $libName => $libOptions) { |
||
204 | if (!is_array($libOptions)) { |
||
205 | $libName = $libOptions; |
||
206 | $libOptions = []; |
||
207 | } |
||
208 | $this->app->libs->loadLib($libName, $libOptions); |
||
209 | } |
||
210 | } |
||
211 | View Code Duplication | foreach ($this->dynAssets['js'] as $asset) { |
|
|
|||
212 | if (is_array($asset) && !empty($asset['libs'])) { |
||
213 | foreach ($asset['libs'] as $libName) { |
||
214 | $this->app->libs->loadLib($libName); |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | View Code Duplication | foreach ($this->libAssets['js'] as $asset) { |
|
219 | if (is_array($asset) && !empty($asset['libs'])) { |
||
220 | foreach ($asset['libs'] as $libName) { |
||
221 | $this->app->libs->loadLib($libName); |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
227 | public function head() { |
||
228 | |||
229 | echo "<title>{$this->title}</title>\n"; |
||
230 | if (!empty(\App::$primary->config['site']['favicon']) && file_exists(\App::$primary->path . '/' . \App::$primary->config['site']['favicon'])) { |
||
231 | echo " <link rel='shortcut icon' href='" . \App::$primary->config['site']['favicon'] . "' />"; |
||
232 | } elseif (!empty($this->template->config['favicon']) && file_exists($this->template->path . "/{$this->template->config['favicon']}")) { |
||
233 | echo " <link rel='shortcut icon' href='/templates/{$this->template->name}/{$this->template->config['favicon']}' />"; |
||
234 | } elseif (!empty($this->template->config['favicon']) && file_exists($this->app->path . "/static/images/{$this->template->config['favicon']}")) { |
||
235 | echo " <link rel='shortcut icon' href='/static/images/{$this->template->config['favicon']}' />"; |
||
236 | } elseif (file_exists($this->app->path . '/static/images/favicon.ico')) { |
||
237 | echo " <link rel='shortcut icon' href='/static/images/favicon.ico' />"; |
||
238 | } |
||
239 | |||
240 | foreach ($this->getMetaTags() as $meta) { |
||
241 | echo "\n " . Html::el('meta', $meta, '', null); |
||
242 | } |
||
243 | |||
244 | if (!empty(Inji::$config['assets']['js'])) { |
||
245 | foreach (Inji::$config['assets']['js'] as $js) { |
||
246 | $this->customAsset('js', $js); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | $this->checkNeedLibs(); |
||
251 | $this->parseCss(); |
||
252 | echo "\n <script src='" . Statics::file(($this->app->type != 'app' ? '/' . $this->app->name : '') . "/static/system/js/Inji.js") . "'></script>"; |
||
253 | } |
||
254 | |||
255 | public function parseCss() { |
||
256 | $css = $this->getCss(); |
||
257 | $urls = []; |
||
258 | $timeStr = ''; |
||
259 | $cssAll = ''; |
||
260 | $exclude = ['^http:', '^https:', '^//']; |
||
261 | foreach ($css as $href) { |
||
262 | if (!empty($this->loadedCss[$href])) { |
||
263 | continue; |
||
264 | } |
||
265 | foreach ($exclude as $item) { |
||
266 | if (preg_match("!{$item}!", $href)) { |
||
267 | echo "\n <link href='{$href}' rel='stylesheet' type='text/css' />"; |
||
268 | continue; |
||
269 | } |
||
270 | } |
||
271 | $path = $this->app->staticLoader->parsePath($href); |
||
272 | if (file_exists($path)) { |
||
273 | $this->loadedCss[$href] = $href; |
||
274 | $urls[$href] = $path; |
||
275 | $timeStr .= filemtime($path); |
||
276 | } else { |
||
277 | echo "\n <link href='{$href}' rel='stylesheet' type='text/css' />"; |
||
278 | } |
||
279 | } |
||
280 | $timeMd5 = md5($timeStr); |
||
281 | $cacheDir = Cache::getDir('static'); |
||
282 | if (!file_exists($cacheDir . '/all' . $timeMd5 . '.css')) { |
||
283 | foreach ($urls as $primaryUrl => $url) { |
||
284 | $source = file_get_contents($url); |
||
285 | $rootPath = substr($primaryUrl, 0, strrpos($primaryUrl, '/')); |
||
286 | $levelUpPath = substr($rootPath, 0, strrpos($rootPath, '/')); |
||
287 | $source = preg_replace('!url\((\'?"?)[\.]{2}!isU', 'url($1' . $levelUpPath, $source); |
||
288 | $source = preg_replace('!url\((\'?"?)[\.]{1}!isU', 'url($1' . $rootPath, $source); |
||
289 | $source = preg_replace('#url\(([\'"]){1}(?!http|https|/|data\:)([^/])#isU', 'url($1' . $rootPath . '/$2', $source); |
||
290 | $source = preg_replace('#url\((?!http|https|/|data\:|\'|")([^/])#isU', 'url(' . $rootPath . '/$1$2', $source); |
||
291 | $cssAll .= $source . "\n"; |
||
292 | } |
||
293 | file_put_contents($cacheDir . '/all' . $timeMd5 . '.css', $cssAll); |
||
294 | } |
||
295 | echo "\n <link href='/{$cacheDir}/all{$timeMd5}.css' rel='stylesheet' type='text/css' />"; |
||
296 | } |
||
297 | |||
298 | View Code Duplication | public function getCss() { |
|
299 | $css = []; |
||
300 | if (!empty($this->libAssets['css'])) { |
||
301 | $this->ResolveCssHref($this->libAssets['css'], 'libs', $css); |
||
302 | } |
||
303 | if (!empty($this->template->config['css'])) { |
||
304 | $this->ResolveCssHref($this->template->config['css'], 'template', $css); |
||
305 | } |
||
306 | if (!empty($this->dynAssets['css'])) { |
||
307 | $this->ResolveCssHref($this->dynAssets['css'], 'custom', $css); |
||
308 | } |
||
309 | return $css; |
||
310 | } |
||
311 | |||
312 | public function ResolveCssHref($cssArray, $type = 'custom', &$hrefs) { |
||
313 | switch ($type) { |
||
314 | case 'libs': |
||
315 | foreach ($cssArray as $css) { |
||
316 | if (is_array($css)) { |
||
317 | $this->ResolveCssHref($css, $type, $hrefs); |
||
318 | continue; |
||
319 | } |
||
320 | $hrefs[$css] = $css; |
||
321 | } |
||
322 | break; |
||
323 | View Code Duplication | case 'template': |
|
324 | foreach ($cssArray as $css) { |
||
325 | if (is_array($css)) { |
||
326 | $this->ResolveCssHref($css, $type, $hrefs); |
||
327 | continue; |
||
328 | } |
||
329 | if (strpos($css, '://') !== false) { |
||
330 | $href = $css; |
||
331 | } else { |
||
332 | $href = $this->app->templatesPath . "/{$this->template->name}/css/{$css}"; |
||
333 | } |
||
334 | $hrefs[$href] = $href; |
||
335 | } |
||
336 | break; |
||
337 | View Code Duplication | case 'custom': |
|
338 | foreach ($cssArray as $css) { |
||
339 | if (is_array($css)) { |
||
340 | $this->ResolveCssHref($css, $type, $hrefs); |
||
341 | continue; |
||
342 | } |
||
343 | if (strpos($css, '//') !== false) { |
||
344 | $href = $css; |
||
345 | } else { |
||
346 | $href = ($this->app->type != 'app' ? '/' . $this->app->name : '') . $css; |
||
347 | } |
||
348 | $hrefs[$href] = $href; |
||
349 | } |
||
350 | break; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | public function getMetaTags() { |
||
377 | |||
378 | public function addMetaTag($meta) { |
||
385 | |||
386 | public function bodyEnd() { |
||
447 | |||
448 | View Code Duplication | public function getScripts() { |
|
461 | |||
462 | public function genScriptArray($jsArray, $type = 'custom', &$resultArray) { |
||
516 | |||
517 | public function customAsset($type, $asset, $lib = false) { |
||
526 | |||
527 | public function setTitle($title, $add = true) { |
||
538 | |||
539 | public function widget($_widgetName, $_params = [], $lineParams = null) { |
||
571 | |||
572 | public function getWidgetPaths($widgetName) { |
||
598 | } |
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.