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 |
||
16 | class View extends \yii\web\View |
||
17 | { |
||
18 | |||
19 | /** @var bool */ |
||
20 | public $enableMinify = true; |
||
21 | |||
22 | /** @var bool */ |
||
23 | public $minifyCss = true; |
||
24 | |||
25 | /** @var bool */ |
||
26 | public $minifyJs = true; |
||
27 | |||
28 | /** @var bool */ |
||
29 | public $removeComments = true; |
||
30 | |||
31 | /** @var string path alias to web base (in url) */ |
||
32 | public $web_path = '@web'; |
||
33 | |||
34 | /** @var string path alias to web base (absolute) */ |
||
35 | public $base_path = '@webroot'; |
||
36 | |||
37 | /** @var string path alias to save minify result */ |
||
38 | public $minify_path = '@webroot/minify'; |
||
39 | |||
40 | /** @var array positions of js files to be minified */ |
||
41 | public $js_position = [self::POS_END, self::POS_HEAD]; |
||
42 | |||
43 | /** @var bool|string charset forcibly assign, otherwise will use all of the files found charset */ |
||
44 | public $force_charset = false; |
||
45 | |||
46 | /** @var bool whether to change @import on content */ |
||
47 | public $expand_imports = true; |
||
48 | |||
49 | /** @var int */ |
||
50 | public $css_linebreak_pos = 2048; |
||
51 | |||
52 | /** @var int|bool chmod of minified file. If false chmod not set */ |
||
53 | public $file_mode = 0664; |
||
54 | |||
55 | /** @var array schemes that will be ignored during normalization url */ |
||
56 | public $schemas = ['//', 'http://', 'https://', 'ftp://']; |
||
57 | |||
58 | /** @var bool do I need to compress the result html page. */ |
||
59 | public $compress_output = false; |
||
60 | |||
61 | /** |
||
62 | * @throws \rmrevin\yii\minify\Exception |
||
63 | */ |
||
64 | 6 | public function init() |
|
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | 4 | public function endPage($ajaxMode = false) |
|
131 | |||
132 | /** |
||
133 | * @return self |
||
134 | */ |
||
135 | 4 | protected function minifyCSS() |
|
167 | |||
168 | /** |
||
169 | * @param array $files |
||
170 | */ |
||
171 | 4 | protected function processMinifyCss($files) |
|
232 | |||
233 | /** |
||
234 | * @return self |
||
235 | */ |
||
236 | 3 | protected function minifyJS() |
|
277 | |||
278 | /** |
||
279 | * @param integer $position |
||
280 | * @param array $files |
||
281 | */ |
||
282 | 3 | protected function processMinifyJs($position, $files) |
|
309 | |||
310 | /** |
||
311 | * @param string $url |
||
312 | * @param boolean $checkSlash |
||
313 | * @return bool |
||
314 | */ |
||
315 | 4 | protected function isUrl($url, $checkSlash = true) |
|
324 | |||
325 | /** |
||
326 | * @param string $string |
||
327 | * @return bool |
||
328 | */ |
||
329 | 4 | protected function isContainsConditionalComment($string) |
|
333 | |||
334 | /** |
||
335 | * @param string $file |
||
336 | * @param string $html |
||
337 | * @return bool |
||
338 | */ |
||
339 | 4 | protected function thisFileNeedMinify($file, $html) |
|
343 | |||
344 | /** |
||
345 | * @param string $code |
||
346 | * @return string |
||
347 | */ |
||
348 | 2 | protected function collectCharsets(&$code) |
|
354 | |||
355 | /** |
||
356 | * @param string $code |
||
357 | * @return string |
||
358 | */ |
||
359 | 3 | protected function collectImports(&$code) |
|
365 | |||
366 | /** |
||
367 | * @param string $code |
||
368 | * @return string |
||
369 | */ |
||
370 | protected function collectFonts(&$code) |
||
376 | |||
377 | /** |
||
378 | * @param string $code |
||
379 | */ |
||
380 | 3 | protected function removeCssComments(&$code) |
|
386 | |||
387 | /** |
||
388 | * @param string $code |
||
389 | */ |
||
390 | 3 | protected function removeJsComments(&$code) |
|
397 | |||
398 | /** |
||
399 | * @param string $code |
||
400 | */ |
||
401 | 4 | protected function expandImports(&$code) |
|
418 | |||
419 | /** |
||
420 | * @param string $url |
||
421 | * @return null|string |
||
422 | */ |
||
423 | 2 | protected function _getImportContent($url) |
|
441 | |||
442 | /** |
||
443 | * @param string $code |
||
444 | * @param string $pattern |
||
445 | * @param callable $handler |
||
446 | * @return string |
||
447 | */ |
||
448 | 3 | protected function _collect(&$code, $pattern, $handler) |
|
462 | |||
463 | /** |
||
464 | * @param string $file |
||
465 | * @return string |
||
466 | */ |
||
467 | 4 | protected function getAbsoluteFilePath($file) |
|
471 | |||
472 | /** |
||
473 | * @param array $files |
||
474 | * @return string |
||
475 | */ |
||
476 | 4 | protected function _getSummaryFilesHash($files) |
|
489 | } |
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.