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 string filemtime or sha1 */ |
||
23 | public $fileCheckAlgorithm = 'filemtime'; |
||
24 | |||
25 | /** @var bool */ |
||
26 | public $minifyCss = true; |
||
27 | |||
28 | /** @var bool */ |
||
29 | public $minifyJs = true; |
||
30 | |||
31 | /** @var bool */ |
||
32 | public $removeComments = true; |
||
33 | |||
34 | /** @var string path alias to web base (in url) */ |
||
35 | public $web_path = '@web'; |
||
36 | |||
37 | /** @var string path alias to web base (absolute) */ |
||
38 | public $base_path = '@webroot'; |
||
39 | |||
40 | /** @var string path alias to save minify result */ |
||
41 | public $minify_path = '@webroot/minify'; |
||
42 | |||
43 | /** @var array positions of js files to be minified */ |
||
44 | public $js_position = [self::POS_END, self::POS_HEAD]; |
||
45 | |||
46 | /** @var bool|string charset forcibly assign, otherwise will use all of the files found charset */ |
||
47 | public $force_charset = false; |
||
48 | |||
49 | /** @var bool whether to change @import on content */ |
||
50 | public $expand_imports = true; |
||
51 | |||
52 | /** @var int */ |
||
53 | public $css_linebreak_pos = 2048; |
||
54 | |||
55 | /** @var int|bool chmod of minified file. If false chmod not set */ |
||
56 | public $file_mode = 0664; |
||
57 | |||
58 | /** @var array schemes that will be ignored during normalization url */ |
||
59 | public $schemas = ['//', 'http://', 'https://', 'ftp://']; |
||
60 | |||
61 | /** @var bool do I need to compress the result html page. */ |
||
62 | public $compress_output = false; |
||
63 | |||
64 | /** |
||
65 | * @var array options for compressing output result |
||
66 | * * extra - use more compact algorithm |
||
67 | * * no-comments - cut all the html comments |
||
68 | */ |
||
69 | public $compress_options = ['extra' => true]; |
||
70 | |||
71 | /** |
||
72 | * @throws \rmrevin\yii\minify\Exception |
||
73 | */ |
||
74 | 6 | public function init() |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 4 | public function endPage($ajaxMode = false) |
|
141 | |||
142 | /** |
||
143 | * @return self |
||
144 | */ |
||
145 | 4 | protected function minifyCSS() |
|
177 | |||
178 | /** |
||
179 | * @param array $files |
||
180 | */ |
||
181 | 4 | protected function processMinifyCss($files) |
|
245 | |||
246 | /** |
||
247 | * @return self |
||
248 | */ |
||
249 | 4 | protected function minifyJS() |
|
290 | |||
291 | /** |
||
292 | * @param integer $position |
||
293 | * @param array $files |
||
294 | */ |
||
295 | 4 | protected function processMinifyJs($position, $files) |
|
321 | |||
322 | /** |
||
323 | * @param string $url |
||
324 | * @param boolean $checkSlash |
||
325 | * @return bool |
||
326 | */ |
||
327 | protected function isUrl($url, $checkSlash = true) |
||
336 | |||
337 | /** |
||
338 | * @param string $string |
||
339 | * @return bool |
||
340 | */ |
||
341 | protected function isContainsConditionalComment($string) |
||
345 | |||
346 | /** |
||
347 | * @param string $file |
||
348 | * @param string $html |
||
349 | * @return bool |
||
350 | */ |
||
351 | protected function thisFileNeedMinify($file, $html) |
||
355 | |||
356 | /** |
||
357 | * @param string $code |
||
358 | * @return string |
||
359 | */ |
||
360 | 2 | protected function collectCharsets(&$code) |
|
366 | |||
367 | /** |
||
368 | * @param string $code |
||
369 | * @return string |
||
370 | */ |
||
371 | protected function collectImports(&$code) |
||
377 | |||
378 | /** |
||
379 | * @param string $code |
||
380 | * @return string |
||
381 | */ |
||
382 | protected function collectFonts(&$code) |
||
388 | |||
389 | /** |
||
390 | * @param string $code |
||
391 | */ |
||
392 | 4 | protected function removeCssComments(&$code) |
|
398 | |||
399 | /** |
||
400 | * @param string $code |
||
401 | */ |
||
402 | 4 | protected function removeJsComments(&$code) |
|
409 | |||
410 | /** |
||
411 | * @param string $code |
||
412 | */ |
||
413 | 4 | protected function expandImports(&$code) |
|
430 | |||
431 | /** |
||
432 | * @param string $url |
||
433 | * @return null|string |
||
434 | */ |
||
435 | protected function _getImportContent($url) |
||
453 | |||
454 | /** |
||
455 | * @param string $code |
||
456 | * @param string $pattern |
||
457 | * @param callable $handler |
||
458 | * @return string |
||
459 | */ |
||
460 | protected function _collect(&$code, $pattern, $handler) |
||
474 | |||
475 | /** |
||
476 | * @param string $file |
||
477 | * @return string |
||
478 | */ |
||
479 | protected function cleanFileName($file) |
||
483 | |||
484 | /** |
||
485 | * @param string $file |
||
486 | * @return string |
||
487 | */ |
||
488 | protected function getAbsoluteFilePath($file) |
||
492 | |||
493 | /** |
||
494 | * @param array $files |
||
495 | * @return string |
||
496 | */ |
||
497 | protected function _getSummaryFilesHash($files) |
||
518 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.