Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Mappable 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 Mappable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait Mappable |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Flat array representation of mapped attributes. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $mappedAttributes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Related mapped objects to save along with the mappable instance. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $targetsToSave = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Register hooks for the trait. |
||
| 38 | * |
||
| 39 | * @codeCoverageIgnore |
||
| 40 | * |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | public static function bootMappable() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Custom query handler for querying mapped attributes. |
||
| 61 | * |
||
| 62 | * @param \Sofa\Eloquence\Builder $query |
||
| 63 | * @param string $method |
||
| 64 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | protected function mappedQuery(Builder $query, $method, ArgumentBag $args) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Adjust mapped columns for select statement. |
||
| 82 | * |
||
| 83 | * @param \Sofa\Eloquence\Builder $query |
||
| 84 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | protected function mappedSelect(Builder $query, ArgumentBag $args) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Handle querying relational mappings. |
||
| 127 | * |
||
| 128 | * @param \Sofa\Eloquence\Builder $query |
||
| 129 | * @param string $method |
||
| 130 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 131 | * @param string $mapping |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapping) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Join mapped table(s) in order to call given method. |
||
| 147 | * |
||
| 148 | * @param \Sofa\Eloquence\Builder $query |
||
| 149 | * @param string $method |
||
| 150 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 151 | * @param string $target |
||
| 152 | * @param string $column |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | protected function mappedJoinQuery($query, $method, ArgumentBag $args, $target, $column) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Order query by mapped attribute. |
||
| 170 | * |
||
| 171 | * @param \Sofa\Eloquence\Builder $query |
||
| 172 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 173 | * @param string $table |
||
| 174 | * @param string $column |
||
| 175 | * @param string $target |
||
| 176 | * @return \Sofa\Eloquence\Builder |
||
| 177 | */ |
||
| 178 | protected function orderByMapped(Builder $query, ArgumentBag $args, $table, $column, $target) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param \Sofa\Eloquence\Builder $query |
||
| 187 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 188 | * @param string $table |
||
| 189 | * @param string $column |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | protected function listsMapped(Builder $query, ArgumentBag $args, $table, $column) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get an array with the values of given mapped attribute. |
||
| 200 | * |
||
| 201 | * @param \Sofa\Eloquence\Builder $query |
||
| 202 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 203 | * @param string $table |
||
| 204 | * @param string $column |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | protected function pluckMapped(Builder $query, ArgumentBag $args, $table, $column) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Add select clause for key of the list array. |
||
| 222 | * |
||
| 223 | * @param \Sofa\Eloquence\Builder $query |
||
| 224 | * @param string $key |
||
| 225 | * @return \Sofa\Eloquence\Builder |
||
| 226 | */ |
||
| 227 | protected function mappedSelectListsKey(Builder $query, $key) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Join mapped table(s). |
||
| 238 | * |
||
| 239 | * @param \Sofa\Eloquence\Builder $query |
||
| 240 | * @param string $target |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | protected function joinMapped(Builder $query, $target) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Join relation's table accordingly. |
||
| 258 | * |
||
| 259 | * @param \Sofa\Eloquence\Builder $query |
||
| 260 | * @param string $segment |
||
| 261 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | protected function joinSegment(Builder $query, $segment, EloquentModel $parent) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Determine whether given table has been already joined. |
||
| 294 | * |
||
| 295 | * @param \Sofa\Eloquence\Builder $query |
||
| 296 | * @param string $table |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | protected function alreadyJoined(Builder $query, $table) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the keys from relation in order to join the table. |
||
| 308 | * |
||
| 309 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
| 310 | * @return array |
||
| 311 | * |
||
| 312 | * @throws \LogicException |
||
| 313 | */ |
||
| 314 | protected function getJoinKeys(Relation $relation) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get single value result from the mapped attribute. |
||
| 333 | * |
||
| 334 | * @param \Sofa\Eloquence\Builder $query |
||
| 335 | * @param string $method |
||
| 336 | * @param string $qualifiedColumn |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | protected function mappedSingleResult(Builder $query, $method, $qualifiedColumn) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Add whereHas subquery on the mapped attribute relation. |
||
| 346 | * |
||
| 347 | * @param \Sofa\Eloquence\Builder $query |
||
| 348 | * @param string $method |
||
| 349 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 350 | * @param string $target |
||
| 351 | * @param string $column |
||
| 352 | * @return \Sofa\Eloquence\Builder |
||
| 353 | */ |
||
| 354 | protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $target, $column) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the relation constraint closure. |
||
| 369 | * |
||
| 370 | * @param string $method |
||
| 371 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 372 | * @return \Closure |
||
| 373 | */ |
||
| 374 | protected function getMappedWhereConstraint($method, ArgumentBag $args) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get boolean called on the original method and set it to default. |
||
| 383 | * |
||
| 384 | * @param \Sofa\EloquenceArgumentBag $args |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | protected function getMappedBoolean(ArgumentBag $args) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Determine the operator for count relation query and set 'not' appropriately. |
||
| 398 | * |
||
| 399 | * @param string $method |
||
| 400 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function getMappedOperator($method, ArgumentBag $args) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the mapping key. |
||
| 418 | * |
||
| 419 | * @param string $key |
||
| 420 | * @return string|null |
||
| 421 | */ |
||
| 422 | public function getMappingForAttribute($key) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Determine whether the mapping points to relation. |
||
| 431 | * |
||
| 432 | * @param string $mapping |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | protected function relationMapping($mapping) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Determine whether a mapping exists for an attribute. |
||
| 442 | * |
||
| 443 | * @param string $key |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function hasMapping($key) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Parse defined mappings into flat array. |
||
| 457 | * |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | protected function parseMappings() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Parse implicit mappings. |
||
| 475 | * |
||
| 476 | * @param array $attributes |
||
| 477 | * @param string $target |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | protected function parseImplicitMapping($attributes, $target) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Map an attribute to a value. |
||
| 489 | * |
||
| 490 | * @param string $key |
||
| 491 | * @return mixed |
||
| 492 | */ |
||
| 493 | protected function mapAttribute($key) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get mapped value. |
||
| 502 | * |
||
| 503 | * @param \Illuminate\Database\Eloquent\Model $target |
||
| 504 | * @param array $segments |
||
| 505 | * @return mixed |
||
| 506 | */ |
||
| 507 | protected function getTarget($target, array $segments) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Set value of a mapped attribute. |
||
| 522 | * |
||
| 523 | * @param string $key |
||
| 524 | * @param mixed $value |
||
| 525 | */ |
||
| 526 | View Code Duplication | protected function setMappedAttribute($key, $value) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Flag mapped model to be saved along with this model. |
||
| 541 | * |
||
| 542 | * @param \Illuminate\Database\Eloquent\Model $target |
||
| 543 | */ |
||
| 544 | protected function addTargetToSave($target) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Save mapped relations. |
||
| 553 | * |
||
| 554 | * @return void |
||
| 555 | */ |
||
| 556 | protected function saveMapped() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Unset mapped attribute. |
||
| 567 | * |
||
| 568 | * @param string $key |
||
| 569 | * @return void |
||
| 570 | */ |
||
| 571 | View Code Duplication | protected function forget($key) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * @codeCoverageIgnore |
||
| 584 | * |
||
| 585 | * @param string $key |
||
| 586 | * @param mixed $value |
||
| 587 | * |
||
| 588 | * @inheritdoc |
||
| 589 | */ |
||
| 590 | protected function mutateAttributeForArray($key, $value) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Get the array of attribute mappings. |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | public function getMaps() |
||
| 610 | } |
||
| 611 |
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.