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 |
||
| 7 | class UserModel extends BaseModel |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Bitrix entity object. |
||
| 11 | * |
||
| 12 | * @var object |
||
| 13 | */ |
||
| 14 | public static $bxObject; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Corresponding object class name. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected static $objectClass = 'CUser'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Have groups been already fetched from DB? |
||
| 25 | * |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected $groupsAreFetched = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Instantiate a query object for the model. |
||
| 32 | * |
||
| 33 | * @return UserQuery |
||
| 34 | */ |
||
| 35 | public static function query() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get a new instance for the current user. |
||
| 42 | * |
||
| 43 | * @param null $fields |
||
| 44 | * |
||
| 45 | * @return static |
||
| 46 | */ |
||
| 47 | public static function current($fields = null) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Fill extra fields when $this->field is called. |
||
| 62 | * |
||
| 63 | * @return null |
||
| 64 | */ |
||
| 65 | protected function afterFill() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Fill model groups if they are already known. |
||
| 74 | * Saves DB queries. |
||
| 75 | * |
||
| 76 | * @param array $groups |
||
| 77 | * |
||
| 78 | * @return null |
||
| 79 | */ |
||
| 80 | public function fillGroups($groups) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get all model attributes from cache or database. |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function get() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get user groups from cache or database. |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function getGroups() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Refresh model from database and place data to $this->fields. |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function refresh() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Refresh user fields and save them to a class field. |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function refreshFields() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Refresh user groups and save them to a class field. |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function refreshGroups() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Check if user is an admin. |
||
| 177 | */ |
||
| 178 | public function isAdmin() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check if this user is the operating user. |
||
| 185 | */ |
||
| 186 | public function isCurrent() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Check if user has role with a given ID. |
||
| 195 | * |
||
| 196 | * @param $role_id |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function hasGroupWithId($role_id) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Check if user is authorized. |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function isAuthorized() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Logout user. |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | public function logout() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Scope to get only users from a given group / groups. |
||
| 231 | * |
||
| 232 | * @param UserQuery $query |
||
| 233 | * @param int|array $id |
||
| 234 | * |
||
| 235 | * @return UserQuery |
||
| 236 | */ |
||
| 237 | public function scopeFromGroup($query, $id) |
||
| 243 | } |
||
| 244 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state