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 |
||
12 | class BaseModel extends Model implements BaseModelEventObserverable, Transformable |
||
13 | { |
||
14 | use BaseModelEventsTrait, TransformableTrait; |
||
15 | |||
16 | /** |
||
17 | * Indicates if the model should be auto set user_id. |
||
18 | * |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $autoRelatedUserId = true; |
||
22 | |||
23 | /** |
||
24 | * The name of the "related user id" column. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | const RELATED_USER_ID = 'user_id'; |
||
29 | |||
30 | /** |
||
31 | * Indicates if the model should be recorded users. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $userstamps = true; |
||
36 | |||
37 | /** |
||
38 | * The name of the "created by" column. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | const CREATED_BY = 'created_by'; |
||
43 | |||
44 | /** |
||
45 | * The name of the "updated by" column. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const UPDATED_BY = 'updated_by'; |
||
50 | |||
51 | /** |
||
52 | * Indicates if the model should be recorded ips. |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $ipstamps = true; |
||
57 | |||
58 | /** |
||
59 | * The name of the "created ip" column. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | const CREATED_IP = 'created_ip'; |
||
64 | |||
65 | /** |
||
66 | * The name of the "updated ip" column. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | const UPDATED_IP = 'updated_ip'; |
||
71 | |||
72 | /** |
||
73 | * The name of the "deleted ip" column. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | const DELETED_IP = 'deleted_ip'; |
||
78 | |||
79 | /** |
||
80 | * The name of the "deleted by" column. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | const DELETED_BY = 'deleted_by'; |
||
85 | |||
86 | /** |
||
87 | * @return \Dingo\Api\Auth\Auth |
||
88 | */ |
||
89 | protected function api_auth() |
||
93 | |||
94 | /** |
||
95 | * Update the creation and update ips. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | protected function updateIps() |
||
100 | { |
||
101 | $ip = smart_get_client_ip(); |
||
102 | |||
103 | if (! $this->isDirty(static::UPDATED_IP) && $this->hasTableColumn(static::UPDATED_IP)) { |
||
104 | $this->{static::UPDATED_IP} = $ip; |
||
105 | } |
||
106 | |||
107 | if (! $this->exists && ! $this->isDirty(static::CREATED_IP) && $this->hasTableColumn(static::CREATED_IP)) { |
||
108 | $this->{static::CREATED_IP} = $ip; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get current model's user_id. |
||
114 | * |
||
115 | * @return mixed|null |
||
116 | */ |
||
117 | public function getRelatedUserId() |
||
121 | |||
122 | /** |
||
123 | * Update the creation and update by users. |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | protected function updateUsers() |
||
142 | |||
143 | /** |
||
144 | * Get current auth user. |
||
145 | * |
||
146 | * @return \Illuminate\Auth\GenericUser|Model|null |
||
147 | */ |
||
148 | public function getAuthUser() |
||
159 | |||
160 | /** |
||
161 | * Get current auth user_id. |
||
162 | * |
||
163 | * @return mixed|null |
||
164 | */ |
||
165 | public function getAuthUserId() |
||
175 | |||
176 | /** |
||
177 | * check auth user owner the current model. |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function isAuthUserOwner() |
||
185 | |||
186 | /** |
||
187 | * get all the database table columns listing. |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getTableColumns() |
||
195 | |||
196 | /** |
||
197 | * check column exist in table. |
||
198 | * |
||
199 | * @param string $column |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function hasTableColumn($column) |
||
207 | |||
208 | /** |
||
209 | * check columns exist in table. |
||
210 | * |
||
211 | * @param array $columns |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function hasTableColumns(array $columns) |
||
219 | } |
||
220 |
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.