| Conditions | 8 |
| Paths | 32 |
| Total Lines | 53 |
| Code Lines | 32 |
| 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_opportunity($user, $guid, $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 | |||
| 46 | if (!elgg_is_logged_in()) { |
||
| 47 | login($user_entity); |
||
| 48 | } |
||
| 49 | |||
| 50 | $entity = get_entity($guid); |
||
| 51 | if (!$entity) { |
||
| 52 | return "Opportunity was not found. Please try a different GUID"; |
||
| 53 | } |
||
| 54 | if (!elgg_instanceof($entity, 'object', 'mission')) { |
||
| 55 | return "Invalid opportunity. Please try a different GUID"; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | $opportunities = elgg_list_entities(array( |
||
| 61 | 'type' => 'object', |
||
| 62 | 'subtype' => 'mission', |
||
| 63 | 'guid' => $guid |
||
| 64 | )); |
||
| 65 | $opportunity = json_decode($opportunities)[0]; |
||
| 66 | |||
| 67 | $opportunity->title = gc_explode_translation($opportunity->title, $lang); |
||
| 68 | |||
| 69 | $likes = elgg_get_annotations(array( |
||
| 70 | 'guid' => $opportunity->guid, |
||
| 71 | 'annotation_name' => 'likes' |
||
| 72 | )); |
||
| 73 | $opportunity->likes = count($likes); |
||
| 74 | |||
| 75 | $liked = elgg_get_annotations(array( |
||
| 76 | 'guid' => $opportunity->guid, |
||
| 77 | 'annotation_owner_guid' => $user_entity->guid, |
||
| 78 | 'annotation_name' => 'likes' |
||
| 79 | )); |
||
| 80 | $opportunity->liked = count($liked) > 0; |
||
| 81 | |||
| 82 | $opportunity->comments = get_entity_comments($opportunity->guid); |
||
| 83 | |||
| 84 | $opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang); |
||
| 85 | $opportunity->description = gc_explode_translation($opportunity->description, $lang); |
||
| 86 | |||
| 87 | return $opportunity; |
||
| 88 | } |
||
| 89 | |||
| 166 |