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 | |||
14 | use ModelConfiguration; |
||
15 | |||
16 | protected $unitConversionDataPostfix = '_ucd'; |
||
17 | protected $multiUnitColumns = []; |
||
18 | protected $multiUnitSelectedUnits = []; |
||
19 | |||
20 | private function getUnitConversionDataColumns() |
||
26 | |||
27 | protected static function bootMultiUnitSupport() |
||
65 | |||
66 | /** |
||
67 | * @param $value |
||
68 | * @param AbstractUnit $unit |
||
69 | * @param string[] $requiredUnits |
||
70 | * |
||
71 | * @return array|null |
||
72 | */ |
||
73 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
89 | |||
90 | public function getMultiUnitExistingConversionData($field) |
||
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | protected function getUnitConversionDataPostfix() |
||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | */ |
||
106 | public function getMultiUnitColumns() |
||
110 | |||
111 | /** |
||
112 | * @param $field |
||
113 | * |
||
114 | * @throws NotSupportedMultiUnitField |
||
115 | * |
||
116 | * @return AbstractUnit[] |
||
117 | */ |
||
118 | public function getMultiUnitFieldSupportedUnits($field) |
||
126 | |||
127 | /** |
||
128 | * @param $field |
||
129 | * |
||
130 | * @throws NotSupportedMultiUnitField |
||
131 | * |
||
132 | * @return AbstractUnit |
||
133 | */ |
||
134 | public function getMultiUnitFieldDefaultUnit($field) |
||
144 | |||
145 | /** |
||
146 | * @param $field |
||
147 | * |
||
148 | * @throws NotSupportedMultiUnitField |
||
149 | * |
||
150 | * @return AbstractUnit |
||
151 | */ |
||
152 | public function getMultiUnitFieldSelectedUnit($field) |
||
162 | |||
163 | /** |
||
164 | * @param $field |
||
165 | * @param string $unit |
||
166 | * |
||
167 | * @throws NotSupportedMultiUnitField |
||
168 | * @throws NotSupportedMultiUnitFieldUnit |
||
169 | */ |
||
170 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
193 | |||
194 | /** |
||
195 | * @param $field |
||
196 | * @param string $unit |
||
197 | * |
||
198 | * @throws NotSupportedMultiUnitField |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
236 | |||
237 | /** |
||
238 | * @param $field |
||
239 | * @param AbstractUnit|null $unit |
||
240 | * |
||
241 | * @throws NotSupportedMultiUnitField |
||
242 | * |
||
243 | * @return mixed |
||
244 | */ |
||
245 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
265 | |||
266 | protected function isMultiUnitField($field) |
||
270 | |||
271 | /** |
||
272 | * @param $field |
||
273 | * |
||
274 | * @throws NotSupportedMultiUnitField |
||
275 | * |
||
276 | * @return AbstractUnit |
||
277 | */ |
||
278 | protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
||
282 | } |
||
283 |