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:
1 | <?php |
||
4 | class Css { |
||
5 | |||
6 | public static $list = []; |
||
7 | private static $host; |
||
8 | private static $config; |
||
9 | |||
10 | View Code Duplication | public function __construct(){ |
|
18 | |||
19 | public function add($css){ |
||
23 | |||
24 | View Code Duplication | public function get(){ |
|
25 | $css = array_unique(self::$list); |
||
26 | $code = ''; |
||
27 | foreach($css as $name){ |
||
28 | $file = self::$config['core']['css'].'/'.$name.'.css'; |
||
29 | if(is_file($file)) $code .= file_get_contents($file); |
||
30 | } |
||
31 | $minifier = new \MatthiasMullie\Minify\CSS(); |
||
32 | $minifier->add($code); |
||
33 | $code = $minifier->minify(); |
||
34 | return "<style>$code</style>"; |
||
35 | } |
||
36 | |||
37 | public function compile(){ |
||
40 | |||
41 | } |
||
42 | ?> |
||
43 |
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.