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(){ |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $value |
||
| 112 | * @param AbstractUnit $unit |
||
| 113 | * @param string[] $requiredUnits |
||
| 114 | * |
||
| 115 | * @return array|null |
||
| 116 | */ |
||
| 117 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
| 133 | |||
| 134 | public function getMultiUnitExistingConversionData($field) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getUnitAttributePostfix() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | protected function getUnitConversionDataPostfix() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function getMultiUnitColumns() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param $field |
||
| 165 | * |
||
| 166 | * @throws NotSupportedMultiUnitField |
||
| 167 | * |
||
| 168 | * @return AbstractUnit[] |
||
| 169 | */ |
||
| 170 | public function getMultiUnitFieldSupportedUnits($field) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param $field |
||
| 181 | * |
||
| 182 | * @throws NotSupportedMultiUnitField |
||
| 183 | * |
||
| 184 | * @return AbstractUnit |
||
| 185 | */ |
||
| 186 | public function getMultiUnitFieldDefaultUnit($field) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param $field |
||
| 199 | * |
||
| 200 | * @throws NotSupportedMultiUnitField |
||
| 201 | * |
||
| 202 | * @return AbstractUnit |
||
| 203 | */ |
||
| 204 | public function getMultiUnitFieldSelectedUnit($field) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param $field |
||
| 217 | * @param string $unit |
||
| 218 | * |
||
| 219 | * @throws NotSupportedMultiUnitField |
||
| 220 | * @throws NotSupportedMultiUnitFieldUnit |
||
| 221 | */ |
||
| 222 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param $field |
||
| 248 | * @param string $unit |
||
| 249 | * |
||
| 250 | * @throws NotSupportedMultiUnitField |
||
| 251 | * |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $field |
||
| 291 | * @param AbstractUnit|null $unit |
||
| 292 | * |
||
| 293 | * @throws NotSupportedMultiUnitField |
||
| 294 | * |
||
| 295 | * @return mixed |
||
| 296 | */ |
||
| 297 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 317 | |||
| 318 | protected function isMultiUnitField($field) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param $field |
||
| 325 | * |
||
| 326 | * @throws NotSupportedMultiUnitField |
||
| 327 | * |
||
| 328 | * @return AbstractUnit |
||
| 329 | */ |
||
| 330 | protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
||
| 346 | |||
| 347 | protected function forgetMultiUnitFieldUnitInput($field) |
||
| 354 | |||
| 355 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $field |
||
| 365 | * |
||
| 366 | * @throws NotSupportedMultiUnitField |
||
| 367 | */ |
||
| 368 | protected function resetMultiUnitFieldUnit($field) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Determine if a set mutator exists for an attribute. |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function hasSetMutator($key) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set the value of an attribute using its mutator. |
||
| 391 | * |
||
| 392 | * @param string $key |
||
| 393 | * @param mixed $value |
||
| 394 | * |
||
| 395 | * @throws NotSupportedMultiUnitField |
||
| 396 | * |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | protected function setMutatedAttributeValue($key, $value) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Detect changes and set proper base value. |
||
| 417 | * |
||
| 418 | * @param $field |
||
| 419 | * @param $value |
||
| 420 | * |
||
| 421 | * @throws NotSupportedMultiUnitField |
||
| 422 | * |
||
| 423 | * @return mixed |
||
| 424 | */ |
||
| 425 | private function processMultiUnitFieldChanges($field, $value) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Determine if a get mutator exists for an attribute. |
||
| 456 | *coo. |
||
| 457 | * |
||
| 458 | * @param string $key |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | public function hasGetMutator($key) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the value of an attribute using its mutator. |
||
| 473 | * |
||
| 474 | * @param string $key |
||
| 475 | * @param mixed $value |
||
| 476 | * |
||
| 477 | * @throws NotSupportedMultiUnitField |
||
| 478 | * |
||
| 479 | * @return mixed |
||
| 480 | */ |
||
| 481 | public function mutateAttribute($key, $value) |
||
| 496 | } |
||
| 497 |
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: