| Conditions | 9 |
| Paths | 42 |
| Total Lines | 52 |
| Code Lines | 33 |
| 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 |
||
| 27 | private function save(\Base $f3, array $prohibitedFields = []) |
||
| 28 | { |
||
| 29 | // do not allow request to define these fields: |
||
| 30 | $data = $f3->get('REQUEST'); |
||
| 31 | foreach ($prohibitedFields as $field) { |
||
| 32 | if (array_key_exists($field, $data)) { |
||
| 33 | unset($data[$field]); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | // load pre-existing value |
||
| 38 | $db = \Registry::get('db'); |
||
| 39 | $m = $this->getMapper(); |
||
| 40 | if ($f3->get('VERB') == 'PUT') { |
||
| 41 | $m->load(['uuid = ?', $data['uuid']]); |
||
| 42 | } else { |
||
| 43 | $m->load(['users_uuid = ? AND ' . $db->quotekey('key') . ' = ?', $data['users_uuid'], $data['key']]); |
||
| 44 | } |
||
| 45 | |||
| 46 | // copy data and validate |
||
| 47 | $oldMapper = clone($m); |
||
| 48 | $m->copyfrom($data); |
||
| 49 | $m->validationRequired([ |
||
| 50 | 'users_uuid', 'key', 'name', 'query' |
||
| 51 | ]); |
||
| 52 | $errors = $m->validate(false); |
||
| 53 | if (true !== $errors) { |
||
| 54 | foreach ($errors as $error) { |
||
| 55 | $this->setOAuthError('invalid_request'); |
||
| 56 | $this->failure($error['field'], $error['rule']); |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | // load in original data and then replace for save |
||
| 60 | if (!$m->validateSave()) { |
||
| 61 | $this->setOAuthError('invalid_request'); |
||
| 62 | $this->failure('error', 'Unable to update object.'); |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->audit([ |
||
| 67 | 'users_uuid' => $m->users_uuid, |
||
| 68 | 'actor' => $m->client_id, |
||
| 69 | 'event' => 'Report Updated via API', |
||
| 70 | 'old' => $oldMapper->cast(), |
||
| 71 | 'new' => $m->cast() |
||
| 72 | ]); |
||
| 73 | |||
| 74 | // return raw data for object? |
||
| 75 | $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view'); |
||
| 76 | $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields')); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 160 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.