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 | protected $unitAttributePostfix = '_units'; |
||
| 14 | protected $unitConversionDataPostfix = '_ucd'; |
||
| 15 | protected $multiUnitColumns = []; |
||
| 16 | protected $multiUnitSelectedUnits = []; |
||
| 17 | |||
| 18 | private function getUnitConversionDataColumns() |
||
| 24 | |||
| 25 | private function getUnitConversionUnitColumns() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Allows to set input units and process them before multi-unit field. |
||
| 34 | * |
||
| 35 | * @param array $attributes |
||
| 36 | * |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | protected function fillableFromArray(array $attributes) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function getFillable() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return mixed |
||
| 54 | */ |
||
| 55 | public function getHidden() |
||
| 59 | |||
| 60 | protected static function bootMultiUnitSupport() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param $value |
||
| 101 | * @param AbstractUnit $unit |
||
| 102 | * @param string[] $requiredUnits |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
| 119 | |||
| 120 | public function getMultiUnitExistingConversionData($field) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getUnitAttributePostfix() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | protected function getUnitConversionDataPostfix() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getMultiUnitColumns() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $field |
||
| 151 | * |
||
| 152 | * @throws NotSupportedMultiUnitField |
||
| 153 | * |
||
| 154 | * @return AbstractUnit[] |
||
| 155 | */ |
||
| 156 | public function getMultiUnitFieldSupportedUnits($field) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $field |
||
| 167 | * |
||
| 168 | * @throws NotSupportedMultiUnitField |
||
| 169 | * |
||
| 170 | * @return AbstractUnit |
||
| 171 | */ |
||
| 172 | public function getMultiUnitFieldDefaultUnit($field) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param $field |
||
| 185 | * |
||
| 186 | * @throws NotSupportedMultiUnitField |
||
| 187 | * |
||
| 188 | * @return AbstractUnit |
||
| 189 | */ |
||
| 190 | public function getMultiUnitFieldSelectedUnit($field) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param $field |
||
| 203 | * @param string $unit |
||
| 204 | * |
||
| 205 | * @throws NotSupportedMultiUnitField |
||
| 206 | * @throws NotSupportedMultiUnitFieldUnit |
||
| 207 | */ |
||
| 208 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @param $field |
||
| 234 | * @param string $unit |
||
| 235 | * |
||
| 236 | * @throws NotSupportedMultiUnitField |
||
| 237 | * |
||
| 238 | * @return mixed |
||
| 239 | */ |
||
| 240 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $field |
||
| 277 | * @param AbstractUnit|null $unit |
||
| 278 | * |
||
| 279 | * @throws NotSupportedMultiUnitField |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 303 | |||
| 304 | protected function isMultiUnitField($field) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param $field |
||
| 311 | * |
||
| 312 | * @throws NotSupportedMultiUnitField |
||
| 313 | * |
||
| 314 | * @return AbstractUnit |
||
| 315 | */ |
||
| 316 | protected function getMultiUnitFieldUnit($field) |
||
| 332 | |||
| 333 | protected function forgetMultiUnitFieldUnitInput($field) |
||
| 340 | |||
| 341 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param $field |
||
| 349 | * |
||
| 350 | * @throws NotSupportedMultiUnitField |
||
| 351 | */ |
||
| 352 | protected function resetMultiUnitFieldUnit($field) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Determine if a set mutator exists for an attribute. |
||
| 359 | * |
||
| 360 | * @param string $key |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function hasSetMutator($key) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the value of an attribute using its mutator. |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * @param mixed $value |
||
| 378 | * |
||
| 379 | * @throws NotSupportedMultiUnitField |
||
| 380 | * |
||
| 381 | * @return mixed |
||
| 382 | */ |
||
| 383 | protected function setMutatedAttributeValue($key, $value) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Detect changes and set proper base value. |
||
| 401 | * |
||
| 402 | * @param $field |
||
| 403 | * @param $value |
||
| 404 | * |
||
| 405 | * @throws NotSupportedMultiUnitField |
||
| 406 | * |
||
| 407 | * @return mixed |
||
| 408 | */ |
||
| 409 | private function processMultiUnitFieldChanges($field, $value) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Determine if a get mutator exists for an attribute. |
||
| 436 | *coo. |
||
| 437 | * |
||
| 438 | * @param string $key |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function hasGetMutator($key) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get the value of an attribute using its mutator. |
||
| 453 | * |
||
| 454 | * @param string $key |
||
| 455 | * @param mixed $value |
||
| 456 | * |
||
| 457 | * @throws NotSupportedMultiUnitField |
||
| 458 | * |
||
| 459 | * @return mixed |
||
| 460 | */ |
||
| 461 | public function mutateAttribute($key, $value) |
||
| 476 | } |
||
| 477 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: