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 |
||
8 | class Field |
||
9 | { |
||
10 | public $crudApi; |
||
11 | |||
12 | /** |
||
13 | * Field constructor. |
||
14 | * |
||
15 | * @param CrudApi $crudApi |
||
16 | */ |
||
17 | public function __construct(CrudApi $crudApi) |
||
21 | |||
22 | /** |
||
23 | * Determine if the field is an id field. |
||
24 | * |
||
25 | * @param $field |
||
26 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | public function isIdField($field) |
||
33 | |||
34 | /** |
||
35 | * Parse a relation field name into the relation name. |
||
36 | * |
||
37 | * @param string $field Field name |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getRelatedField($field) |
||
47 | |||
48 | /** |
||
49 | * Retrieve the models primary field for display purposes. |
||
50 | * |
||
51 | * @param $item Model to retrieve primary field of |
||
52 | * @param null|array $config CrudApi Configuration |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getPrimaryField($item, $config = null) |
||
86 | |||
87 | /** |
||
88 | * Display the primary field. |
||
89 | * |
||
90 | * @param $item |
||
91 | * @param null $config |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function displayPrimaryField($item, $config = null) |
||
101 | |||
102 | /** |
||
103 | * Render fields into appropriate format for an item creation form. |
||
104 | * |
||
105 | * @param $fields |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | View Code Duplication | public function formCreate($fields) |
|
157 | |||
158 | /** |
||
159 | * Render fields into appropriate format for an edit form. |
||
160 | * |
||
161 | * @param array $fields Fields to render. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | View Code Duplication | public function formEdit($fields) |
|
210 | |||
211 | /** |
||
212 | * Return fields as table headings. |
||
213 | * |
||
214 | * @param array $fields Fields to display. |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function tableHeadings($fields) |
||
228 | |||
229 | /** |
||
230 | * Output the instances fields into table content format. |
||
231 | * |
||
232 | * @param array $fields |
||
233 | * @param $instance |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function tableContent($fields, $instance) |
||
262 | |||
263 | /** |
||
264 | * Convert a field internal name into human readable name. |
||
265 | * |
||
266 | * Example: |
||
267 | * 'Organisation_id' becomes 'Organisation Id'. |
||
268 | * |
||
269 | * @param string $field Field to convert. |
||
270 | * |
||
271 | * @return mixed|string |
||
272 | */ |
||
273 | public function humanReadableField($field) |
||
279 | } |
||
280 |
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.