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 |
||
12 | class BaseModel extends Model |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | public static $tag; |
||
18 | |||
19 | /** |
||
20 | * @var string[] |
||
21 | */ |
||
22 | public static $tagProperties = []; |
||
23 | |||
24 | /** |
||
25 | * @return string[] |
||
26 | 9 | */ |
|
27 | public function getYmlAttributes() |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | 9 | */ |
|
35 | public function getYml() |
||
45 | |||
46 | /** |
||
47 | * @param array $params |
||
48 | */ |
||
49 | public function setParams(array $params) |
||
53 | |||
54 | /** |
||
55 | * @param array $pictures |
||
56 | */ |
||
57 | public function setPictures(array $pictures) |
||
61 | |||
62 | /** |
||
63 | * @param array $options |
||
64 | 9 | */ |
|
65 | public function setDeliveryOptions(array $options) |
||
69 | 6 | ||
70 | /** |
||
71 | 9 | * @param $valuesModel |
|
72 | * @param null $onValidationError |
||
73 | * |
||
74 | * @return bool |
||
75 | * @throws Exception |
||
76 | */ |
||
77 | 9 | public function loadModel($valuesModel, $onValidationError = null) |
|
100 | 9 | ||
101 | 9 | /** |
|
102 | * @return string |
||
103 | 9 | */ |
|
104 | 9 | View Code Duplication | protected function getYmlStartTag() |
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | 9 | View Code Duplication | protected function getYmlEndTag() |
126 | 9 | ||
127 | /** |
||
128 | 9 | * @return string |
|
129 | 9 | */ |
|
130 | 9 | protected function getYmlBody() |
|
134 | |||
135 | 9 | /** |
|
136 | * @return string |
||
137 | */ |
||
138 | protected function getYmlTagProperties() |
||
152 | |||
153 | /** |
||
154 | * @param string $attribute |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function getAttributeValue($attribute) |
||
161 | |||
162 | /** |
||
163 | * @param string $attribute |
||
164 | * @return string |
||
165 | */ |
||
166 | protected function getYmlAttribute($attribute) |
||
177 | } |
||
178 |
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.