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 |
||
13 | class StyleSheets extends AbstractHelper |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $files = []; |
||
19 | |||
20 | protected $rawStyles = []; |
||
21 | |||
22 | /** |
||
23 | * @param $file |
||
24 | * @param bool $condition |
||
25 | * @return $this |
||
26 | */ |
||
27 | public function add($file, $condition = false) |
||
32 | |||
33 | /** |
||
34 | * @param $content |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function addRaw($content) |
||
43 | |||
44 | /** |
||
45 | * @param $file |
||
46 | * @param bool $condition |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function prepend($file, $condition = false) |
||
57 | |||
58 | /** |
||
59 | * @param $file |
||
60 | * @param bool $condition |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function remove($file, $condition = false) |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | public function __toString() |
||
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | View Code Duplication | public function renderRaw() |
|
100 | |||
101 | /** |
||
102 | * @param $path |
||
103 | * @param $condition |
||
104 | * @return string |
||
105 | */ |
||
106 | public function buildTag($path, $condition = null) |
||
116 | |||
117 | /** |
||
118 | * @param $source |
||
119 | * @return string |
||
120 | */ |
||
121 | public function buildURL($source) |
||
129 | } |
||
130 |
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.