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:
Complex classes like FieldContainer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FieldContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class FieldContainer extends JsonObject |
||
29 | { |
||
30 | 47 | public function fieldDefinition($field) |
|
58 | |||
59 | /** |
||
60 | * @param string $field |
||
61 | * @param mixed $value |
||
62 | * @return $this |
||
63 | */ |
||
64 | 6 | public function set($field, $value) |
|
68 | |||
69 | 20 | protected function isValidField($field) |
|
73 | |||
74 | /** |
||
75 | * @return bool |
||
76 | */ |
||
77 | 1 | public function getFieldAsBool($name) |
|
81 | |||
82 | /** |
||
83 | * @return float |
||
84 | */ |
||
85 | 1 | public function getFieldAsNumber($name) |
|
90 | |||
91 | /** |
||
92 | * @return int |
||
93 | */ |
||
94 | 1 | public function getFieldAsInteger($name) |
|
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | public function getFieldAsString($name) |
|
107 | |||
108 | /** |
||
109 | * @return LocalizedString |
||
110 | */ |
||
111 | 1 | public function getFieldAsLocalizedString($name) |
|
120 | |||
121 | /** |
||
122 | * @return LocalizedEnum |
||
123 | */ |
||
124 | 1 | public function getFieldAsLocalizedEnum($name) |
|
133 | |||
134 | /** |
||
135 | * @return Enum |
||
136 | */ |
||
137 | 1 | public function getFieldAsEnum($name) |
|
146 | |||
147 | /** |
||
148 | * @return Money |
||
149 | */ |
||
150 | 1 | public function getFieldAsMoney($name) |
|
159 | |||
160 | /** |
||
161 | * @return DateDecorator |
||
162 | */ |
||
163 | 1 | public function getFieldAsDate($name) |
|
172 | |||
173 | /** |
||
174 | * @return TimeDecorator |
||
175 | */ |
||
176 | 1 | public function getFieldAsTime($name) |
|
185 | |||
186 | /** |
||
187 | * @return DateTimeDecorator |
||
188 | */ |
||
189 | 1 | public function getFieldAsDateTime($name) |
|
198 | |||
199 | /** |
||
200 | * @return Reference |
||
201 | */ |
||
202 | 1 | public function getFieldAsReference($name) |
|
211 | |||
212 | /** |
||
213 | * @return Set |
||
214 | */ |
||
215 | 1 | View Code Duplication | public function getFieldAsBoolSet($name) |
224 | |||
225 | /** |
||
226 | * @return Set |
||
227 | */ |
||
228 | 1 | View Code Duplication | public function getFieldAsNumberSet($name) |
237 | |||
238 | /** |
||
239 | * @return Set |
||
240 | */ |
||
241 | 1 | View Code Duplication | public function getFieldAsIntegerSet($name) |
250 | |||
251 | /** |
||
252 | * @return Set |
||
253 | */ |
||
254 | 1 | View Code Duplication | public function getFieldAsStringSet($name) |
263 | |||
264 | /** |
||
265 | * @return Set |
||
266 | */ |
||
267 | 1 | View Code Duplication | public function getFieldAsLocalizedStringSet($name) |
276 | |||
277 | /** |
||
278 | * @return Set |
||
279 | */ |
||
280 | 1 | View Code Duplication | public function getFieldAsLocalizedEnumSet($name) |
289 | |||
290 | /** |
||
291 | * @return Set |
||
292 | */ |
||
293 | 1 | View Code Duplication | public function getFieldAsEnumSet($name) |
302 | |||
303 | /** |
||
304 | * @return Set |
||
305 | */ |
||
306 | 1 | View Code Duplication | public function getFieldAsMoneySet($name) |
315 | |||
316 | /** |
||
317 | * @return Set |
||
318 | */ |
||
319 | 1 | View Code Duplication | public function getFieldAsDateSet($name) |
328 | |||
329 | /** |
||
330 | * @return Set |
||
331 | */ |
||
332 | 1 | View Code Duplication | public function getFieldAsTimeSet($name) |
341 | |||
342 | /** |
||
343 | * @return Set |
||
344 | */ |
||
345 | 1 | View Code Duplication | public function getFieldAsDateTimeSet($name) |
354 | |||
355 | /** |
||
356 | * @return Set |
||
357 | */ |
||
358 | 1 | View Code Duplication | public function getFieldAsReferenceSet($name) |
367 | } |
||
368 |
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.