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 | use ModelConfiguration; |
||
14 | |||
15 | protected $unitConversionDataPostfix = '_ucd'; |
||
16 | protected $multiUnitColumns = []; |
||
17 | protected $multiUnitSelectedUnits = []; |
||
18 | |||
19 | private function getUnitConversionDataColumns() |
||
25 | |||
26 | protected static function bootMultiUnitSupport() |
||
70 | |||
71 | /** |
||
72 | * @param $value |
||
73 | * @param AbstractUnit $unit |
||
74 | * @param string[] $requiredUnits |
||
75 | * |
||
76 | * @return array|null |
||
77 | */ |
||
78 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
95 | |||
96 | public function getMultiUnitExistingConversionData($field) |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | protected function getUnitConversionDataPostfix() |
||
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getMultiUnitColumns() |
||
116 | |||
117 | /** |
||
118 | * @param $field |
||
119 | * |
||
120 | * @throws NotSupportedMultiUnitField |
||
121 | * |
||
122 | * @return AbstractUnit[] |
||
123 | */ |
||
124 | public function getMultiUnitFieldSupportedUnits($field) |
||
132 | |||
133 | /** |
||
134 | * @param $field |
||
135 | * |
||
136 | * @throws NotSupportedMultiUnitField |
||
137 | * |
||
138 | * @return AbstractUnit |
||
139 | */ |
||
140 | public function getMultiUnitFieldDefaultUnit($field) |
||
150 | |||
151 | /** |
||
152 | * @param $field |
||
153 | * |
||
154 | * @throws NotSupportedMultiUnitField |
||
155 | * |
||
156 | * @return AbstractUnit |
||
157 | */ |
||
158 | public function getMultiUnitFieldSelectedUnit($field) |
||
168 | |||
169 | /** |
||
170 | * @param $field |
||
171 | * @param string $unit |
||
172 | * |
||
173 | * @throws NotSupportedMultiUnitField |
||
174 | * @throws NotSupportedMultiUnitFieldUnit |
||
175 | */ |
||
176 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
199 | |||
200 | /** |
||
201 | * @param $field |
||
202 | * @param string $unit |
||
203 | * |
||
204 | * @throws NotSupportedMultiUnitField |
||
205 | * |
||
206 | * @return mixed |
||
207 | */ |
||
208 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
242 | |||
243 | /** |
||
244 | * @param $field |
||
245 | * @param AbstractUnit|null $unit |
||
246 | * |
||
247 | * @throws NotSupportedMultiUnitField |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
271 | |||
272 | protected function isMultiUnitField($field) |
||
276 | |||
277 | /** |
||
278 | * @param $field |
||
279 | * |
||
280 | * @throws NotSupportedMultiUnitField |
||
281 | * |
||
282 | * @return AbstractUnit |
||
283 | */ |
||
284 | protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
||
288 | } |
||
289 |