Complex classes like HasBinaryUuid 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 HasBinaryUuid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait HasBinaryUuid |
||
| 10 | { |
||
| 11 | protected static function bootHasBinaryUuid() |
||
| 12 | { |
||
| 13 | static::creating(function (Model $model) { |
||
| 14 | $uuidAttributes = $model->getUuidAttributes(); |
||
| 15 | |||
| 16 | foreach ($uuidAttributes as $key) { |
||
| 17 | if ($model->{$key}) { |
||
| 18 | continue; |
||
| 19 | } |
||
| 20 | |||
| 21 | $model->{$key} = static::encodeUuid(static::generateUuid()); |
||
| 22 | } |
||
| 23 | }); |
||
| 24 | } |
||
| 25 | |||
| 26 | public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder |
||
| 42 | |||
| 43 | public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder |
||
| 55 | |||
| 56 | public static function generateUuid() : string |
||
| 60 | |||
| 61 | public static function encodeUuid($uuid): string |
||
| 73 | |||
| 74 | public static function decodeUuid(string $binaryUuid): string |
||
| 82 | |||
| 83 | public function isBinary($uuidStr) |
||
| 87 | |||
| 88 | public function getUuidKeys() |
||
| 92 | |||
| 93 | public function toArray() |
||
| 130 | |||
| 131 | public function getRelatedBinaryKeyName($attribute): string |
||
| 137 | |||
| 138 | public function getAttribute($key) |
||
| 148 | |||
| 149 | public function setAttribute($key, $value) |
||
| 157 | |||
| 158 | protected function getUuidSuffix() |
||
| 162 | |||
| 163 | protected function uuidTextAttribute($key) |
||
| 175 | |||
| 176 | public function getUuidAttributes() |
||
| 187 | |||
| 188 | public function getUuidTextAttribute(): ?string |
||
| 198 | |||
| 199 | public function setUuidTextAttribute(string $uuid) |
||
| 209 | |||
| 210 | public function getQueueableId() |
||
| 214 | |||
| 215 | public function newQueryForRestoration($id) |
||
| 219 | |||
| 220 | public function newEloquentBuilder($query) |
||
| 224 | |||
| 225 | public function getRouteKeyName() |
||
| 233 | |||
| 234 | public function resolveRouteBinding($value) |
||
| 235 | { |
||
| 236 | $keyName = $this->getRouteKeyName(); |
||
| 237 | |||
| 238 | if (is_array($keyName)) { |
||
| 239 | $value = explode(':', strval($value), count($keyName)); |
||
| 240 | return $this->where(array_combine($keyName, $value))->frist(); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $this->where($keyName, $value)->first(); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function getKeyName() |
||
| 247 | { |
||
| 248 | return (! property_exists($this, 'primaryKey') || $this->primaryKey === 'id') ? 'uuid' : $this->primaryKey; |
||
| 249 | } |
||
| 250 | |||
| 251 | public function getIncrementing() |
||
| 252 | { |
||
| 253 | return false; |
||
| 254 | } |
||
| 255 | |||
| 256 | public function resolveRouteBinding($value) |
||
| 260 | |||
| 261 | public function strUuidSuffix($str) |
||
| 262 | { |
||
| 267 | |||
| 268 | private function decodeIdArray($ids) |
||
| 276 | } |
||
| 277 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: