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 |
||
17 | class Card extends Model |
||
18 | { |
||
19 | use HasSlug, Validatable; |
||
20 | |||
21 | /** |
||
22 | * Constant for card active status |
||
23 | * |
||
24 | * @var bool |
||
25 | */ |
||
26 | const ACTIVE = true; |
||
27 | /** |
||
28 | * Constant for card inactive status |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | const INACTIVE = false; |
||
33 | |||
34 | /** |
||
35 | * Max size of slug |
||
36 | * |
||
37 | * @var int |
||
38 | */ |
||
39 | const SLUG_SIZE = 100; |
||
40 | |||
41 | /** |
||
42 | * Timestamp enabled. |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | public $timestamps = true; |
||
47 | /** |
||
48 | * Name of database table. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $table = 'card'; |
||
53 | /** |
||
54 | * List of allowed columns to be used in $this->fill(). |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $fillable = ['title', 'slug', 'active', 'content', 'meta_description', 'category_id']; |
||
59 | |||
60 | protected $appends = ['short_modified']; |
||
61 | |||
62 | protected $casts = [ |
||
63 | 'active' => 'boolean', |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * Get the category this card belong to |
||
68 | */ |
||
69 | 2 | public function category() |
|
73 | |||
74 | /** |
||
75 | * Get collection of card views |
||
76 | */ |
||
77 | public function cardViews() |
||
81 | |||
82 | /** |
||
83 | * Get formatted version of updated_at attribute |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 5 | public function getShortModifiedAttribute(): string |
|
95 | |||
96 | /** |
||
97 | * Get the options for generating the slug. |
||
98 | */ |
||
99 | 11 | public function getSlugOptions(): SlugOptions |
|
106 | |||
107 | /** |
||
108 | * Get collection of validation rules for model attributes |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | 11 | protected function getRules(): array |
|
125 | |||
126 | /** |
||
127 | * Score to filter by active cards |
||
128 | * |
||
129 | * @param Builder $query |
||
130 | * @return Builder |
||
131 | */ |
||
132 | 6 | public function scopeActive(Builder $query): Builder |
|
136 | |||
137 | /** |
||
138 | * Score to filter by LIKE search |
||
139 | * |
||
140 | * @param Builder $query |
||
141 | * @param string $keyword |
||
142 | * @return Builder |
||
143 | */ |
||
144 | 3 | View Code Duplication | public function scopeSearch(Builder $query, string $keyword): Builder |
151 | |||
152 | /** |
||
153 | * Update card view counter by 1 and insert record in card view table |
||
154 | * |
||
155 | * @param string $ip |
||
156 | */ |
||
157 | public function incrementViews(string $ip): void |
||
174 | } |
||
175 |
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.