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 getUuidKeys() |
||
| 84 | { |
||
| 85 | return (property_exists($this, 'uuids') && is_array($this->uuids)) ? $this->uuids : []; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function toArray() |
||
| 89 | { |
||
| 90 | $uuidAttributes = $this->getUuidAttributes(); |
||
| 91 | |||
| 92 | $array = parent::toArray(); |
||
| 93 | $pivotUuids = []; |
||
| 94 | |||
| 95 | if (! $this->exists || ! is_array($uuidAttributes)) { |
||
| 96 | return $array; |
||
| 97 | } |
||
| 98 | |||
| 99 | foreach ($uuidAttributes as $attributeKey) { |
||
| 100 | if (! array_key_exists($attributeKey, $array)) { |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | $uuidKey = $this->getRelatedBinaryKeyName($attributeKey); |
||
| 104 | $array[$attributeKey] = $this->{$uuidKey}; |
||
| 105 | } |
||
| 106 | |||
| 107 | if (isset($array['pivot'])) { |
||
| 108 | $pivotUuids = $array['pivot']; |
||
| 109 | |||
| 110 | if (! is_array($pivotUuids)) { |
||
| 111 | $pivotUuids = get_object_vars($pivotUuids); |
||
| 112 | } |
||
| 113 | |||
| 114 | foreach ($pivotUuids as $key => $uuid) { |
||
| 115 | $pivotUuids[$key] = $this->decodeUuid($uuid); |
||
| 116 | } |
||
| 117 | |||
| 118 | $array['pivot'] = $pivotUuids; |
||
| 119 | } |
||
| 120 | |||
| 121 | return $array; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function getRelatedBinaryKeyName($attribute): string |
||
| 125 | { |
||
| 126 | $suffix = $this->getUuidSuffix(); |
||
| 127 | |||
| 128 | return preg_match('/(?:[a-zA-Z_]+)?id/i', $attribute) ? "{$attribute}{$suffix}" : $attribute; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function getAttribute($key) |
||
| 132 | { |
||
| 133 | $uuidKey = $this->uuidTextAttribute($key); |
||
| 134 | |||
| 135 | if ($uuidKey && $this->{$uuidKey} !== null) { |
||
| 136 | return static::decodeUuid($this->{$uuidKey}); |
||
| 137 | } |
||
| 138 | |||
| 139 | return parent::getAttribute($key); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function setAttribute($key, $value) |
||
| 143 | { |
||
| 144 | if ($this->uuidTextAttribute($key)) { |
||
| 145 | $value = static::encodeUuid($value); |
||
| 146 | } |
||
| 147 | |||
| 148 | return parent::setAttribute($key, $value); |
||
| 149 | } |
||
| 150 | |||
| 151 | protected function getUuidSuffix() |
||
| 152 | { |
||
| 153 | return (property_exists($this, 'uuidSuffix')) ? $this->uuidSuffix : '_text'; |
||
| 154 | } |
||
| 155 | |||
| 156 | protected function uuidTextAttribute($key) |
||
| 157 | { |
||
| 158 | $uuidAttributes = $this->getUuidAttributes(); |
||
| 159 | $suffix = $this->getUuidSuffix(); |
||
| 160 | $offset = -(strlen($suffix)); |
||
| 161 | |||
| 162 | if (substr($key, $offset) == $suffix && in_array(($uuidKey = substr($key, 0, $offset)), $uuidAttributes)) { |
||
| 163 | return $uuidKey; |
||
| 164 | } |
||
| 165 | |||
| 166 | return false; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getUuidAttributes() |
||
| 170 | { |
||
| 171 | $uuidKeys = $this->getUuidKeys(); |
||
| 172 | |||
| 173 | // non composite primary keys will return a string so casting required |
||
| 174 | $key = (array) $this->getKeyName(); |
||
| 175 | |||
| 176 | $uuidAttributes = array_unique(array_merge($uuidKeys, $key)); |
||
| 177 | |||
| 178 | return $uuidAttributes; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function getUuidTextAttribute(): ?string |
||
| 182 | { |
||
| 183 | $key = $this->getKeyName(); |
||
| 184 | |||
| 185 | if (! $this->exists || is_array($key)) { |
||
| 186 | return null; |
||
| 187 | } |
||
| 188 | |||
| 189 | return static::decodeUuid($this->{$key}); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function setUuidTextAttribute(string $uuid) |
||
| 193 | { |
||
| 194 | $key = $this->getKeyName(); |
||
| 195 | |||
| 196 | if (! is_string($key)) { |
||
| 197 | throw new Exception('composite keys not allowed for attribute mutation'); |
||
| 198 | } |
||
| 199 | |||
| 200 | $this->{$key} = static::encodeUuid($uuid); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getQueueableId() |
||
| 204 | { |
||
| 205 | return base64_encode($this->{$this->getKeyName()}); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function newQueryForRestoration($id) |
||
| 209 | { |
||
| 210 | return $this->newQueryWithoutScopes()->whereKey(base64_decode($id)); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function newEloquentBuilder($query) |
||
| 214 | { |
||
| 215 | return new Builder($query); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getRouteKeyName() |
||
| 219 | { |
||
| 220 | $keyName = $this->getKeyName(); |
||
| 221 | |||
| 222 | $routeKeyName = is_string($keyName) ? $this->strUuidSuffix($keyName) : array_map([&$this, 'strUuidSuffix'], $keyName); |
||
| 223 | |||
| 224 | return $routeKeyName; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function getKeyName() |
||
| 228 | { |
||
| 229 | return (! property_exists($this, 'primaryKey') || $this->primaryKey === 'id') ? 'uuid' : $this->primaryKey; |
||
| 230 | } |
||
| 231 | |||
| 232 | public function getQualifiedKeyName() |
||
| 233 | { |
||
| 234 | return $this->qualifyColumn($this->getKeyName()); |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getIncrementing() |
||
| 241 | |||
| 242 | public function resolveRouteBinding($value) |
||
| 243 | { |
||
| 244 | $keyName = $this->getKeyName(); |
||
| 245 | |||
| 246 | if (is_array($keyName)) { |
||
| 247 | $parsed = explode(':', strval($value), count($keyName)); |
||
| 248 | |||
| 249 | $value = array_combine($keyName, $parsed); |
||
| 250 | } |
||
| 251 | |||
| 252 | return $this->withUuid($value)->first(); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function strUuidSuffix($str) |
||
| 256 | { |
||
| 257 | $suffix = $this->getUuidSuffix(); |
||
| 258 | |||
| 259 | return "{$str}{$suffix}"; |
||
| 261 | } |
||
| 262 |
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: