Conditions | 26 |
Paths | 2436 |
Total Lines | 135 |
Lines | 25 |
Ratio | 18.52 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
190 | function query_the_posts($user, $password, $object, $query, $group, $guid, $limit, $offset, $from_date, $to_date, $lang) |
||
191 | { |
||
192 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
193 | if (!$user_entity) { |
||
194 | return "User was not found. Please try a different GUID, username, or email address"; |
||
195 | } |
||
196 | if (!$user_entity instanceof ElggUser) { |
||
197 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
198 | } |
||
199 | |||
200 | $valid = elgg_authenticate($user_entity->username, $password); |
||
201 | |||
202 | $type = "object"; |
||
203 | $subtype = ""; |
||
204 | switch ($object) { |
||
205 | case "blog": |
||
206 | $subtype = "blog"; |
||
207 | break; |
||
208 | case "discussion": |
||
209 | $subtype = "groupforumtopic"; |
||
210 | break; |
||
211 | case "event": |
||
212 | $subtype = "event_calendar"; |
||
213 | break; |
||
214 | case "group": |
||
215 | $type = "group"; |
||
216 | break; |
||
217 | case "opportunity": |
||
218 | $subtype = "mission"; |
||
219 | break; |
||
220 | case "wire": |
||
221 | $subtype = "thewire"; |
||
222 | break; |
||
223 | default: |
||
224 | return "Please use one of the following object types: 'blog', 'discussion', 'event', 'group', 'opportunity', 'wire'"; |
||
225 | break; |
||
|
|||
226 | } |
||
227 | |||
228 | $data = "Username/password combination is not correct."; |
||
229 | if ($valid === true) { |
||
230 | if (!elgg_is_logged_in()) { |
||
231 | login($user_entity); |
||
232 | } |
||
233 | |||
234 | if ($guid) { |
||
235 | |||
236 | $data = json_decode(elgg_list_entities(array( |
||
237 | 'type' => $type, |
||
238 | 'subtype' => $subtype, |
||
239 | 'guid' => $guid |
||
240 | ))); |
||
241 | |||
242 | $replies = json_decode(elgg_list_entities(array( |
||
243 | 'type' => 'object', |
||
244 | 'subtype' => 'comment', |
||
245 | 'container_guid' => $item->guid |
||
246 | ))); |
||
247 | if( count($replies) > 0 ){ |
||
248 | $data[0]->replies = $replies; |
||
249 | } |
||
250 | |||
251 | } else { |
||
252 | |||
253 | $db_prefix = elgg_get_config('dbprefix'); |
||
254 | |||
255 | $params = array( |
||
256 | 'type' => $type, |
||
257 | 'subtype' => $subtype, |
||
258 | 'limit' => $limit, |
||
259 | 'offset' => $offset |
||
260 | ); |
||
261 | |||
262 | if ($query) { |
||
263 | if ($object == "group") { |
||
264 | $params['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid"); |
||
265 | $params['wheres'] = array("(ge.name LIKE '%" . $query . "%' OR ge.description LIKE '%" . $query . "%')"); |
||
266 | View Code Duplication | } else { |
|
267 | $params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid"); |
||
268 | $params['wheres'] = array("(oe.title LIKE '%" . $query . "%' OR oe.description LIKE '%" . $query . "%')"); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | if ($group) { |
||
273 | $params['container_guid'] = $group; |
||
274 | } |
||
275 | |||
276 | View Code Duplication | if ($from_date) { |
|
277 | $params['joins'] = array("JOIN {$db_prefix}entities fd ON e.guid = fd.guid"); |
||
278 | $params['wheres'] = array("(fd.time_updated >= " . strtotime($from_date) . ")"); |
||
279 | } |
||
280 | View Code Duplication | if ($to_date) { |
|
281 | $params['joins'] = array("JOIN {$db_prefix}entities td ON e.guid = td.guid"); |
||
282 | $params['wheres'] = array("(td.time_updated <= " . strtotime($to_date) . ")"); |
||
283 | } |
||
284 | |||
285 | $ia = elgg_set_ignore_access(true); |
||
286 | $data = json_decode(elgg_list_entities_from_metadata($params)); |
||
287 | |||
288 | if( $object == "discussion" ){ |
||
289 | foreach ($data as $discussion) { |
||
290 | $replies = json_decode(elgg_list_entities_from_metadata(array( |
||
291 | 'type' => 'object', |
||
292 | 'subtype' => 'discussion_reply', |
||
293 | 'container_guid' => $discussion->guid |
||
294 | ))); |
||
295 | |||
296 | if( count($replies) > 0 ){ |
||
297 | $replies = array_reverse($replies); |
||
298 | |||
299 | $discussionsArray = array(); |
||
300 | foreach ($replies as $reply) { |
||
301 | $discussionsArray[] = $reply; |
||
302 | } |
||
303 | $discussion->replies = $discussionsArray; |
||
304 | } |
||
305 | } |
||
306 | View Code Duplication | } else { |
|
307 | foreach ($data as $item) { |
||
308 | $replies = json_decode(elgg_list_entities(array( |
||
309 | 'type' => 'object', |
||
310 | 'subtype' => 'comment', |
||
311 | 'container_guid' => $item->guid |
||
312 | ))); |
||
313 | |||
314 | if( count($replies) > 0 ){ |
||
315 | $item->replies = $replies; |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | elgg_set_ignore_access($ia); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | return $data; |
||
324 | } |
||
325 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.