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 |
||
10 | trait MultiUnitSupport |
||
11 | { |
||
12 | protected $unitAttributePostfix = '_units'; |
||
13 | protected $unitConversionDataPostfix = '_ucd'; |
||
14 | protected $multiUnitColumns = []; |
||
15 | |||
16 | private function getUnitConversionDataColumns() |
||
22 | |||
23 | private function getUnitConversionUnitColumns() |
||
29 | |||
30 | /** |
||
31 | * Allows to set input units and process them before multi-unit field. |
||
32 | * |
||
33 | * @param array $attributes |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | protected function fillableFromArray(array $attributes) |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getFillable() |
||
49 | |||
50 | /** |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function getHidden() |
||
57 | |||
58 | protected static function bootMultiUnitSupport() |
||
95 | |||
96 | /** |
||
97 | * @param $value |
||
98 | * @param AbstractUnit $unit |
||
99 | * @param string[] $requiredUnits |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
116 | |||
117 | public function getMultiUnitExistingConversionData($field) |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getUnitAttributePostfix() |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | protected function getUnitConversionDataPostfix() |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | protected function getMultiUnitColumns() |
||
145 | |||
146 | /** |
||
147 | * @param $field |
||
148 | * |
||
149 | * @throws NotSupportedMultiUnitField |
||
150 | * |
||
151 | * @return AbstractUnit[] |
||
152 | */ |
||
153 | public function getMultiUnitFieldSupportedUnits($field) |
||
161 | |||
162 | /** |
||
163 | * @param $field |
||
164 | * |
||
165 | * @throws NotSupportedMultiUnitField |
||
166 | * |
||
167 | * @return AbstractUnit |
||
168 | */ |
||
169 | public function getMultiUnitFieldDefaultUnit($field) |
||
179 | |||
180 | /** |
||
181 | * @param $field |
||
182 | * @param AbstractUnit|null $unit |
||
183 | * |
||
184 | * @throws NotSupportedMultiUnitField |
||
185 | * |
||
186 | * @return mixed |
||
187 | */ |
||
188 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
208 | |||
209 | protected function isMultiUnitField($field) |
||
213 | |||
214 | /** |
||
215 | * @param $field |
||
216 | * |
||
217 | * @throws NotSupportedMultiUnitField |
||
218 | * |
||
219 | * @return AbstractUnit |
||
220 | */ |
||
221 | protected function getMultiUnitFieldUnit($field) |
||
237 | |||
238 | protected function forgetMultiUnitFieldUnitInput($field) |
||
245 | |||
246 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
251 | |||
252 | /** |
||
253 | * @param $field |
||
254 | * |
||
255 | * @throws NotSupportedMultiUnitField |
||
256 | */ |
||
257 | protected function resetMultiUnitFieldUnit($field) |
||
261 | |||
262 | /** |
||
263 | * Determine if a set mutator exists for an attribute. |
||
264 | * |
||
265 | * @param string $key |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | public function hasSetMutator($key) |
||
277 | |||
278 | /** |
||
279 | * Set the value of an attribute using its mutator. |
||
280 | * |
||
281 | * @param string $key |
||
282 | * @param mixed $value |
||
283 | * |
||
284 | * @throws NotSupportedMultiUnitField |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | protected function setMutatedAttributeValue($key, $value) |
||
299 | |||
300 | /** |
||
301 | * Detect changes and set proper base value. |
||
302 | * |
||
303 | * @param $field |
||
304 | * @param $value |
||
305 | * |
||
306 | * @throws NotSupportedMultiUnitField |
||
307 | * |
||
308 | * @return mixed |
||
309 | */ |
||
310 | private function processMultiUnitFieldChanges($field, $value) |
||
334 | |||
335 | /** |
||
336 | * Determine if a get mutator exists for an attribute. |
||
337 | *coo. |
||
338 | * |
||
339 | * @param string $key |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | public function hasGetMutator($key) |
||
351 | |||
352 | /** |
||
353 | * Get the value of an attribute using its mutator. |
||
354 | * |
||
355 | * @param string $key |
||
356 | * @param mixed $value |
||
357 | * |
||
358 | * @throws NotSupportedMultiUnitField |
||
359 | * |
||
360 | * @return mixed |
||
361 | */ |
||
362 | public function mutateAttribute($key, $value) |
||
372 | } |
||
373 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.