Complex classes like MetadataTrait 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 MetadataTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait MetadataTrait |
||
| 21 | { |
||
| 22 | protected static $relationHooks = []; |
||
| 23 | protected static $relationCategories = []; |
||
| 24 | protected static $methodPrimary = []; |
||
| 25 | protected static $methodAlternate = []; |
||
| 26 | protected $loadEagerRelations = []; |
||
| 27 | |||
| 28 | 3 | /* |
|
| 29 | * Retrieve and assemble this model's metadata for OData packaging |
||
| 30 | 3 | */ |
|
| 31 | public function metadata() |
||
| 32 | { |
||
| 33 | 2 | assert($this instanceof Model, get_class($this)); |
|
| 34 | 2 | ||
| 35 | // Break these out separately to enable separate reuse |
||
| 36 | 2 | $connect = $this->getConnection(); |
|
| 37 | $builder = $connect->getSchemaBuilder(); |
||
| 38 | 2 | ||
| 39 | 1 | $table = $this->getTable(); |
|
| 40 | |||
| 41 | if (!$builder->hasTable($table)) { |
||
| 42 | 1 | return []; |
|
| 43 | 1 | } |
|
| 44 | 1 | ||
| 45 | $columns = $builder->getColumnListing($table); |
||
| 46 | 1 | $mask = $this->metadataMask(); |
|
| 47 | $columns = array_intersect($columns, $mask); |
||
| 48 | 1 | ||
| 49 | 1 | $tableData = []; |
|
| 50 | 1 | ||
| 51 | $rawFoo = $connect->getDoctrineSchemaManager()->listTableColumns($table); |
||
| 52 | 1 | $foo = []; |
|
| 53 | 1 | $getters = $this->collectGetters(); |
|
| 54 | 1 | ||
| 55 | foreach ($rawFoo as $key => $val) { |
||
| 56 | 1 | // Work around glitch in Doctrine when reading from MariaDB which added ` characters to root key value |
|
| 57 | $key = trim($key, '`'); |
||
| 58 | 1 | $foo[$key] = $val; |
|
| 59 | 1 | } |
|
| 60 | 1 | ||
| 61 | 1 | foreach ($columns as $column) { |
|
| 62 | 1 | // Doctrine schema manager returns columns with lowercased names |
|
| 63 | 1 | $rawColumn = $foo[strtolower($column)]; |
|
| 64 | 1 | $nullable = !($rawColumn->getNotNull()); |
|
| 65 | $fillable = in_array($column, $this->getFillable()); |
||
| 66 | 1 | $rawType = $rawColumn->getType(); |
|
| 67 | $type = $rawType->getName(); |
||
| 68 | $default = $this->$column; |
||
| 69 | $tableData[$column] = ['type' => $type, |
||
| 70 | 'nullable' => $nullable, |
||
| 71 | 'fillable' => $fillable, |
||
| 72 | 'default' => $default |
||
| 73 | 4 | ]; |
|
| 74 | } |
||
| 75 | 4 | ||
| 76 | foreach ($getters as $get) { |
||
| 77 | 4 | if (isset($tableData[$get])) { |
|
| 78 | 4 | continue; |
|
| 79 | 4 | } |
|
| 80 | 2 | $default = $this->$get; |
|
| 81 | 4 | $tableData[$get] = ['type' => 'text', 'nullable' => true, 'fillable' => false, 'default' => $default]; |
|
| 82 | 1 | } |
|
| 83 | 1 | ||
| 84 | return $tableData; |
||
| 85 | 4 | } |
|
| 86 | |||
| 87 | /* |
||
| 88 | * Return the set of fields that are permitted to be in metadata |
||
| 89 | * - following same visible-trumps-hidden guideline as Laravel |
||
| 90 | */ |
||
| 91 | 5 | public function metadataMask() |
|
| 92 | { |
||
| 93 | 5 | $attribs = array_keys($this->getAllAttributes()); |
|
| 94 | 5 | ||
| 95 | 1 | $visible = $this->getVisible(); |
|
| 96 | $hidden = $this->getHidden(); |
||
| 97 | if (0 < count($visible)) { |
||
| 98 | 4 | assert(!empty($visible)); |
|
| 99 | $attribs = array_intersect($visible, $attribs); |
||
| 100 | 4 | } elseif (0 < count($hidden)) { |
|
| 101 | assert(!empty($hidden)); |
||
| 102 | 4 | $attribs = array_diff($attribs, $hidden); |
|
| 103 | 4 | } |
|
| 104 | 4 | ||
| 105 | 4 | return $attribs; |
|
| 106 | 4 | } |
|
| 107 | 4 | ||
| 108 | /* |
||
| 109 | 4 | * Get the endpoint name being exposed |
|
| 110 | 1 | * |
|
| 111 | 1 | */ |
|
| 112 | 1 | public function getEndpointName() |
|
|
|
|||
| 113 | 1 | { |
|
| 114 | $endpoint = isset($this->endpoint) ? $this->endpoint : null; |
||
| 115 | 4 | ||
| 116 | 4 | if (!isset($endpoint)) { |
|
| 117 | $bitter = get_class(); |
||
| 118 | 4 | $name = substr($bitter, strrpos($bitter, '\\')+1); |
|
| 119 | return ($name); |
||
| 120 | } |
||
| 121 | return ($endpoint); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get model's relationships. |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function getRelationships() |
||
| 130 | { |
||
| 131 | if (empty(static::$relationHooks)) { |
||
| 132 | $hooks = []; |
||
| 133 | |||
| 134 | $rels = $this->getRelationshipsFromMethods(true); |
||
| 135 | |||
| 136 | $this->getRelationshipsUnknownPolyMorph($rels, $hooks); |
||
| 137 | |||
| 138 | $this->getRelationshipsKnownPolyMorph($rels, $hooks); |
||
| 139 | |||
| 140 | $this->getRelationshipsHasOne($rels, $hooks); |
||
| 141 | |||
| 142 | $this->getRelationshipsHasMany($rels, $hooks); |
||
| 143 | |||
| 144 | static::$relationHooks = $hooks; |
||
| 145 | } |
||
| 146 | |||
| 147 | return static::$relationHooks; |
||
| 148 | } |
||
| 149 | |||
| 150 | protected function getAllAttributes() |
||
| 151 | { |
||
| 152 | // Adapted from http://stackoverflow.com/a/33514981 |
||
| 153 | // $columns = $this->getFillable(); |
||
| 154 | // Another option is to get all columns for the table like so: |
||
| 155 | $builder = $this->getConnection()->getSchemaBuilder(); |
||
| 156 | $columns = $builder->getColumnListing($this->getTable()); |
||
| 157 | // but it's safer to just get the fillable fields |
||
| 158 | |||
| 159 | $attributes = $this->getAttributes(); |
||
| 160 | |||
| 161 | foreach ($columns as $column) { |
||
| 162 | if (!array_key_exists($column, $attributes)) { |
||
| 163 | $attributes[$column] = null; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | $methods = $this->collectGetters(); |
||
| 168 | |||
| 169 | foreach ($methods as $method) { |
||
| 170 | $attributes[$method] = null; |
||
| 171 | } |
||
| 172 | |||
| 173 | return $attributes; |
||
| 174 | } |
||
| 175 | 3 | ||
| 176 | /** |
||
| 177 | 3 | * @param bool $biDir |
|
| 178 | * |
||
| 179 | 3 | * @return array |
|
| 180 | 3 | */ |
|
| 181 | 3 | protected function getRelationshipsFromMethods($biDir = false) |
|
| 182 | 3 | { |
|
| 183 | 3 | $biDirVal = intval($biDir); |
|
| 184 | 3 | $isCached = isset(static::$relationCategories[$biDirVal]) && !empty(static::$relationCategories[$biDirVal]); |
|
| 185 | 3 | if (!$isCached) { |
|
| 186 | 3 | $model = $this; |
|
| 187 | 3 | $relationships = [ |
|
| 188 | 3 | 'HasOne' => [], |
|
| 189 | 'UnknownPolyMorphSide' => [], |
||
| 190 | 3 | 'HasMany' => [], |
|
| 191 | 'KnownPolyMorphSide' => [] |
||
| 192 | 3 | ]; |
|
| 193 | 3 | $methods = get_class_methods($model); |
|
| 194 | 3 | if (!empty($methods)) { |
|
| 195 | 3 | foreach ($methods as $method) { |
|
| 196 | 3 | if (!method_exists('Illuminate\Database\Eloquent\Model', $method) |
|
| 197 | 3 | ) { |
|
| 198 | 3 | //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php |
|
| 199 | 3 | $reflection = new \ReflectionMethod($model, $method); |
|
| 200 | 3 | ||
| 201 | 3 | $file = new \SplFileObject($reflection->getFileName()); |
|
| 202 | $file->seek($reflection->getStartLine()-1); |
||
| 203 | 3 | $code = ''; |
|
| 204 | 3 | while ($file->key() < $reflection->getEndLine()) { |
|
| 205 | 3 | $code .= $file->current(); |
|
| 206 | 3 | $file->next(); |
|
| 207 | 3 | } |
|
| 208 | 3 | ||
| 209 | 3 | $code = trim(preg_replace('/\s\s+/', '', $code)); |
|
| 210 | 3 | assert( |
|
| 211 | false !== stripos($code, 'function'), |
||
| 212 | 3 | 'Function definition must have keyword \'function\'' |
|
| 213 | 3 | ); |
|
| 214 | 3 | $begin = strpos($code, 'function('); |
|
| 215 | $code = substr($code, $begin, strrpos($code, '}')-$begin+1); |
||
| 216 | 3 | $lastCode = $code[strlen($code)-1]; |
|
| 217 | 3 | assert('}' == $lastCode, 'Final character of function definition must be closing brace'); |
|
| 218 | 3 | foreach ([ |
|
| 219 | 3 | 'hasMany', |
|
| 220 | 3 | 'hasManyThrough', |
|
| 221 | 'belongsToMany', |
||
| 222 | 1 | 'hasOne', |
|
| 223 | 3 | 'belongsTo', |
|
| 224 | 'morphOne', |
||
| 225 | 1 | 'morphTo', |
|
| 226 | 'morphMany', |
||
| 227 | 1 | 'morphToMany', |
|
| 228 | 'morphedByMany' |
||
| 229 | 1 | ] as $relation) { |
|
| 230 | $search = '$this->' . $relation . '('; |
||
| 231 | 3 | if ($pos = stripos($code, $search)) { |
|
| 232 | 2 | //Resolve the relation's model to a Relation object. |
|
| 233 | 2 | $relationObj = $model->$method(); |
|
| 234 | 3 | if ($relationObj instanceof Relation) { |
|
| 235 | 3 | $relObject = $relationObj->getRelated(); |
|
| 236 | 3 | $relatedModel = '\\' . get_class($relObject); |
|
| 237 | 3 | if (in_array(MetadataTrait::class, class_uses($relatedModel))) { |
|
| 238 | 3 | $relations = [ |
|
| 239 | 3 | 'hasManyThrough', |
|
| 240 | 3 | 'belongsToMany', |
|
| 241 | 'hasMany', |
||
| 242 | 'morphMany', |
||
| 243 | 'morphToMany', |
||
| 244 | 'morphedByMany' |
||
| 245 | ]; |
||
| 246 | if (in_array($relation, $relations)) { |
||
| 247 | //Collection or array of models (because Collection is Arrayable) |
||
| 248 | $relationships['HasMany'][$method] = $biDir ? $relationObj : $relatedModel; |
||
| 249 | } elseif ('morphTo' === $relation) { |
||
| 250 | // Model isn't specified because relation is polymorphic |
||
| 251 | $relationships['UnknownPolyMorphSide'][$method] = |
||
| 252 | $biDir ? $relationObj : '\Illuminate\Database\Eloquent\Model|\Eloquent'; |
||
| 253 | } else { |
||
| 254 | //Single model is returned |
||
| 255 | $relationships['HasOne'][$method] = $biDir ? $relationObj : $relatedModel; |
||
| 256 | } |
||
| 257 | if (in_array($relation, ['morphMany', 'morphOne', 'morphToMany'])) { |
||
| 258 | $relationships['KnownPolyMorphSide'][$method] = |
||
| 259 | $biDir ? $relationObj : $relatedModel; |
||
| 260 | } |
||
| 261 | if (in_array($relation, ['morphedByMany'])) { |
||
| 262 | $relationships['UnknownPolyMorphSide'][$method] = |
||
| 263 | $biDir ? $relationObj : $relatedModel; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | static::$relationCategories[$biDirVal] = $relationships; |
||
| 273 | } |
||
| 274 | return static::$relationCategories[$biDirVal]; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the visible attributes for the model. |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | abstract public function getVisible(); |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the hidden attributes for the model. |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | abstract public function getHidden(); |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the primary key for the model. |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | abstract public function getKeyName(); |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get the current connection name for the model. |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | abstract public function getConnectionName(); |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the database connection for the model. |
||
| 307 | * |
||
| 308 | * @return \Illuminate\Database\Connection |
||
| 309 | */ |
||
| 310 | abstract public function getConnection(); |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get all of the current attributes on the model. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | abstract public function getAttributes(); |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get the table associated with the model. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | abstract public function getTable(); |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get the fillable attributes for the model. |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | abstract public function getFillable(); |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Dig up all defined getters on the model. |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | protected function collectGetters() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param $foo |
||
| 361 | * @param mixed $condition |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | private function polyglotKeyMethodNames($foo, $condition = false) |
||
| 407 | |||
| 408 | private function polyglotKeyMethodBackupNames($foo, $condition = false) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param $hooks |
||
| 445 | * @param $first |
||
| 446 | * @param $property |
||
| 447 | * @param $last |
||
| 448 | * @param $mult |
||
| 449 | * @param $targ |
||
| 450 | * @param string|null $targ |
||
| 451 | * @param null|mixed $type |
||
| 452 | */ |
||
| 453 | private function addRelationsHook(&$hooks, $first, $property, $last, $mult, $targ, $type = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param $rels |
||
| 471 | * @param $hooks |
||
| 472 | */ |
||
| 473 | private function getRelationshipsHasMany($rels, &$hooks) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param $rels |
||
| 500 | * @param $hooks |
||
| 501 | */ |
||
| 502 | private function getRelationshipsHasOne($rels, &$hooks) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param $rels |
||
| 529 | * @param $hooks |
||
| 530 | */ |
||
| 531 | private function getRelationshipsKnownPolyMorph($rels, &$hooks) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param $rels |
||
| 556 | * @param $hooks |
||
| 557 | */ |
||
| 558 | private function getRelationshipsUnknownPolyMorph($rels, &$hooks) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * SUpplemental function to retrieve cast array for Laravel versions that do not supply hasCasts. |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | public function retrieveCasts() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Return list of relations to be eager-loaded by Laravel query provider. |
||
| 593 | * |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | public function getEagerLoad() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Set list of relations to be eager-loaded. |
||
| 604 | * |
||
| 605 | * @param array $relations |
||
| 606 | */ |
||
| 607 | public function setEagerLoad(array $relations) |
||
| 613 | |||
| 614 | /* |
||
| 615 | * Is this model the known side of at least one polymorphic relation? |
||
| 616 | */ |
||
| 617 | public function isKnownPolymorphSide() |
||
| 624 | |||
| 625 | /* |
||
| 626 | * Is this model on the unknown side of at least one polymorphic relation? |
||
| 627 | */ |
||
| 628 | public function isUnknownPolymorphSide() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Extract entity gubbins detail for later downstream use. |
||
| 638 | * |
||
| 639 | * @return EntityGubbins |
||
| 640 | */ |
||
| 641 | public function extractGubbins() |
||
| 694 | } |
||
| 695 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.