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 |
||
| 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 | public 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 string $unit |
||
| 184 | * |
||
| 185 | * @throws NotSupportedMultiUnitField |
||
| 186 | * |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param $field |
||
| 226 | * @param AbstractUnit|null $unit |
||
| 227 | * |
||
| 228 | * @throws NotSupportedMultiUnitField |
||
| 229 | * |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 252 | |||
| 253 | protected function isMultiUnitField($field) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $field |
||
| 260 | * |
||
| 261 | * @throws NotSupportedMultiUnitField |
||
| 262 | * |
||
| 263 | * @return AbstractUnit |
||
| 264 | */ |
||
| 265 | protected function getMultiUnitFieldUnit($field) |
||
| 281 | |||
| 282 | protected function forgetMultiUnitFieldUnitInput($field) |
||
| 289 | |||
| 290 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param $field |
||
| 298 | * |
||
| 299 | * @throws NotSupportedMultiUnitField |
||
| 300 | */ |
||
| 301 | protected function resetMultiUnitFieldUnit($field) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Determine if a set mutator exists for an attribute. |
||
| 308 | * |
||
| 309 | * @param string $key |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function hasSetMutator($key) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set the value of an attribute using its mutator. |
||
| 324 | * |
||
| 325 | * @param string $key |
||
| 326 | * @param mixed $value |
||
| 327 | * |
||
| 328 | * @throws NotSupportedMultiUnitField |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | protected function setMutatedAttributeValue($key, $value) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Detect changes and set proper base value. |
||
| 346 | * |
||
| 347 | * @param $field |
||
| 348 | * @param $value |
||
| 349 | * |
||
| 350 | * @throws NotSupportedMultiUnitField |
||
| 351 | * |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | private function processMultiUnitFieldChanges($field, $value) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Determine if a get mutator exists for an attribute. |
||
| 381 | *coo. |
||
| 382 | * |
||
| 383 | * @param string $key |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function hasGetMutator($key) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the value of an attribute using its mutator. |
||
| 398 | * |
||
| 399 | * @param string $key |
||
| 400 | * @param mixed $value |
||
| 401 | * |
||
| 402 | * @throws NotSupportedMultiUnitField |
||
| 403 | * |
||
| 404 | * @return mixed |
||
| 405 | */ |
||
| 406 | public function mutateAttribute($key, $value) |
||
| 416 | } |
||
| 417 |
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.