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:
| 1 | <?php |
||
| 14 | class Contact extends Model |
||
| 15 | { |
||
| 16 | use SoftDeletes; |
||
| 17 | |||
| 18 | protected $casts = [ |
||
| 19 | 'active' => 'boolean', |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Attributes |
||
| 24 | */ |
||
| 25 | |||
| 26 | View Code Duplication | public function getFilterTagsAttribute() |
|
| 35 | |||
| 36 | |||
| 37 | View Code Duplication | public function setFilterTagsAttribute($tags) |
|
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Scopes |
||
| 49 | */ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Scope a query to only include active pings. |
||
| 53 | * |
||
| 54 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 55 | */ |
||
| 56 | public function scopeActive($query) |
||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Trim each item in the array |
||
| 65 | * |
||
| 66 | * @param array $tags |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | private function trimArrayItems(array $tags) |
||
| 76 | } |
||
| 77 |
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.