| Conditions | 9 |
| Paths | 48 |
| Total Lines | 53 |
| Code Lines | 37 |
| 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 |
||
| 36 | function get_comments_all($user, $guid, $limit, $offset, $lang) |
||
| 37 | { |
||
| 38 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
| 39 | if (!$user_entity) { |
||
| 40 | return "User was not found. Please try a different GUID, username, or email address"; |
||
| 41 | } |
||
| 42 | if (!$user_entity instanceof ElggUser) { |
||
| 43 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
| 44 | } |
||
| 45 | if (!elgg_is_logged_in()) { |
||
| 46 | login($user_entity); |
||
| 47 | } |
||
| 48 | |||
| 49 | $entity = get_entity($guid); |
||
| 50 | $subtype = 'nothing'; |
||
| 51 | if (!$entity) { |
||
| 52 | return "Discussion was not found. Please try a different GUID"; |
||
| 53 | } else if (elgg_instanceof($entity, "object", "groupforumtopic")){ |
||
| 54 | $subtype = 'discussion_reply'; |
||
| 55 | } else { |
||
| 56 | $subtype = 'comment'; |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | $all_replies = elgg_list_entities_from_metadata(array( |
||
| 61 | 'type' => 'object', |
||
| 62 | 'subtype' => $subtype, |
||
| 63 | 'container_guid' => $guid, |
||
| 64 | 'limit' => $limit, |
||
| 65 | 'offset' => $offset |
||
| 66 | )); |
||
| 67 | $replies = json_decode($all_replies); |
||
| 68 | $comments = array(); |
||
| 69 | foreach ($replies as $reply) { |
||
| 70 | $likes = elgg_get_annotations(array( |
||
| 71 | 'guid' => $reply->guid, |
||
| 72 | 'annotation_name' => 'likes' |
||
| 73 | )); |
||
| 74 | $reply->likes = count($likes); |
||
| 75 | |||
| 76 | $liked = elgg_get_annotations(array( |
||
| 77 | 'guid' => $reply->guid, |
||
| 78 | 'annotation_owner_guid' => $user_entity->guid, |
||
| 79 | 'annotation_name' => 'likes' |
||
| 80 | )); |
||
| 81 | $reply->liked = count($liked) > 0; |
||
| 82 | |||
| 83 | $reply->userDetails = get_user_block($reply->owner_guid, $lang); |
||
| 84 | $comments[] = $reply; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $comments; |
||
| 88 | } |
||
| 89 | |||
| 173 |