| Conditions | 28 |
| Paths | 2648 |
| Total Lines | 141 |
| Code Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 162 | public function editPost(\Base $f3) |
||
| 163 | { |
||
| 164 | $this->csrf('@admin_config_list'); |
||
| 165 | $this->redirectLoggedOutUser(); |
||
| 166 | |||
| 167 | if (false == $f3->get('is_root')) { |
||
| 168 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 169 | return $f3->reroute('@admin'); |
||
| 170 | } |
||
| 171 | |||
| 172 | $view = $this->template_path . 'edit.phtml'; |
||
| 173 | |||
| 174 | $f3->set('breadcrumbs', [ |
||
| 175 | _('Admin') => 'admin', |
||
| 176 | _('Config') => 'admin_config_list', |
||
| 177 | _('Edit') => '', |
||
| 178 | ]); |
||
| 179 | |||
| 180 | // get current user details |
||
| 181 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 182 | $mapper = new Mappers\ConfigData; |
||
| 183 | |||
| 184 | $mapper->load(['uuid = ?', $uuid]); |
||
| 185 | |||
| 186 | if (null == $mapper->id) { |
||
| 187 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 188 | return $f3->reroute('@admin_config_list'); |
||
| 189 | } |
||
| 190 | |||
| 191 | $oldMapper = clone $mapper; |
||
| 192 | |||
| 193 | // only allow updating of these fields |
||
| 194 | $data = $f3->get('REQUEST'); |
||
| 195 | $fields = [ |
||
| 196 | 'value', 'options', 'description' |
||
| 197 | ]; |
||
| 198 | |||
| 199 | // check input data has values set for the above fields |
||
| 200 | foreach ($fields as $k => $field) { |
||
| 201 | if (!array_key_exists($field, $data)) { |
||
| 202 | $data[$field] = null; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | // then remove any input data fields that aren't in the above fields |
||
| 206 | foreach ($data as $field => $v) { |
||
| 207 | if (!in_array($field, $fields)) { |
||
| 208 | unset($data[$field]); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | // type check for filtering and validation |
||
| 213 | $fRules = 'trim|sanitize_string'; |
||
| 214 | switch ($mapper->type) { |
||
| 215 | case 'text': |
||
| 216 | case 'textarea': |
||
| 217 | break; |
||
| 218 | |||
| 219 | case 'html': |
||
| 220 | case 'markdown': |
||
| 221 | case 'ini': |
||
| 222 | case 'yaml': |
||
| 223 | // trust raw input! |
||
| 224 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 225 | $fRules = ''; |
||
| 226 | break; |
||
| 227 | |||
| 228 | case 'json': |
||
| 229 | $data['value'] = $f3->get('REQUEST_UNCLEAN.value'); |
||
| 230 | $fRules = 'valid_json_String'; |
||
|
|
|||
| 231 | |||
| 232 | case 'email': |
||
| 233 | $fRules = 'sanitize_email'; |
||
| 234 | $vRules = 'valid_email'; |
||
| 235 | break; |
||
| 236 | |||
| 237 | case 'url': |
||
| 238 | $vRules = 'valid_url'; |
||
| 239 | break; |
||
| 240 | |||
| 241 | case 'numeric': |
||
| 242 | case 'whole_number': |
||
| 243 | case 'integer': |
||
| 244 | case 'boolean': |
||
| 245 | case 'float': |
||
| 246 | if ('float' == $mapper->type) { |
||
| 247 | $fRules .= 'sanitize_floats'; |
||
| 248 | } else { |
||
| 249 | $fRules = 'sanitize_numbers'; |
||
| 250 | } |
||
| 251 | $vRules = $mapper->type; |
||
| 252 | break; |
||
| 253 | |||
| 254 | case 'date': |
||
| 255 | $vRules = $mapper->type; |
||
| 256 | break; |
||
| 257 | } |
||
| 258 | |||
| 259 | if (!empty($fRules)) { |
||
| 260 | $this->filterRules(['value' => $fRules]); |
||
| 261 | } |
||
| 262 | if (!empty($vRules)) { |
||
| 263 | $this->validationRules(['value' => $vRules]); |
||
| 264 | $errors = $this->validate(false, ['value' => $data['value']]); |
||
| 265 | if (true !== $errors) { |
||
| 266 | $this->notify(['warning' => $this->validationErrors($errors)]); |
||
| 267 | $f3->set('form', $mapper->cast()); |
||
| 268 | echo \View::instance()->render($this->template_path . 'edit.phtml'); |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | // update required fields to check from ones which changed |
||
| 274 | // validate the entered data |
||
| 275 | $data['uuid'] = $uuid; |
||
| 276 | $mapper->copyfrom($data); |
||
| 277 | |||
| 278 | // no change, do nothing |
||
| 279 | if ($mapper->cast() === $oldMapper->cast()) { |
||
| 280 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 281 | return $f3->reroute('@admin_config_list'); |
||
| 282 | } |
||
| 283 | |||
| 284 | // reset usermapper and copy in valid data |
||
| 285 | $mapper->load(['uuid = ?', $data['uuid']]); |
||
| 286 | $mapper->copyfrom($data); |
||
| 287 | if ($mapper->validateSave()) { |
||
| 288 | $this->audit([ |
||
| 289 | 'event' => 'Config Data Updated', |
||
| 290 | 'old' => $oldMapper->cast(), |
||
| 291 | 'new' => $mapper->cast() |
||
| 292 | ]); |
||
| 293 | $this->notify(_('The config data was updated!'), 'success'); |
||
| 294 | } else { |
||
| 295 | $this->notify(_('Unable to update config data!'), 'error'); |
||
| 296 | $f3->set('form', $f3->get('REQUEST')); |
||
| 297 | echo \View::instance()->render($view); |
||
| 298 | return; |
||
| 299 | } |
||
| 300 | |||
| 301 | $f3->reroute('@admin_config_search' . '?search=' . $mapper->uuid); |
||
| 302 | } |
||
| 303 | |||
| 429 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.