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:
Complex classes like UsersData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UsersData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class UsersData extends Admin |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * For admin listing and search results |
||
| 19 | */ |
||
| 20 | use Traits\SearchController; |
||
| 21 | |||
| 22 | protected $template_path = 'cms/admin/usersdata/'; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * |
||
| 28 | * @param \Base $f3 |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | View Code Duplication | public function listing(\Base $f3) |
|
| 32 | { |
||
| 33 | $view = strtolower(trim(strip_tags($f3->get('REQUEST.view')))); |
||
| 34 | $view = empty($view) ? 'list.phtml' : $view . '.phtml'; |
||
| 35 | $f3->set('REQUEST.view', $view); |
||
| 36 | |||
| 37 | $f3->set('results', $this->getListingResults($f3, new Mappers\UsersData)); |
||
| 38 | |||
| 39 | $f3->set('breadcrumbs', [ |
||
| 40 | _('Admin') => 'admin', |
||
| 41 | _('Users') => 'admin_users_list', |
||
| 42 | _('Data') => 'admin_usersdata_list', |
||
| 43 | ]); |
||
| 44 | |||
| 45 | $f3->set('form', $f3->get('REQUEST')); |
||
| 46 | echo \View::instance()->render($this->template_path . $view); |
||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | * |
||
| 53 | * @param \Base $f3 |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function search(\Base $f3) |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * |
||
| 79 | * @param \Base $f3 |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function edit(\Base $f3) |
||
| 83 | { |
||
| 84 | $this->redirectLoggedOutUser(); |
||
| 85 | $this->csrf(); |
||
| 86 | |||
| 87 | if (false == $f3->get('isRoot')) { |
||
| 88 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 89 | return $f3->reroute('@admin'); |
||
| 90 | } |
||
| 91 | |||
| 92 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 93 | $usersModel = Models\Users::instance(); |
||
| 94 | $mapper = $usersModel->getDataMapper(); |
||
| 95 | $mapper->load(['uuid = ?', $uuid]); |
||
| 96 | |||
| 97 | if (null == $mapper->id) { |
||
| 98 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 99 | return $f3->reroute('@admin_users_lists'); |
||
| 100 | } |
||
| 101 | |||
| 102 | $f3->set('breadcrumbs', [ |
||
| 103 | _('Admin') => 'admin', |
||
| 104 | _('Users') => $this->url('@admin_users_search', [ |
||
| 105 | 'search' => $mapper->users_uuid, |
||
| 106 | 'search_fields' => 'uuid', |
||
| 107 | 'type' => 'exact', |
||
| 108 | ]), |
||
| 109 | _('Data') => $this->url('@admin_usersdata_search', [ |
||
| 110 | 'search' => $mapper->users_uuid, |
||
| 111 | 'search_fields' => 'users_uuid', |
||
| 112 | 'order' => 'key', |
||
| 113 | 'type' => 'exact', |
||
| 114 | ]), |
||
| 115 | _('Edit') => '', |
||
| 116 | ]); |
||
| 117 | |||
| 118 | $f3->set('form', $mapper->cast()); |
||
| 119 | echo \View::instance()->render($this->template_path . 'edit.phtml'); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * |
||
| 125 | * |
||
| 126 | * @param \Base $f3 |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public function editPost(\Base $f3) |
||
| 130 | { |
||
| 131 | $this->csrf('@admin_usersdata_list'); |
||
| 132 | $this->redirectLoggedOutUser(); |
||
| 133 | |||
| 134 | if (false == $f3->get('isRoot')) { |
||
| 135 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 136 | return $f3->reroute('@admin'); |
||
| 137 | } |
||
| 138 | |||
| 139 | $view = $this->template_path . 'edit.phtml'; |
||
| 140 | |||
| 141 | // get current user details |
||
| 142 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 143 | $usersModel = Models\Users::instance(); |
||
| 144 | $mapper = $usersModel->getDataMapper(); |
||
| 145 | $mapper->load(['uuid = ?', $uuid]); |
||
| 146 | |||
| 147 | if (null == $mapper->id) { |
||
| 148 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 149 | return $f3->reroute('@admin_usersdata_list'); |
||
| 150 | } |
||
| 151 | |||
| 152 | $f3->set('breadcrumbs', [ |
||
| 153 | _('Admin') => 'admin', |
||
| 154 | _('Users') => $this->url('@admin_users_search', [ |
||
| 155 | 'search' => $mapper->users_uuid, |
||
| 156 | 'search_fields' => 'uuid', |
||
| 157 | 'type' => 'exact', |
||
| 158 | ]), |
||
| 159 | _('Data') => $this->url('@admin_usersdata_search', [ |
||
| 160 | 'search' => $mapper->users_uuid, |
||
| 161 | 'search_fields' => 'users_uuid', |
||
| 162 | 'order' => 'key', |
||
| 163 | 'type' => 'exact', |
||
| 164 | ]), |
||
| 165 | _('Edit') => '', |
||
| 166 | ]); |
||
| 167 | |||
| 168 | // only allow updating of these fields |
||
| 169 | $data = $f3->get('REQUEST'); |
||
| 170 | $fields = [ |
||
| 171 | 'value' |
||
| 172 | ]; |
||
| 173 | |||
| 174 | // check input data has values set for the above fields |
||
| 175 | foreach ($fields as $k => $field) { |
||
| 176 | if (!array_key_exists($field, $data)) { |
||
| 177 | $data[$field] = null; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | // then remove any input data fields that aren't in the above fields |
||
| 181 | foreach ($data as $field => $v) { |
||
| 182 | if (!array_key_exists($field, $data) || empty($data[$field])) { |
||
| 183 | unset($data[$field]); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | // type check for filtering and validation |
||
| 188 | $fRules = ''; |
||
| 189 | switch ($mapper->type) { |
||
| 190 | case 'text': |
||
| 191 | case 'textarea': |
||
| 192 | $fRules = 'trim|sanitize_string'; |
||
| 193 | break; |
||
| 194 | |||
| 195 | case 'html': |
||
| 196 | case 'markdown': |
||
| 197 | case 'ini': |
||
| 198 | case 'yaml': |
||
| 199 | // trust raw input! |
||
| 200 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 201 | break; |
||
| 202 | |||
| 203 | case 'json': |
||
| 204 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 205 | break; |
||
| 206 | |||
| 207 | case 'email': |
||
| 208 | $fRules = 'sanitize_email'; |
||
| 209 | $vRules = 'valid_email'; |
||
| 210 | break; |
||
| 211 | |||
| 212 | case 'url': |
||
| 213 | $fRules = 'trim|sanitize_string'; |
||
| 214 | $vRules = 'valid_url'; |
||
| 215 | break; |
||
| 216 | |||
| 217 | case 'numeric': |
||
| 218 | case 'whole_number': |
||
| 219 | case 'integer': |
||
| 220 | case 'boolean': |
||
| 221 | View Code Duplication | case 'float': |
|
| 222 | $fRules = 'trim|sanitize_string'; |
||
| 223 | if ('float' == $mapper->type) { |
||
| 224 | $fRules .= '|sanitize_floats'; |
||
| 225 | } else { |
||
| 226 | $fRules = 'sanitize_numbers'; |
||
| 227 | } |
||
| 228 | $vRules = $mapper->type; |
||
| 229 | break; |
||
| 230 | |||
| 231 | case 'date': |
||
| 232 | $fRules = 'trim|sanitize_string'; |
||
| 233 | $vRules = $mapper->type; |
||
| 234 | break; |
||
| 235 | } |
||
| 236 | |||
| 237 | if (!empty($fRules)) { |
||
| 238 | $this->filterRules(['value' => $fRules]); |
||
| 239 | } |
||
| 240 | View Code Duplication | if (!empty($vRules)) { |
|
| 241 | $this->validationRules(['value' => $vRules]); |
||
| 242 | $errors = $this->validate(false, ['value' => $data['value']]); |
||
| 243 | if (true !== $errors) { |
||
| 244 | $this->notify(['warning' => $this->validationErrors($errors)]); |
||
| 245 | $f3->set('form', $mapper->cast()); |
||
| 246 | echo \View::instance()->render($this->template_path . 'edit.phtml'); |
||
| 247 | return; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // update required fields to check from ones which changed |
||
| 252 | // validate the entered data |
||
| 253 | $data['uuid'] = $uuid; |
||
| 254 | $mapper->copyfrom($data); |
||
| 255 | $mapper->validationRequired($fields); |
||
| 256 | $errors = $mapper->validate(false); |
||
| 257 | View Code Duplication | if (is_array($errors)) { |
|
| 258 | $this->notify(['warning' => $mapper->validationErrors($errors)]); |
||
| 259 | $f3->set('form', $f3->get('REQUEST')); |
||
| 260 | echo \View::instance()->render($view); |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | |||
| 264 | // no change, do nothing |
||
| 265 | if ($mapper->changed()) { |
||
| 266 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 267 | return $f3->reroute('@admin_usersdata_list'); |
||
| 268 | } |
||
| 269 | |||
| 270 | // reset usermapper and copy in valid data |
||
| 271 | $mapper->load(['uuid = ?', $data['uuid']]); |
||
| 272 | $mapper->copyfrom($data); |
||
| 273 | View Code Duplication | if ($mapper->save()) { |
|
| 274 | $this->notify(_('The account data was updated!'), 'success'); |
||
| 275 | } else { |
||
| 276 | $this->notify(_('Unable to update account data!'), 'error'); |
||
| 277 | $f3->set('form', $f3->get('REQUEST')); |
||
| 278 | echo \View::instance()->render($view); |
||
| 279 | return; |
||
| 280 | } |
||
| 281 | |||
| 282 | $f3->reroute('@admin_usersdata_search' . '?search=' . $mapper->uuid); |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * |
||
| 288 | * |
||
| 289 | * @param \Base $f3 |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | public function add(\Base $f3) |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * |
||
| 332 | * |
||
| 333 | * @param \Base $f3 |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | public function addPost(\Base $f3) |
||
| 337 | { |
||
| 338 | $this->csrf('@admin_usersdata_list'); |
||
| 339 | $this->redirectLoggedOutUser(); |
||
| 340 | |||
| 341 | if (false == $f3->get('isRoot')) { |
||
| 342 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 343 | return $f3->reroute('@admin'); |
||
| 344 | } |
||
| 345 | |||
| 346 | $view = $this->template_path . 'add.phtml'; |
||
| 347 | |||
| 348 | $users_uuid = $f3->get('REQUEST.users_uuid'); |
||
| 349 | $usersModel = Models\Users::instance(); |
||
| 350 | $mapper = $usersModel->getDataMapper(); |
||
| 351 | |||
| 352 | $f3->set('breadcrumbs', [ |
||
| 353 | _('Admin') => 'admin', |
||
| 354 | _('Users') => $this->url('@admin_users_search', [ |
||
| 355 | 'search' => $users_uuid, |
||
| 356 | 'search_fields' => 'uuid', |
||
| 357 | 'type' => 'exact', |
||
| 358 | ]), |
||
| 359 | _('Data') => $this->url('@admin_usersdata_search', [ |
||
| 360 | 'search' => $users_uuid, |
||
| 361 | 'search_fields' => 'users_uuid', |
||
| 362 | 'order' => 'key', |
||
| 363 | 'type' => 'exact', |
||
| 364 | ]), |
||
| 365 | _('Add') => '', |
||
| 366 | ]); |
||
| 367 | |||
| 368 | // only allow updating of these fields |
||
| 369 | $data = $f3->get('REQUEST'); |
||
| 370 | $fields = [ |
||
| 371 | 'users_uuid', 'key', 'value', 'type' |
||
| 372 | ]; |
||
| 373 | |||
| 374 | // check input data has values set for the above fields |
||
| 375 | foreach ($fields as $k => $field) { |
||
| 376 | if (!array_key_exists($field, $data)) { |
||
| 377 | $data[$field] = null; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | // then remove any input data fields that aren't in the above fields |
||
| 381 | foreach ($data as $field => $v) { |
||
| 382 | if (!in_array($field, $fields)) { |
||
| 383 | unset($data[$field]); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | // update required fields to check from ones which changed |
||
| 388 | // validate the entered data |
||
| 389 | $data['users_uuid'] = $users_uuid; |
||
| 390 | $mapper->copyfrom($data); |
||
| 391 | $mapper->validationRequired($fields); |
||
| 392 | $errors = $mapper->validate(false); |
||
| 393 | View Code Duplication | if (is_array($errors)) { |
|
| 394 | $this->notify(['warning' => $mapper->validationErrors($errors)]); |
||
| 395 | $f3->set('form', $f3->get('REQUEST')); |
||
| 396 | echo \View::instance()->render($view); |
||
| 397 | return; |
||
| 398 | } |
||
| 399 | |||
| 400 | // no change, do nothing |
||
| 401 | if (!$mapper->changed()) { |
||
| 402 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 403 | return $f3->reroute('@admin_usersdata_list'); |
||
| 404 | } |
||
| 405 | |||
| 406 | // reset usermapper and copy in valid data |
||
| 407 | $mapper->load(['uuid = ?', $mapper->uuid]); |
||
| 408 | $mapper->copyfrom($data); |
||
| 409 | View Code Duplication | if ($mapper->save()) { |
|
| 410 | $this->notify(_('The account data was updated!'), 'success'); |
||
| 411 | } else { |
||
| 412 | $this->notify(_('Unable to update account data!'), 'error'); |
||
| 413 | $f3->set('form', $f3->get('REQUEST')); |
||
| 414 | echo \View::instance()->render($view); |
||
| 415 | return; |
||
| 416 | } |
||
| 417 | |||
| 418 | $f3->reroute('@admin_usersdata_edit' . '?uuid=' . $mapper->uuid); |
||
| 419 | } |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * |
||
| 424 | * |
||
| 425 | * @param \Base $f3 |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function delete(\Base $f3) |
||
| 452 | |||
| 453 | } |
||
| 454 |