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 |
||
39 | class UserModel extends BitrixModel |
||
40 | { |
||
41 | /** |
||
42 | * Bitrix entity object. |
||
43 | * |
||
44 | * @var object |
||
45 | */ |
||
46 | public static $bxObject; |
||
47 | |||
48 | /** |
||
49 | * Corresponding object class name. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected static $objectClass = 'CUser'; |
||
54 | |||
55 | /** |
||
56 | * Current user cache. |
||
57 | * |
||
58 | * @var static |
||
59 | */ |
||
60 | protected static $currentUser = null; |
||
61 | |||
62 | /** |
||
63 | * Have groups been already fetched from DB? |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $groupsAreFetched = false; |
||
68 | |||
69 | /** |
||
70 | * Instantiate a query object for the model. |
||
71 | * |
||
72 | * @return UserQuery |
||
73 | */ |
||
74 | public static function query() |
||
75 | { |
||
76 | return new UserQuery(static::instantiateObject(), get_called_class()); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get a new instance for the current user |
||
81 | * |
||
82 | * @return static |
||
83 | */ |
||
84 | public static function current() |
||
90 | |||
91 | /** |
||
92 | * Get a fresh instance for the current user and save it to local cache. |
||
93 | * |
||
94 | * @return static |
||
95 | */ |
||
96 | public static function freshCurrent() |
||
102 | |||
103 | /** |
||
104 | * Fill extra fields when $this->field is called. |
||
105 | * |
||
106 | * @return null |
||
107 | */ |
||
108 | protected function afterFill() |
||
114 | |||
115 | /** |
||
116 | * Fill model groups if they are already known. |
||
117 | * Saves DB queries. |
||
118 | * |
||
119 | * @param array $groups |
||
120 | * |
||
121 | * @return null |
||
122 | */ |
||
123 | public function fillGroups($groups) |
||
129 | |||
130 | /** |
||
131 | * Load model fields from database if they are not loaded yet. |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function load() |
||
142 | |||
143 | /** |
||
144 | * Get user groups from cache or database. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getGroups() |
||
156 | |||
157 | /** |
||
158 | * Refresh model from database and place data to $this->fields. |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function refresh() |
||
170 | |||
171 | /** |
||
172 | * Refresh user fields and save them to a class field. |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | View Code Duplication | public function refreshFields() |
|
197 | |||
198 | /** |
||
199 | * Refresh user groups and save them to a class field. |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | public function refreshGroups() |
||
219 | |||
220 | /** |
||
221 | * Check if user is an admin. |
||
222 | */ |
||
223 | public function isAdmin() |
||
227 | |||
228 | /** |
||
229 | * Check if this user is the operating user. |
||
230 | */ |
||
231 | public function isCurrent() |
||
237 | |||
238 | /** |
||
239 | * Check if user has role with a given ID. |
||
240 | * |
||
241 | * @param $role_id |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function hasGroupWithId($role_id) |
||
249 | |||
250 | /** |
||
251 | * Check if user is authorized. |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function isAuthorized() |
||
261 | |||
262 | /** |
||
263 | * Check if user is guest. |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function isGuest() |
||
271 | |||
272 | /** |
||
273 | * Logout user. |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | public function logout() |
||
278 | { |
||
279 | global $USER; |
||
280 | |||
281 | $USER->logout(); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Scope to get only users from a given group / groups. |
||
286 | * |
||
287 | * @param UserQuery $query |
||
288 | * @param int|array $id |
||
289 | * |
||
290 | * @return UserQuery |
||
291 | */ |
||
292 | public function scopeFromGroup($query, $id) |
||
298 | |||
299 | /** |
||
300 | * Substitute old group with the new one. |
||
301 | * |
||
302 | * @param int $old |
||
303 | * @param int $new |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | public function substituteGroup($old, $new) |
||
321 | } |
||
322 |
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.