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() |
||
96 | |||
97 | /** |
||
98 | * @param $value |
||
99 | * @param AbstractUnit $unit |
||
100 | * @param string[] $requiredUnits |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
117 | |||
118 | public function getMultiUnitExistingConversionData($field) |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getUnitAttributePostfix() |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | protected function getUnitConversionDataPostfix() |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function getMultiUnitColumns() |
||
146 | |||
147 | /** |
||
148 | * @param $field |
||
149 | * |
||
150 | * @throws NotSupportedMultiUnitField |
||
151 | * |
||
152 | * @return AbstractUnit[] |
||
153 | */ |
||
154 | public function getMultiUnitFieldSupportedUnits($field) |
||
162 | |||
163 | /** |
||
164 | * @param $field |
||
165 | * |
||
166 | * @throws NotSupportedMultiUnitField |
||
167 | * |
||
168 | * @return AbstractUnit |
||
169 | */ |
||
170 | public function getMultiUnitFieldDefaultUnit($field) |
||
180 | |||
181 | /** |
||
182 | * @param $field |
||
183 | * @param AbstractUnit|null $unit |
||
184 | * |
||
185 | * @throws NotSupportedMultiUnitField |
||
186 | * |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
209 | |||
210 | protected function isMultiUnitField($field) |
||
214 | |||
215 | /** |
||
216 | * @param $field |
||
217 | * |
||
218 | * @throws NotSupportedMultiUnitField |
||
219 | * |
||
220 | * @return AbstractUnit |
||
221 | */ |
||
222 | protected function getMultiUnitFieldUnit($field) |
||
238 | |||
239 | protected function forgetMultiUnitFieldUnitInput($field) |
||
246 | |||
247 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
252 | |||
253 | /** |
||
254 | * @param $field |
||
255 | * |
||
256 | * @throws NotSupportedMultiUnitField |
||
257 | */ |
||
258 | protected function resetMultiUnitFieldUnit($field) |
||
262 | |||
263 | /** |
||
264 | * Determine if a set mutator exists for an attribute. |
||
265 | * |
||
266 | * @param string $key |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function hasSetMutator($key) |
||
278 | |||
279 | /** |
||
280 | * Set the value of an attribute using its mutator. |
||
281 | * |
||
282 | * @param string $key |
||
283 | * @param mixed $value |
||
284 | * |
||
285 | * @throws NotSupportedMultiUnitField |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | protected function setMutatedAttributeValue($key, $value) |
||
300 | |||
301 | /** |
||
302 | * Detect changes and set proper base value. |
||
303 | * |
||
304 | * @param $field |
||
305 | * @param $value |
||
306 | * |
||
307 | * @throws NotSupportedMultiUnitField |
||
308 | * |
||
309 | * @return mixed |
||
310 | */ |
||
311 | private function processMultiUnitFieldChanges($field, $value) |
||
335 | |||
336 | /** |
||
337 | * Determine if a get mutator exists for an attribute. |
||
338 | *coo. |
||
339 | * |
||
340 | * @param string $key |
||
341 | * |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function hasGetMutator($key) |
||
352 | |||
353 | /** |
||
354 | * Get the value of an attribute using its mutator. |
||
355 | * |
||
356 | * @param string $key |
||
357 | * @param mixed $value |
||
358 | * |
||
359 | * @throws NotSupportedMultiUnitField |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | public function mutateAttribute($key, $value) |
||
373 | } |
||
374 |
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.