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 | use ModelConfiguration; |
||
| 14 | |||
| 15 | protected $unitConversionDataPostfix = '_ucd'; |
||
| 16 | protected $multiUnitColumns = []; |
||
| 17 | protected $multiUnitSelectedUnits = []; |
||
| 18 | |||
| 19 | private function getUnitConversionDataColumns() |
||
|
|
|||
| 20 | { |
||
| 21 | return array_map(function ($column) { |
||
| 22 | return $column.$this->getUnitConversionDataPostfix(); |
||
| 23 | }, array_keys($this->getMultiUnitColumns())); |
||
| 24 | } |
||
| 25 | |||
| 26 | protected static function bootMultiUnitSupport() |
||
| 27 | { |
||
| 28 | //save conversion table if base value is changed |
||
| 29 | static::creating(function ($model) { |
||
| 30 | /** |
||
| 31 | * @var Model|MultiUnitSupport $model |
||
| 32 | */ |
||
| 33 | foreach ($model->getMultiUnitColumns() as $unitBasedColumn => $options) { |
||
| 34 | if (isset($model->attributes[$unitBasedColumn])) { |
||
| 35 | $model->{$unitBasedColumn.$model->getUnitConversionDataPostfix()} = json_encode( |
||
| 36 | $model->calculateMultiUnitConversionData( |
||
| 37 | $model->attributes[$unitBasedColumn], |
||
| 38 | $model->getMultiUnitFieldUnit($unitBasedColumn), |
||
| 39 | $options['supported_units'] |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | $model->{$unitBasedColumn} = $model->processMultiUnitFieldChanges( |
||
| 43 | $unitBasedColumn, |
||
| 44 | $model->{$unitBasedColumn} |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | }); |
||
| 49 | static::updating(function ($model) { |
||
| 50 | /** |
||
| 51 | * @var Model|MultiUnitSupport $model |
||
| 52 | */ |
||
| 53 | foreach (Arr::only($model->getMultiUnitColumns(), array_keys($model->getDirty())) as $unitBasedColumn => $options) { |
||
| 54 | $model->{$unitBasedColumn.$model->getUnitConversionDataPostfix()} = json_encode( |
||
| 55 | $model->calculateMultiUnitConversionData( |
||
| 56 | $model->getDirty()[$unitBasedColumn], |
||
| 57 | $model->getMultiUnitFieldUnit($unitBasedColumn, true), |
||
| 58 | $options['supported_units'] |
||
| 59 | ) |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | }); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param $value |
||
| 67 | * @param AbstractUnit $unit |
||
| 68 | * @param string[] $requiredUnits |
||
| 69 | * |
||
| 70 | * @return array|null |
||
| 71 | */ |
||
| 72 | private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
||
| 73 | { |
||
| 74 | if (is_null($value)) { |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $conversionData = []; |
||
| 79 | foreach ($requiredUnits as $requiredUnitClass) { |
||
| 80 | /** |
||
| 81 | * @var AbstractUnit $requiredUnit |
||
| 82 | */ |
||
| 83 | $requiredUnit = new $requiredUnitClass(); |
||
| 84 | $conversionData[$requiredUnit->getId()] = (new $unit($value))->as($requiredUnit); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $conversionData; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getMultiUnitExistingConversionData($field) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | protected function getUnitConversionDataPostfix() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function getMultiUnitColumns() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param $field |
||
| 113 | * |
||
| 114 | * @throws NotSupportedMultiUnitField |
||
| 115 | * |
||
| 116 | * @return AbstractUnit[] |
||
| 117 | */ |
||
| 118 | public function getMultiUnitFieldSupportedUnits($field) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param $field |
||
| 129 | * |
||
| 130 | * @throws NotSupportedMultiUnitField |
||
| 131 | * |
||
| 132 | * @return AbstractUnit |
||
| 133 | */ |
||
| 134 | public function getMultiUnitFieldDefaultUnit($field) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param $field |
||
| 147 | * |
||
| 148 | * @throws NotSupportedMultiUnitField |
||
| 149 | * |
||
| 150 | * @return AbstractUnit |
||
| 151 | */ |
||
| 152 | public function getMultiUnitFieldSelectedUnit($field) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param $field |
||
| 165 | * @param string $unit |
||
| 166 | * |
||
| 167 | * @throws NotSupportedMultiUnitField |
||
| 168 | * @throws NotSupportedMultiUnitFieldUnit |
||
| 169 | */ |
||
| 170 | public function setMultiUnitFieldSelectedUnit($field, $unit) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $field |
||
| 196 | * @param string $unit |
||
| 197 | * |
||
| 198 | * @throws NotSupportedMultiUnitField |
||
| 199 | * |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param $field |
||
| 239 | * @param AbstractUnit|null $unit |
||
| 240 | * |
||
| 241 | * @throws NotSupportedMultiUnitField |
||
| 242 | * |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 265 | |||
| 266 | protected function isMultiUnitField($field) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param $field |
||
| 273 | * |
||
| 274 | * @throws NotSupportedMultiUnitField |
||
| 275 | * |
||
| 276 | * @return AbstractUnit |
||
| 277 | */ |
||
| 278 | protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
||
| 282 | } |
||
| 283 |