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 Group_Field { |
||
9 | /** |
||
10 | * Unique group identificator. Generated randomly. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $group_id; |
||
15 | |||
16 | /** |
||
17 | * Sanitized group name. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $name; |
||
22 | |||
23 | /** |
||
24 | * Group label, used during rendering. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $label; |
||
29 | |||
30 | /** |
||
31 | * Group label underscore template. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $label_template; |
||
36 | |||
37 | /** |
||
38 | * Group fields. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $fields = array(); |
||
43 | |||
44 | /** |
||
45 | * List of registered unique field names |
||
46 | * |
||
47 | * @see verify_unique_field_name() |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $registered_field_names = array(); |
||
51 | |||
52 | /** |
||
53 | * Create a group field with the specified name and label. |
||
54 | * |
||
55 | * @param string $name |
||
56 | * @param string $label |
||
57 | * @param array $fields |
||
58 | */ |
||
59 | public function __construct($name, $label, $fields) { |
||
69 | |||
70 | /** |
||
71 | * Add a group of fields. |
||
72 | * |
||
73 | * @param array $fields |
||
74 | */ |
||
75 | public function add_fields( $fields ) { |
||
91 | |||
92 | /** |
||
93 | * Fields attribute getter. |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | public function get_fields() { |
||
100 | |||
101 | /** |
||
102 | * Return the names of all fields. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function get_field_names() { |
||
115 | |||
116 | /** |
||
117 | * Returns an array that holds the field data, suitable for JSON representation. |
||
118 | * This data will be available in the Underscore template and the Backbone Model. |
||
119 | * |
||
120 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
121 | * @return array |
||
122 | */ |
||
123 | public function to_json( $load ) { |
||
146 | |||
147 | /** |
||
148 | * Group ID attribute getter. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function get_group_id() { |
||
155 | |||
156 | /** |
||
157 | * Set the group label. |
||
158 | * |
||
159 | * @param string $label If null, the label will be generated from the group name |
||
160 | */ |
||
161 | public function set_label( $label ) { |
||
176 | |||
177 | /** |
||
178 | * Label attribute getter. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function get_label() { |
||
185 | |||
186 | /** |
||
187 | * Set the Underscore label template. |
||
188 | * |
||
189 | * @param string $template |
||
190 | */ |
||
191 | public function set_label_template($template) { |
||
194 | |||
195 | /** |
||
196 | * Set the Underscore label template. |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public function get_label_template() { |
||
203 | |||
204 | /** |
||
205 | * Print the label Underscore template. |
||
206 | */ |
||
207 | public function template_label() { |
||
210 | |||
211 | /** |
||
212 | * Set the group name. |
||
213 | * |
||
214 | * @param string $name Group name, either sanitized or not |
||
215 | */ |
||
216 | public function set_name( $name ) { |
||
226 | |||
227 | /** |
||
228 | * Return the group name. |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public function get_name() { |
||
235 | |||
236 | /** |
||
237 | * Assign a DataStore instance for all group fields. |
||
238 | * |
||
239 | * @param object $store |
||
240 | */ |
||
241 | public function set_datastore( Datastore_Interface $store ) { |
||
248 | |||
249 | /** |
||
250 | * Set a prefix for all group fields. |
||
251 | * |
||
252 | * @param string $prefix |
||
253 | */ |
||
254 | public function set_prefix( $prefix ) { |
||
259 | |||
260 | /** |
||
261 | * Perform checks whether there is a field registered with the name $name. |
||
262 | * If not, the field name is recorded. |
||
263 | * |
||
264 | * @param string $name |
||
265 | */ |
||
266 | public function verify_unique_field_name( $name ) { |
||
273 | } |
||
274 |
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.