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 |
||
15 | abstract class BasicObject { |
||
16 | |||
17 | /** |
||
18 | * List of the database entity fields. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $fieldsList; |
||
23 | |||
24 | /** |
||
25 | * List of fields aliases. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $fieldsAliases = []; |
||
30 | |||
31 | /** |
||
32 | * Sets values of the object fields. |
||
33 | * |
||
34 | * @param array<string> $valuesList List of the values. |
||
35 | * |
||
36 | * @return mixed Number of added values or FALSE. |
||
37 | */ |
||
38 | public function setFieldsValues($valuesList) { |
||
75 | |||
76 | /** |
||
77 | * Sets value to the object's field. |
||
78 | * |
||
79 | * @param string $fieldName Name of the field. |
||
80 | * @param mixed $fieldValue Value of the field. |
||
81 | * |
||
82 | * @return object Object itself on success (for the method chaining support). |
||
83 | * @throws \Exception If object has no field with such name. |
||
84 | */ |
||
85 | View Code Duplication | public function setFieldValue($fieldName, $fieldValue) { |
|
98 | |||
99 | /** |
||
100 | * Returns fields list array. |
||
101 | * |
||
102 | * @param bool $withAliases If this flag is `true` then we will have fields |
||
103 | * aliases in the result array as well. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getFieldsList($withAliases = false) { |
||
125 | |||
126 | /** |
||
127 | * Returns value of the field by it's name or alias. |
||
128 | * |
||
129 | * @param string $fieldName Field name or alias. |
||
130 | * |
||
131 | * @return mixed |
||
132 | * @throws \Exception If object doesn't have this field or alias. |
||
133 | */ |
||
134 | View Code Duplication | public function getFieldValue($fieldName) { |
|
145 | |||
146 | /** |
||
147 | * Returns type custed new empty field value. |
||
148 | * |
||
149 | * @param mixed $fieldValue Current field value. |
||
150 | * @param mixed $newValue New value. |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | private static function getEmptyValue($fieldValue, $newValue) { |
||
165 | |||
166 | /** |
||
167 | * Shows current object in structure view in the browser. |
||
168 | */ |
||
169 | public function show() { |
||
174 | |||
175 | /** |
||
176 | * Returns object's field name by getter/setter method name. |
||
177 | * |
||
178 | * @param string $methodNameFragment Method name fragment without 'get' or |
||
179 | * 'set' prefix. |
||
180 | * @return string Corresponded field name. |
||
181 | */ |
||
182 | protected function getFieldName($methodNameFragment) { |
||
185 | |||
186 | /** |
||
187 | * Magic method to wrap getters and setters with own methods. |
||
188 | * |
||
189 | * @param string $methodName Name of the method. |
||
190 | * @param array $methodParams Array of method parameters. |
||
191 | * |
||
192 | * @return mixed |
||
193 | * @throws \Exception If some method is invalid or not exists. |
||
194 | */ |
||
195 | public function __call($methodName, $methodParams) { |
||
210 | |||
211 | /** |
||
212 | * Magic method to wrap setters as fields values assignment. |
||
213 | * |
||
214 | * @param string $fieldName Name of the field. |
||
215 | * @param mixed $fieldValue Value of the field. |
||
216 | * |
||
217 | * @return mixed The return value of the callback, or FALSE on error. |
||
218 | */ |
||
219 | public function __set($fieldName, $fieldValue) { |
||
222 | |||
223 | /** |
||
224 | * Magic method to wrap getters as fields values calls. |
||
225 | * |
||
226 | * @param string $fieldName Name of the field. |
||
227 | * |
||
228 | * @return mixed The return value of the callback, or FALSE on error. |
||
229 | */ |
||
230 | public function __get($fieldName) { |
||
233 | |||
234 | } |
||
235 |
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.