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 |
||
9 | class Scripts extends AbstractHelper |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $files = []; |
||
16 | |||
17 | protected $rawScripts = []; |
||
18 | |||
19 | protected $defaultPlaceholder = "head"; |
||
20 | |||
21 | /** |
||
22 | * @param $file |
||
23 | * @param bool $placeholder |
||
24 | * @return $this |
||
25 | */ |
||
26 | public function add($file, $placeholder = false) |
||
30 | |||
31 | /** |
||
32 | * @param $file |
||
33 | * @param string $direction |
||
34 | * @param bool $placeholder |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function addFile($file, $direction = 'add', $placeholder = false) |
||
55 | |||
56 | /** |
||
57 | * @param $content |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function addRaw($content) |
||
66 | |||
67 | /** |
||
68 | * @param $file |
||
69 | * @param bool $placeholder |
||
70 | * @return Scripts |
||
71 | */ |
||
72 | public function prepend($file, $placeholder = false) |
||
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | public function __toString() |
||
84 | |||
85 | /** |
||
86 | * @param bool $placeholder |
||
87 | * @return string |
||
88 | */ |
||
89 | public function render($placeholder = false) |
||
100 | |||
101 | /** |
||
102 | * @param $files |
||
103 | * @return string |
||
104 | */ |
||
105 | public function renderHMTL($files) |
||
134 | |||
135 | /** |
||
136 | * @param $path |
||
137 | * @return string |
||
138 | */ |
||
139 | public function buildTag($path) |
||
143 | |||
144 | /** |
||
145 | * @param $source |
||
146 | * @return string |
||
147 | */ |
||
148 | public function buildURL($source) |
||
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | public function renderRaw() |
||
166 | } |
||
167 |
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.