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() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param $value |
||
| 86 | * @param AbstractUnit $unit |
||
| 87 | * @param string[] $requiredUnits |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
| 104 | |||
| 105 | public function getMultiUnitExistingConversionData($field) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getUnitAttributePostfix() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | protected function getUnitConversionDataPostfix() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | protected function getMultiUnitColumns() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param $field |
||
| 136 | * |
||
| 137 | * @throws NotSupportedMultiUnitField |
||
| 138 | * |
||
| 139 | * @return AbstractUnit[] |
||
| 140 | */ |
||
| 141 | public function getMultiUnitFieldSupportedUnits($field) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param $field |
||
| 152 | * |
||
| 153 | * @throws NotSupportedMultiUnitField |
||
| 154 | * |
||
| 155 | * @return AbstractUnit |
||
| 156 | */ |
||
| 157 | public function getMultiUnitFieldDefaultUnit($field) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param $field |
||
| 170 | * @param AbstractUnit|null $unit |
||
| 171 | * |
||
| 172 | * @throws NotSupportedMultiUnitField |
||
| 173 | * |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 196 | |||
| 197 | protected function isMultiUnitField($field) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param $field |
||
| 204 | * |
||
| 205 | * @throws NotSupportedMultiUnitField |
||
| 206 | * |
||
| 207 | * @return AbstractUnit |
||
| 208 | */ |
||
| 209 | protected function getMultiUnitFieldUnit($field) |
||
| 225 | |||
| 226 | protected function forgetMultiUnitFieldUnitInput($field) |
||
| 233 | |||
| 234 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param $field |
||
| 242 | * |
||
| 243 | * @throws NotSupportedMultiUnitField |
||
| 244 | */ |
||
| 245 | protected function resetMultiUnitFieldUnit($field) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determine if a set mutator exists for an attribute. |
||
| 252 | * |
||
| 253 | * @param string $key |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function hasSetMutator($key) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set the value of an attribute using its mutator. |
||
| 268 | * |
||
| 269 | * @param string $key |
||
| 270 | * @param mixed $value |
||
| 271 | * |
||
| 272 | * @throws NotSupportedMultiUnitField |
||
| 273 | * |
||
| 274 | * @return mixed |
||
| 275 | */ |
||
| 276 | protected function setMutatedAttributeValue($key, $value) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Detect changes and set proper base value. |
||
| 290 | * |
||
| 291 | * @param $field |
||
| 292 | * @param $value |
||
| 293 | * |
||
| 294 | * @throws NotSupportedMultiUnitField |
||
| 295 | * |
||
| 296 | * @return mixed |
||
| 297 | */ |
||
| 298 | private function processMultiUnitFieldChanges($field, $value) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Determine if a get mutator exists for an attribute. |
||
| 325 | *coo. |
||
| 326 | * |
||
| 327 | * @param string $key |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function hasGetMutator($key) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the value of an attribute using its mutator. |
||
| 342 | * |
||
| 343 | * @param string $key |
||
| 344 | * @param mixed $value |
||
| 345 | * |
||
| 346 | * @throws NotSupportedMultiUnitField |
||
| 347 | * |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | public function mutateAttribute($key, $value) |
||
| 360 | } |
||
| 361 |
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
Idableprovides a methodequalsIdthat 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.