| Conditions | 15 |
| Paths | 49 |
| Total Lines | 43 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 28 | function refactorObject(array &$object, string $ruleName):void { |
||
| 29 | $rule = $this->ci->config->item("refactor_$ruleName"); |
||
| 30 | if ($rule == null) return; // No need to go further as rule doesn't exist. |
||
| 31 | // Unset |
||
| 32 | if (isset($rule['unset'])) { |
||
| 33 | $this->unset_values($object, $rule); |
||
| 34 | } |
||
| 35 | // Replace |
||
| 36 | if (isset($rule['replace'])) { |
||
| 37 | $this->replace_fields($object, $rule); |
||
| 38 | } |
||
| 39 | // Bools |
||
| 40 | if (isset($rule['bools'])) { |
||
| 41 | foreach($rule['bools'] as $boolKey) { |
||
| 42 | $object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true'; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | // Objects |
||
| 46 | if (isset($rule['objects'])) { |
||
| 47 | foreach($rule['objects'] as $field => $data) { |
||
| 48 | $ids = json_decode($object[$field]); |
||
| 49 | if (is_scalar($ids)) { |
||
| 50 | // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
||
| 51 | $this->ci->db->where($this->primaryKey, $ids); |
||
| 52 | $query = $this->ci->db->get($data['table']); |
||
| 53 | if ($query->num_rows() == 0) { |
||
| 54 | $object[$field] = json_encode (json_decode ("{}")); |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | $object[$field] = $query->result_array()[0]; |
||
| 58 | if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | $object[$field] = []; |
||
| 62 | foreach($ids as $id) { |
||
| 63 | $this->ci->db->where($this->primaryKey, $id); |
||
| 64 | $query = $this->ci->db->get($data['table']); |
||
| 65 | if ($query->num_rows() == 0) { |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | $object[$field][] = $query->result_array()[0]; |
||
| 69 | // Recursion |
||
| 70 | if (isset($data['refactor'])) $this->refactorObject($object[$field][count($object[$field]) - 1], $data['refactor']); |
||
| 71 | } |
||
| 99 |