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 |
||
| 15 | class Users extends Admin |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * For admin listing and search results |
||
| 19 | */ |
||
| 20 | use Traits\SearchController; |
||
| 21 | |||
| 22 | protected $template_path = 'cms/admin/users/'; |
||
| 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\Users)); |
||
| 38 | |||
| 39 | $f3->set('breadcrumbs', [ |
||
| 40 | _('Admin') => 'admin', |
||
| 41 | _('Users') => 'admin_users_list', |
||
| 42 | ]); |
||
| 43 | |||
| 44 | $f3->set('form', $f3->get('REQUEST')); |
||
| 45 | echo \View::instance()->render($this->template_path . $view); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * |
||
| 52 | * @param \Base $f3 |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function search(\Base $f3) |
|
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * |
||
| 77 | * @param \Base $f3 |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function edit(\Base $f3) |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * |
||
| 111 | * |
||
| 112 | * @param \Base $f3 |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function editPost(\Base $f3) |
||
| 116 | { |
||
| 117 | $this->csrf('@admin_users_list'); |
||
| 118 | $this->redirectLoggedOutUser(); |
||
| 119 | |||
| 120 | if (false == $f3->get('isRoot')) { |
||
| 121 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 122 | return $f3->reroute('@admin'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $view = $this->template_path . 'edit.phtml'; |
||
| 126 | |||
| 127 | $f3->set('breadcrumbs', [ |
||
| 128 | _('Admin') => 'admin', |
||
| 129 | _('Users') => 'admin_users_list', |
||
| 130 | _('Edit') => '', |
||
| 131 | ]); |
||
| 132 | |||
| 133 | // get current user details |
||
| 134 | $usersModel = Models\Users::instance(); |
||
| 135 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 136 | $usersMapper = $usersModel->getUserByUUID($uuid); |
||
| 137 | View Code Duplication | if (null == $usersMapper->id) { |
|
| 138 | $this->notify(_('The account no longer exists!'), 'error'); |
||
| 139 | $f3->set('form', $f3->get('REQUEST')); |
||
| 140 | echo \View::instance()->render('user/account.phtml'); |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | $oldUserMapper = clone $usersMapper; |
||
| 144 | |||
| 145 | // only allow updating of these fields |
||
| 146 | $data = $f3->get('REQUEST'); |
||
| 147 | $fields = [ |
||
| 148 | 'email', |
||
| 149 | 'password', |
||
| 150 | 'firstname', |
||
| 151 | 'lastname', |
||
| 152 | 'password_question', |
||
| 153 | 'password_answer', |
||
| 154 | 'scopes', |
||
| 155 | 'status', |
||
| 156 | ]; |
||
| 157 | |||
| 158 | // check input data has values set for the above fields |
||
| 159 | foreach ($fields as $k => $field) { |
||
| 160 | if (!array_key_exists($field, $data)) { |
||
| 161 | $data[$field] = null; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | // then remove any input data fields that aren't in the above fields |
||
| 165 | foreach ($data as $field => $v) { |
||
| 166 | if (!in_array($field, $fields)) { |
||
| 167 | unset($data[$field]); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | // is this a password change? if so, check they match |
||
| 172 | $str = Helpers\Str::instance(); |
||
| 173 | $password = $f3->get('REQUEST.password'); |
||
| 174 | $confirm_password = $f3->get('REQUEST.confirm_password'); |
||
| 175 | View Code Duplication | if (!empty($password) || !empty($confirm_password)) { |
|
| 176 | if ($password !== $confirm_password) { |
||
| 177 | $this->notify(_('That password and confirm password must match!'), 'warning'); |
||
| 178 | $f3->set('form', $f3->get('REQUEST')); |
||
| 179 | echo \View::instance()->render($view); |
||
| 180 | return; |
||
| 181 | } elseif ($str->passwordVerify($usersMapper->password, $password)) { |
||
| 182 | $this->notify(_('The new password and old password are the same!'), 'warning'); |
||
| 183 | $f3->set('form', $f3->get('REQUEST')); |
||
| 184 | echo \View::instance()->render($view); |
||
| 185 | return; |
||
| 186 | } else { |
||
| 187 | // set new hashed password |
||
| 188 | $data['password'] = $str->password($password); |
||
| 189 | } |
||
| 190 | } else { |
||
| 191 | // same password |
||
| 192 | $data['password'] = $usersMapper->password; |
||
| 193 | } |
||
| 194 | |||
| 195 | // check if email address change that email isn't taken |
||
| 196 | $email = $f3->get('REQUEST.email'); |
||
| 197 | View Code Duplication | if ($usersMapper->email !== $email) { |
|
| 198 | $usersMapper->load(['email = ?', $email]); |
||
| 199 | if ($usersMapper->email == $email) { |
||
| 200 | $this->notify(sprintf(_('The email address %s is already in use!'), $email), 'warning'); |
||
| 201 | $f3->set('form', $f3->get('REQUEST')); |
||
| 202 | echo \View::instance()->render($view); |
||
| 203 | return; |
||
| 204 | } else { |
||
| 205 | // new email |
||
| 206 | $data['email'] = $email; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | // update required fields to check from ones which changed |
||
| 211 | // validate the entered data |
||
| 212 | $data['uuid'] = $uuid; |
||
| 213 | $usersMapper->copyfrom($data); |
||
| 214 | $usersMapper->validationRequired($fields); |
||
| 215 | $errors = $usersMapper->validate(false); |
||
| 216 | View Code Duplication | if (is_array($errors)) { |
|
| 217 | $this->notify(['warning' => $usersMapper->validationErrors($errors)]); |
||
| 218 | $f3->set('form', $f3->get('REQUEST')); |
||
| 219 | echo \View::instance()->render($view); |
||
| 220 | return; |
||
| 221 | } |
||
| 222 | |||
| 223 | // no change, do nothing |
||
| 224 | View Code Duplication | if ($usersMapper->cast() === $oldUserMapper->cast()) { |
|
| 225 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 226 | $f3->set('form', $f3->get('REQUEST')); |
||
| 227 | echo \View::instance()->render($view); |
||
| 228 | return; |
||
| 229 | } |
||
| 230 | |||
| 231 | // reset usermapper and copy in valid data |
||
| 232 | $usersMapper->load(['uuid = ?', $data['uuid']]); |
||
| 233 | $usersMapper->copyfrom($data); |
||
| 234 | View Code Duplication | if ($usersMapper->save()) { |
|
| 235 | $this->notify(_('The account was updated!'), 'success'); |
||
| 236 | } else { |
||
| 237 | $this->notify(_('Unable to update your account!'), 'error'); |
||
| 238 | $f3->set('form', $f3->get('REQUEST')); |
||
| 239 | echo \View::instance()->render($view); |
||
| 240 | return; |
||
| 241 | } |
||
| 242 | |||
| 243 | $f3->reroute('@admin_users_search' . '?search=' . $usersMapper->uuid); |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * |
||
| 249 | * |
||
| 250 | * @param \Base $f3 |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public function delete(\Base $f3) |
||
| 278 | |||
| 279 | } |
||
| 280 |