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() |
||
| 100 | |||
| 101 | private function forgetUnitsInput() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param $value |
||
| 113 | * @param AbstractUnit $unit |
||
| 114 | * @param string[] $requiredUnits |
||
| 115 | * |
||
| 116 | * @return array|null |
||
| 117 | */ |
||
| 118 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
| 135 | |||
| 136 | public function getMultiUnitExistingConversionData($field) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getUnitAttributePostfix() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | protected function getUnitConversionDataPostfix() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function getMultiUnitColumns() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $field |
||
| 167 | * |
||
| 168 | * @throws NotSupportedMultiUnitField |
||
| 169 | * |
||
| 170 | * @return AbstractUnit[] |
||
| 171 | */ |
||
| 172 | public function getMultiUnitFieldSupportedUnits($field) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $field |
||
| 183 | * |
||
| 184 | * @throws NotSupportedMultiUnitField |
||
| 185 | * |
||
| 186 | * @return AbstractUnit |
||
| 187 | */ |
||
| 188 | public function getMultiUnitFieldDefaultUnit($field) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param $field |
||
| 201 | * |
||
| 202 | * @throws NotSupportedMultiUnitField |
||
| 203 | * |
||
| 204 | * @return AbstractUnit |
||
| 205 | */ |
||
| 206 | public function getMultiUnitFieldSelectedUnit($field) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param $field |
||
| 219 | * @param string $unit |
||
| 220 | * |
||
| 221 | * @throws NotSupportedMultiUnitField |
||
| 222 | * @throws NotSupportedMultiUnitFieldUnit |
||
| 223 | */ |
||
| 224 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param $field |
||
| 250 | * @param string $unit |
||
| 251 | * |
||
| 252 | * @throws NotSupportedMultiUnitField |
||
| 253 | * |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $field |
||
| 293 | * @param AbstractUnit|null $unit |
||
| 294 | * |
||
| 295 | * @throws NotSupportedMultiUnitField |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 319 | |||
| 320 | protected function isMultiUnitField($field) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param $field |
||
| 327 | * |
||
| 328 | * @throws NotSupportedMultiUnitField |
||
| 329 | * |
||
| 330 | * @return AbstractUnit |
||
| 331 | */ |
||
| 332 | protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
||
| 348 | |||
| 349 | protected function forgetMultiUnitFieldUnitInput($field) |
||
| 356 | |||
| 357 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param $field |
||
| 367 | * |
||
| 368 | * @throws NotSupportedMultiUnitField |
||
| 369 | */ |
||
| 370 | protected function resetMultiUnitFieldUnit($field) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Determine if a set mutator exists for an attribute. |
||
| 377 | * |
||
| 378 | * @param string $key |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public function hasSetMutator($key) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the value of an attribute using its mutator. |
||
| 393 | * |
||
| 394 | * @param string $key |
||
| 395 | * @param mixed $value |
||
| 396 | * |
||
| 397 | * @throws NotSupportedMultiUnitField |
||
| 398 | * |
||
| 399 | * @return mixed |
||
| 400 | */ |
||
| 401 | protected function setMutatedAttributeValue($key, $value) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Detect changes and set proper base value. |
||
| 419 | * |
||
| 420 | * @param $field |
||
| 421 | * @param $value |
||
| 422 | * |
||
| 423 | * @throws NotSupportedMultiUnitField |
||
| 424 | * |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | private function processMultiUnitFieldChanges($field, $value) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Determine if a get mutator exists for an attribute. |
||
| 458 | *coo. |
||
| 459 | * |
||
| 460 | * @param string $key |
||
| 461 | * |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | public function hasGetMutator($key) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get the value of an attribute using its mutator. |
||
| 475 | * |
||
| 476 | * @param string $key |
||
| 477 | * @param mixed $value |
||
| 478 | * |
||
| 479 | * @throws NotSupportedMultiUnitField |
||
| 480 | * |
||
| 481 | * @return mixed |
||
| 482 | */ |
||
| 483 | public function mutateAttribute($key, $value) |
||
| 498 | } |
||
| 499 |
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: