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() |
||
| 59 | { |
||
| 60 | //save conversion table if base value is changed |
||
| 61 | static::creating(function ($model) { |
||
| 62 | /** |
||
| 63 | * @var Model|MultiUnitSupport $model |
||
| 64 | */ |
||
| 65 | foreach ($model->getMultiUnitColumns() as $unitBasedColumn => $options) { |
||
| 66 | if (isset($model->attributes[$unitBasedColumn])) { |
||
| 67 | $model->{$unitBasedColumn.$model->getUnitConversionDataPostfix()} = json_encode( |
||
| 68 | $model->calculateMultiUnitConversionData( |
||
| 69 | $model->attributes[$unitBasedColumn], |
||
| 70 | $model->getMultiUnitFieldUnit($unitBasedColumn), |
||
| 71 | $options['supported_units'] |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | $model->{$unitBasedColumn} = $model->processMultiUnitFieldChanges( |
||
| 75 | $unitBasedColumn, |
||
| 76 | $model->{$unitBasedColumn} |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | //prevent saving of unit columns |
||
| 81 | foreach ($model->getUnitConversionUnitColumns() as $unitColumn) { |
||
| 82 | if (isset($model->attributes[$unitColumn])) { |
||
| 83 | unset($model->attributes[$unitColumn]); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | }); |
||
| 87 | static::updating(function ($model) { |
||
| 88 | /** |
||
| 89 | * @var Model|MultiUnitSupport $model |
||
| 90 | */ |
||
| 91 | foreach (Arr::only($model->getMultiUnitColumns(), array_keys($model->getDirty())) as $unitBasedColumn => $options) { |
||
|
|
|||
| 92 | $model->{$unitBasedColumn.$model->getUnitConversionDataPostfix()} = json_encode($model->calculateMultiUnitConversionData($model->getDirty()[$unitBasedColumn], new $options['default_unit'](), $options['supported_units'])); |
||
| 93 | } |
||
| 94 | }); |
||
| 95 | } |
||
| 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) |
||
| 105 | { |
||
| 106 | $conversionData = []; |
||
| 107 | foreach ($requiredUnits as $requiredUnitClass) { |
||
| 108 | /** |
||
| 109 | * @var AbstractUnit $requiredUnit |
||
| 110 | */ |
||
| 111 | $requiredUnit = new $requiredUnitClass(); |
||
| 112 | $conversionData[$requiredUnit->getSymbol()] = (new $unit($value))->as($requiredUnit); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $conversionData; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function getMultiUnitExistingConversionData($field) |
||
| 119 | { |
||
| 120 | return json_decode($this->{$field.$this->getUnitConversionDataPostfix()} ?? null); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getUnitAttributePostfix() |
||
| 127 | { |
||
| 128 | return $this->unitAttributePostfix; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | protected function getUnitConversionDataPostfix() |
||
| 135 | { |
||
| 136 | return $this->unitConversionDataPostfix; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | protected function getMultiUnitColumns() |
||
| 143 | { |
||
| 144 | return $this->multiUnitColumns; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param $field |
||
| 149 | * |
||
| 150 | * @throws NotSupportedMultiUnitField |
||
| 151 | * |
||
| 152 | * @return AbstractUnit[] |
||
| 153 | */ |
||
| 154 | public function getMultiUnitFieldSupportedUnits($field) |
||
| 155 | { |
||
| 156 | if ($this->isMultiUnitField($field)) { |
||
| 157 | return $this->getMultiUnitColumns()[$field]['supported_units']; |
||
| 158 | } |
||
| 159 | |||
| 160 | throw new NotSupportedMultiUnitField($field); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param $field |
||
| 165 | * |
||
| 166 | * @throws NotSupportedMultiUnitField |
||
| 167 | * |
||
| 168 | * @return AbstractUnit |
||
| 169 | */ |
||
| 170 | public function getMultiUnitFieldDefaultUnit($field) |
||
| 171 | { |
||
| 172 | if ($this->isMultiUnitField($field)) { |
||
| 173 | $unitClass = $this->getMultiUnitColumns()[$field]['default_unit']; |
||
| 174 | |||
| 175 | return new $unitClass(); |
||
| 176 | } |
||
| 177 | |||
| 178 | throw new NotSupportedMultiUnitField($field); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $field |
||
| 183 | * @param AbstractUnit|null $unit |
||
| 184 | * |
||
| 185 | * @throws NotSupportedMultiUnitField |
||
| 186 | * |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
||
| 190 | { |
||
| 191 | if ($this->isMultiUnitField($field)) { |
||
| 192 | if (isset($this->{$field})) { |
||
| 193 | if (is_null($unit)) { |
||
| 194 | $unit = $this->getMultiUnitFieldUnit($field); |
||
| 195 | } |
||
| 196 | $existingConversionData = $this->getMultiUnitExistingConversionData($field); |
||
| 197 | if (!is_null($existingConversionData) && !is_null($existingConversionData->{$unit->getSymbol()})) { |
||
| 198 | return $existingConversionData->{$unit->getSymbol()}; |
||
| 199 | } |
||
| 200 | return ($this->getMultiUnitFieldDefaultUnit($field)->setValue($this->{$field} ?? $this->attributes[$field]))->as(new $unit()); |
||
| 201 | } else { |
||
| 202 | return; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | throw new NotSupportedMultiUnitField($field); |
||
| 207 | } |
||
| 208 | |||
| 209 | protected function isMultiUnitField($field) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param $field |
||
| 216 | * |
||
| 217 | * @throws NotSupportedMultiUnitField |
||
| 218 | * |
||
| 219 | * @return AbstractUnit |
||
| 220 | */ |
||
| 221 | protected function getMultiUnitFieldUnit($field) |
||
| 237 | |||
| 238 | protected function forgetMultiUnitFieldUnitInput($field) |
||
| 245 | |||
| 246 | protected function setMultiUnitFieldUnit($field, AbstractUnit $unit) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $field |
||
| 254 | * |
||
| 255 | * @throws NotSupportedMultiUnitField |
||
| 256 | */ |
||
| 257 | protected function resetMultiUnitFieldUnit($field) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Determine if a set mutator exists for an attribute. |
||
| 264 | * |
||
| 265 | * @param string $key |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function hasSetMutator($key) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set the value of an attribute using its mutator. |
||
| 280 | * |
||
| 281 | * @param string $key |
||
| 282 | * @param mixed $value |
||
| 283 | * |
||
| 284 | * @throws NotSupportedMultiUnitField |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | protected function setMutatedAttributeValue($key, $value) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Detect changes and set proper base value. |
||
| 302 | * |
||
| 303 | * @param $field |
||
| 304 | * @param $value |
||
| 305 | * |
||
| 306 | * @throws NotSupportedMultiUnitField |
||
| 307 | * |
||
| 308 | * @return mixed |
||
| 309 | */ |
||
| 310 | private function processMultiUnitFieldChanges($field, $value) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Determine if a get mutator exists for an attribute. |
||
| 337 | *coo. |
||
| 338 | * |
||
| 339 | * @param string $key |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function hasGetMutator($key) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the value of an attribute using its mutator. |
||
| 354 | * |
||
| 355 | * @param string $key |
||
| 356 | * @param mixed $value |
||
| 357 | * |
||
| 358 | * @throws NotSupportedMultiUnitField |
||
| 359 | * |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public function mutateAttribute($key, $value) |
||
| 372 | } |
||
| 373 |
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.