Complex classes like Metable 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 Metable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait Metable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Query methods customizable by this trait. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $metaQueryable = [ |
||
| 21 | 'where', 'whereBetween', 'whereIn', 'whereNull', |
||
| 22 | 'whereDate', 'whereYear', 'whereMonth', 'whereDay', |
||
| 23 | 'orderBy', 'pluck', 'value', 'aggregate', |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Register hooks for the trait. |
||
| 28 | * |
||
| 29 | * @codeCoverageIgnore |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public static function bootMetable() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Determine wheter method called on the query is customizable by this trait. |
||
| 53 | * |
||
| 54 | * @param string $method |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | protected function isMetaQueryable($method) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Custom query handler for querying meta attributes. |
||
| 64 | * |
||
| 65 | * @param \Sofa\Eloquence\Builder $query |
||
| 66 | * @param string $method |
||
| 67 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 68 | * @return mixed |
||
| 69 | */ |
||
| 70 | protected function metaQuery(Builder $query, $method, ArgumentBag $args) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Adjust meta columns for select statement. |
||
| 81 | * |
||
| 82 | * @param \Sofa\Eloquence\Builder $query |
||
| 83 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | protected function metaSelect(Builder $query, ArgumentBag $args) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Join meta attributes table in order to call provided method. |
||
| 113 | * |
||
| 114 | * @param \Sofa\Eloquence\Builder $query |
||
| 115 | * @param string $method |
||
| 116 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | protected function metaJoinQuery(Builder $query, $method, ArgumentBag $args) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Order query by meta attribute. |
||
| 134 | * |
||
| 135 | * @param \Sofa\Eloquence\Builder $query |
||
| 136 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 137 | * @param string $alias |
||
| 138 | * @return \Sofa\Eloquence\Builder |
||
| 139 | */ |
||
| 140 | protected function orderByMeta(Builder $query, $args, $alias) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get an array with the values of given meta attribute. |
||
| 149 | * |
||
| 150 | * @param \Sofa\Eloquence\Builder $query |
||
| 151 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 152 | * @param string $alias |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | protected function pluckMeta(Builder $query, ArgumentBag $args, $alias) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add select clause for key of the list array. |
||
| 170 | * |
||
| 171 | * @param \Sofa\Eloquence\Builder $query |
||
| 172 | * @param string $key |
||
| 173 | * @return \Sofa\Eloquence\Builder |
||
| 174 | */ |
||
| 175 | protected function metaSelectListsKey(Builder $query, $key) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get single value result from the meta attribute. |
||
| 190 | * |
||
| 191 | * @param \Sofa\Eloquence\Builder $query |
||
| 192 | * @param string $method |
||
| 193 | * @param string $alias |
||
| 194 | * @return mixed |
||
| 195 | */ |
||
| 196 | protected function metaSingleResult(Builder $query, $method, $alias) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Join meta attributes table. |
||
| 203 | * |
||
| 204 | * @param \Sofa\Eloquence\Builder $query |
||
| 205 | * @param string $column |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | protected function joinMeta(Builder $query, $column) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Generate unique alias for meta attributes table. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | protected function generateMetaAlias() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add whereHas subquery on the meta attributes relation. |
||
| 237 | * |
||
| 238 | * @param \Sofa\Eloquence\Builder $query |
||
| 239 | * @param string $method |
||
| 240 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 241 | * @return \Sofa\Eloquence\Builder |
||
| 242 | */ |
||
| 243 | protected function metaHasQuery(Builder $query, $method, ArgumentBag $args) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get boolean called on the original method and set it to default. |
||
| 260 | * |
||
| 261 | * @param \Sofa\EloquenceArgumentBag $args |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | protected function getMetaBoolean(ArgumentBag $args) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Determine the operator for count relation query. |
||
| 275 | * |
||
| 276 | * @param string $method |
||
| 277 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | protected function getMetaOperator($method, ArgumentBag $args) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Integers and floats must be passed in raw form in order to avoid string |
||
| 291 | * comparison, due to the fact that all meta values are stored as strings. |
||
| 292 | * |
||
| 293 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | protected function unbindNumerics(ArgumentBag $args) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the relation constraint closure. |
||
| 313 | * |
||
| 314 | * @param string $method |
||
| 315 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 316 | * @return \Closure |
||
| 317 | */ |
||
| 318 | protected function getMetaWhereConstraint($method, ArgumentBag $args) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Query Builder whereBetween override required to pass raw numeric values. |
||
| 339 | * |
||
| 340 | * @param string $column |
||
| 341 | * @param array $values |
||
| 342 | * @return \Closure |
||
| 343 | */ |
||
| 344 | protected function getMetaBetweenConstraint($column, array $values) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Save new or updated meta attributes and delete the ones that were unset. |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | protected function saveMeta() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Determine whether meta attribute is allowed for the model. |
||
| 374 | * |
||
| 375 | * @param string $key |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public function allowsMeta($key) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Determine whether meta attribute exists on the model. |
||
| 387 | * |
||
| 388 | * @param string $key |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | public function hasMeta($key) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get meta attribute value. |
||
| 398 | * |
||
| 399 | * @param string $key |
||
| 400 | * @return mixed |
||
| 401 | */ |
||
| 402 | public function getMeta($key) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get meta attribute values by group. |
||
| 409 | * |
||
| 410 | * @param string $key |
||
| 411 | * @return mixed |
||
| 412 | */ |
||
| 413 | public function getMetaByGroup($group) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Set meta attribute. |
||
| 420 | * |
||
| 421 | * @param string $key |
||
| 422 | * @param mixed $value |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function setMeta($key, $value, $group = null) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Meta attributes relation. |
||
| 432 | * |
||
| 433 | * @codeCoverageIgnore |
||
| 434 | * |
||
| 435 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 436 | */ |
||
| 437 | public function metaAttributes() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get meta attributes as collection. |
||
| 444 | * |
||
| 445 | * @return \Sofa\Eloquence\Metable\AttributeBag |
||
| 446 | */ |
||
| 447 | public function getMetaAttributes() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Accessor for metaAttributes property |
||
| 456 | * |
||
| 457 | * @return \Sofa\Eloquence\Metable\AttributeBag |
||
| 458 | */ |
||
| 459 | public function getMetaAttributesAttribute() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get meta attributes as associative array. |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | public function getMetaAttributesArray() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Load meta attributes relation. |
||
| 476 | * |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | protected function loadMetaAttributes() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Reload meta attributes from db or set empty bag for newly created model. |
||
| 494 | * |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | protected function reloadMetaAttributes() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Get allowed meta attributes array. |
||
| 506 | * |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | public function getAllowedMeta() |
||
| 513 | } |
||
| 514 |
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.