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