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 MultiUnitSupport 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 MultiUnitSupport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait MultiUnitSupport |
||
12 | { |
||
13 | protected $unitAttributePostfix = '_units'; |
||
14 | protected $unitConversionDataPostfix = '_ucd'; |
||
15 | protected $multiUnitColumns = []; |
||
16 | protected $multiUnitSelectedUnits = []; |
||
17 | |||
18 | private function getUnitConversionDataColumns() |
||
24 | |||
25 | private function getUnitConversionUnitColumns() |
||
31 | |||
32 | /** |
||
33 | * Allows to set input units and process them before multi-unit field. |
||
34 | * |
||
35 | * @param array $attributes |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | protected function fillableFromArray(array $attributes) |
||
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | public function getFillable() |
||
51 | |||
52 | /** |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public function getHidden() |
||
59 | |||
60 | protected static function bootMultiUnitSupport() |
||
100 | |||
101 | private function forgetUnitsInput(){ |
||
109 | |||
110 | /** |
||
111 | * @param $value |
||
112 | * @param AbstractUnit $unit |
||
113 | * @param string[] $requiredUnits |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
130 | |||
131 | public function getMultiUnitExistingConversionData($field) |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getUnitAttributePostfix() |
||
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function getUnitConversionDataPostfix() |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getMultiUnitColumns() |
||
159 | |||
160 | /** |
||
161 | * @param $field |
||
162 | * |
||
163 | * @throws NotSupportedMultiUnitField |
||
164 | * |
||
165 | * @return AbstractUnit[] |
||
166 | */ |
||
167 | public function getMultiUnitFieldSupportedUnits($field) |
||
175 | |||
176 | /** |
||
177 | * @param $field |
||
178 | * |
||
179 | * @throws NotSupportedMultiUnitField |
||
180 | * |
||
181 | * @return AbstractUnit |
||
182 | */ |
||
183 | public function getMultiUnitFieldDefaultUnit($field) |
||
193 | |||
194 | /** |
||
195 | * @param $field |
||
196 | * |
||
197 | * @throws NotSupportedMultiUnitField |
||
198 | * |
||
199 | * @return AbstractUnit |
||
200 | */ |
||
201 | public function getMultiUnitFieldSelectedUnit($field) |
||
211 | |||
212 | /** |
||
213 | * @param $field |
||
214 | * @param string $unit |
||
215 | * |
||
216 | * @throws NotSupportedMultiUnitField |
||
217 | * @throws NotSupportedMultiUnitFieldUnit |
||
218 | */ |
||
219 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
242 | |||
243 | /** |
||
244 | * @param $field |
||
245 | * @param string $unit |
||
246 | * |
||
247 | * @throws NotSupportedMultiUnitField |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
285 | |||
286 | /** |
||
287 | * @param $field |
||
288 | * @param AbstractUnit|null $unit |
||
289 | * |
||
290 | * @throws NotSupportedMultiUnitField |
||
291 | * |
||
292 | * @return mixed |
||
293 | */ |
||
294 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
314 | |||
315 | protected function isMultiUnitField($field) |
||
319 | |||
320 | /** |
||
321 | * @param $field |
||
322 | * |
||
323 | * @throws NotSupportedMultiUnitField |
||
324 | * |
||
325 | * @return AbstractUnit |
||
326 | */ |
||
327 | protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
||
343 | |||
344 | protected function forgetMultiUnitFieldUnitInput($field) |
||
351 | |||
352 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
359 | |||
360 | /** |
||
361 | * @param $field |
||
362 | * |
||
363 | * @throws NotSupportedMultiUnitField |
||
364 | */ |
||
365 | protected function resetMultiUnitFieldUnit($field) |
||
369 | |||
370 | /** |
||
371 | * Determine if a set mutator exists for an attribute. |
||
372 | * |
||
373 | * @param string $key |
||
374 | * |
||
375 | * @return bool |
||
376 | */ |
||
377 | public function hasSetMutator($key) |
||
385 | |||
386 | /** |
||
387 | * Set the value of an attribute using its mutator. |
||
388 | * |
||
389 | * @param string $key |
||
390 | * @param mixed $value |
||
391 | * |
||
392 | * @throws NotSupportedMultiUnitField |
||
393 | * |
||
394 | * @return mixed |
||
395 | */ |
||
396 | protected function setMutatedAttributeValue($key, $value) |
||
411 | |||
412 | /** |
||
413 | * Detect changes and set proper base value. |
||
414 | * |
||
415 | * @param $field |
||
416 | * @param $value |
||
417 | * |
||
418 | * @throws NotSupportedMultiUnitField |
||
419 | * |
||
420 | * @return mixed |
||
421 | */ |
||
422 | private function processMultiUnitFieldChanges($field, $value) |
||
446 | |||
447 | /** |
||
448 | * Determine if a get mutator exists for an attribute. |
||
449 | *coo. |
||
450 | * |
||
451 | * @param string $key |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function hasGetMutator($key) |
||
463 | |||
464 | /** |
||
465 | * Get the value of an attribute using its mutator. |
||
466 | * |
||
467 | * @param string $key |
||
468 | * @param mixed $value |
||
469 | * |
||
470 | * @throws NotSupportedMultiUnitField |
||
471 | * |
||
472 | * @return mixed |
||
473 | */ |
||
474 | public function mutateAttribute($key, $value) |
||
489 | } |
||
490 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: