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 | View Code Duplication | 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 | protected function listsMapped(Builder $query, ArgumentBag $args, $table, $column) |
||
189 | |||
190 | /** |
||
191 | * Get an array with the values of given mapped attribute. |
||
192 | * |
||
193 | * @param \Sofa\Eloquence\Builder $query |
||
194 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
195 | * @param string $table |
||
196 | * @param string $column |
||
197 | * @return array |
||
198 | */ |
||
199 | protected function pluckMapped(Builder $query, ArgumentBag $args, $table, $column) |
||
211 | |||
212 | /** |
||
213 | * Add select clause for key of the list array. |
||
214 | * |
||
215 | * @param \Sofa\Eloquence\Builder $query |
||
216 | * @param string $key |
||
217 | * @return \Sofa\Eloquence\Builder |
||
218 | */ |
||
219 | protected function mappedSelectListsKey(Builder $query, $key) |
||
227 | |||
228 | /** |
||
229 | * Join mapped table(s). |
||
230 | * |
||
231 | * @param \Sofa\Eloquence\Builder $query |
||
232 | * @param string $target |
||
233 | * @return string |
||
234 | */ |
||
235 | protected function joinMapped(Builder $query, $target) |
||
247 | |||
248 | /** |
||
249 | * Join relation's table accordingly. |
||
250 | * |
||
251 | * @param \Sofa\Eloquence\Builder $query |
||
252 | * @param string $segment |
||
253 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
254 | * @return array |
||
255 | */ |
||
256 | protected function joinSegment(Builder $query, $segment, EloquentModel $parent) |
||
283 | |||
284 | /** |
||
285 | * Determine whether given table has been already joined. |
||
286 | * |
||
287 | * @param \Sofa\Eloquence\Builder $query |
||
288 | * @param string $table |
||
289 | * @return boolean |
||
290 | */ |
||
291 | protected function alreadyJoined(Builder $query, $table) |
||
297 | |||
298 | /** |
||
299 | * Get the keys from relation in order to join the table. |
||
300 | * |
||
301 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
302 | * @return array |
||
303 | * |
||
304 | * @throws \LogicException |
||
305 | */ |
||
306 | protected function getJoinKeys(Relation $relation) |
||
322 | |||
323 | /** |
||
324 | * Get single value result from the mapped attribute. |
||
325 | * |
||
326 | * @param \Sofa\Eloquence\Builder $query |
||
327 | * @param string $method |
||
328 | * @param string $qualifiedColumn |
||
329 | * @return mixed |
||
330 | */ |
||
331 | protected function mappedSingleResult(Builder $query, $method, $qualifiedColumn) |
||
335 | |||
336 | /** |
||
337 | * Add whereHas subquery on the mapped attribute relation. |
||
338 | * |
||
339 | * @param \Sofa\Eloquence\Builder $query |
||
340 | * @param string $method |
||
341 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
342 | * @param string $target |
||
343 | * @param string $column |
||
344 | * @return \Sofa\Eloquence\Builder |
||
345 | */ |
||
346 | protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $target, $column) |
||
358 | |||
359 | /** |
||
360 | * Get the relation constraint closure. |
||
361 | * |
||
362 | * @param string $method |
||
363 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
364 | * @return \Closure |
||
365 | */ |
||
366 | protected function getMappedWhereConstraint($method, ArgumentBag $args) |
||
372 | |||
373 | /** |
||
374 | * Get boolean called on the original method and set it to default. |
||
375 | * |
||
376 | * @param \Sofa\EloquenceArgumentBag $args |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function getMappedBoolean(ArgumentBag $args) |
||
387 | |||
388 | /** |
||
389 | * Determine the operator for count relation query and set 'not' appropriately. |
||
390 | * |
||
391 | * @param string $method |
||
392 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function getMappedOperator($method, ArgumentBag $args) |
||
407 | |||
408 | /** |
||
409 | * Get the mapping key. |
||
410 | * |
||
411 | * @param string $key |
||
412 | * @return string|null |
||
413 | */ |
||
414 | public function getMappingForAttribute($key) |
||
420 | |||
421 | /** |
||
422 | * Determine whether the mapping points to relation. |
||
423 | * |
||
424 | * @param string $mapping |
||
425 | * @return boolean |
||
426 | */ |
||
427 | protected function relationMapping($mapping) |
||
431 | |||
432 | /** |
||
433 | * Determine whether a mapping exists for an attribute. |
||
434 | * |
||
435 | * @param string $key |
||
436 | * @return boolean |
||
437 | */ |
||
438 | public function hasMapping($key) |
||
446 | |||
447 | /** |
||
448 | * Parse defined mappings into flat array. |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | protected function parseMappings() |
||
464 | |||
465 | /** |
||
466 | * Parse implicit mappings. |
||
467 | * |
||
468 | * @param array $attributes |
||
469 | * @param string $target |
||
470 | * @return void |
||
471 | */ |
||
472 | protected function parseImplicitMapping($attributes, $target) |
||
478 | |||
479 | /** |
||
480 | * Map an attribute to a value. |
||
481 | * |
||
482 | * @param string $key |
||
483 | * @return mixed |
||
484 | */ |
||
485 | protected function mapAttribute($key) |
||
491 | |||
492 | /** |
||
493 | * Get mapped value. |
||
494 | * |
||
495 | * @param \Illuminate\Database\Eloquent\Model $target |
||
496 | * @param array $segments |
||
497 | * @return mixed |
||
498 | */ |
||
499 | protected function getTarget($target, array $segments) |
||
511 | |||
512 | /** |
||
513 | * Set value of a mapped attribute. |
||
514 | * |
||
515 | * @param string $key |
||
516 | * @param mixed $value |
||
517 | */ |
||
518 | View Code Duplication | protected function setMappedAttribute($key, $value) |
|
530 | |||
531 | /** |
||
532 | * Flag mapped model to be saved along with this model. |
||
533 | * |
||
534 | * @param \Illuminate\Database\Eloquent\Model $target |
||
535 | */ |
||
536 | protected function addTargetToSave($target) |
||
542 | |||
543 | /** |
||
544 | * Save mapped relations. |
||
545 | * |
||
546 | * @return void |
||
547 | */ |
||
548 | protected function saveMapped() |
||
556 | |||
557 | /** |
||
558 | * Unset mapped attribute. |
||
559 | * |
||
560 | * @param string $key |
||
561 | * @return void |
||
562 | */ |
||
563 | View Code Duplication | protected function forget($key) |
|
573 | |||
574 | /** |
||
575 | * @codeCoverageIgnore |
||
576 | * |
||
577 | * @inheritdoc |
||
578 | */ |
||
579 | protected function mutateAttributeForArray($key, $value) |
||
589 | |||
590 | /** |
||
591 | * Get the array of attribute mappings. |
||
592 | * |
||
593 | * @return array |
||
594 | */ |
||
595 | public function getMaps() |
||
599 | } |
||
600 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.