Conditions | 29 |
Paths | 1060 |
Total Lines | 85 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
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 |
||
106 | function run(array &$object, $ruleName):void |
||
107 | { |
||
108 | if ($object == null) return; |
||
109 | // Reolve Rules. |
||
110 | if (is_scalar($ruleName)) { |
||
111 | $rule = $this->ci->config->item("refactor_$ruleName"); |
||
112 | } else { |
||
113 | // Rule was probablt passed in as an array (associative) and we support |
||
114 | // that. |
||
115 | $rule = is_array($ruleName) ? $ruleName : null; |
||
116 | } |
||
117 | |||
118 | if ($rule == null) return; // No need to go further as rule doesn't exist. |
||
119 | // Keep |
||
120 | if (isset($rule['keep'])) { |
||
121 | $keys = array_keys($object); |
||
122 | for ($x = 0; $x < count($object); $x++) { |
||
123 | if (!in_array($keys[$X], $rule['keep'])) { |
||
124 | unset($object[$keys[$x]]); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | // Unset |
||
129 | if (isset($rule['unset'])) { |
||
130 | $this->unset_values($object, $rule); |
||
131 | } |
||
132 | // Replace |
||
133 | if (isset($rule['replace'])) { |
||
134 | $this->replace_fields($object, $rule); |
||
135 | } |
||
136 | // Bools |
||
137 | if (isset($rule['bools'])) { |
||
138 | foreach($rule['bools'] as $boolKey) { |
||
139 | $object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true'; |
||
140 | } |
||
141 | } |
||
142 | // Cast |
||
143 | if (isset($rule['cast'])) { |
||
144 | $this->cast_fields($object, $rule); |
||
145 | } |
||
146 | // Inflate |
||
147 | if (isset($rule['inflate'])) { |
||
148 | foreach($rule['inflate'] as $field => $data) { |
||
149 | $ids = json_decode($object[$field], true); |
||
150 | if (is_scalar($ids)) { |
||
151 | // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
||
152 | $this->ci->db->where($this->primaryKey, $ids); |
||
153 | $query = $this->ci->db->get($data['table']); |
||
154 | if ($query->num_rows() == 0) { |
||
155 | $object[$field] = json_encode (json_decode ("{}")); |
||
156 | continue; |
||
157 | } |
||
158 | $object[$field] = $query->result_array()[0]; |
||
159 | if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
||
160 | continue; |
||
161 | } |
||
162 | if (isset($data['path'])) { |
||
163 | if ($ids == null) return; |
||
164 | $object[$field] = $ids; |
||
165 | $this->ci->jsonp->parse($object[$field]); |
||
166 | if (is_array($object[$field])) { |
||
167 | $refs = $this->ci->jsonp->get_reference($data['path']); |
||
168 | for ($x = 0; $x < count($refs); $x++) { |
||
169 | $refs[$x] = $this->inflate_value($data['table'], $refs[$x]); |
||
170 | // Recursion |
||
171 | if (isset($data['refactor'])) $this->run($refs[$x], $data['refactor']); |
||
172 | } |
||
173 | } else { |
||
174 | $this->ci->jsonp->set($data['path'], $this->inflate_value($data['table'], $ids)); |
||
175 | // Recursion |
||
176 | if (isset($data['refactor'])) $this->run($this->ci->jsonp->get_reference($data['path']), $data['refactor']); |
||
177 | } |
||
178 | return; |
||
179 | } |
||
180 | $object[$field] = []; |
||
181 | if ($ids == null) return; |
||
182 | foreach($ids as $id) { |
||
183 | $this->ci->db->where($this->primaryKey, $id); |
||
184 | $query = $this->ci->db->get($data['table']); |
||
185 | if ($query->num_rows() == 0) { |
||
186 | continue; |
||
187 | } |
||
188 | $object[$field][] = $query->result_array()[0]; |
||
189 | // Recursion |
||
190 | if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
||
191 | } |
||
244 |