Conditions | 8 |
Paths | 24 |
Total Lines | 53 |
Code Lines | 34 |
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 |
||
37 | function get_event($user, $guid, $lang) |
||
38 | { |
||
39 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
40 | if (!$user_entity) { |
||
41 | return "User was not found. Please try a different GUID, username, or email address"; |
||
42 | } |
||
43 | if (!$user_entity instanceof ElggUser) { |
||
44 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
45 | } |
||
46 | |||
47 | $entity = get_entity($guid); |
||
48 | if (!$entity) { |
||
49 | return "Event was not found. Please try a different GUID"; |
||
50 | } |
||
51 | if (!$entity->type !== "event_calendar") { |
||
52 | return "Invalid event. Please try a different GUID"; |
||
53 | } |
||
54 | |||
55 | if (!elgg_is_logged_in()) { |
||
56 | login($user_entity); |
||
57 | } |
||
58 | |||
59 | $events = elgg_list_entities(array( |
||
60 | 'type' => 'object', |
||
61 | 'subtype' => 'event_calendar', |
||
62 | 'guid' => $guid |
||
63 | )); |
||
64 | $event = json_decode($events)[0]; |
||
65 | |||
66 | $likes = elgg_get_annotations(array( |
||
67 | 'guid' => $event->guid, |
||
68 | 'annotation_name' => 'likes' |
||
69 | )); |
||
70 | $event->likes = count($likes); |
||
71 | |||
72 | $liked = elgg_get_annotations(array( |
||
73 | 'guid' => $event->guid, |
||
74 | 'annotation_owner_guid' => $user_entity->guid, |
||
75 | 'annotation_name' => 'likes' |
||
76 | )); |
||
77 | $event->liked = count($liked) > 0; |
||
78 | |||
79 | $event->title = gc_explode_translation($event->title, $lang); |
||
80 | $event->description = gc_explode_translation($event->description, $lang); |
||
81 | |||
82 | $event->userDetails = get_user_block($event->owner_guid, $lang); |
||
83 | |||
84 | $eventObj = get_entity($event->guid); |
||
85 | $event->startDate = date("Y-m-d H:i:s", $eventObj->start_date); |
||
86 | $event->endDate = date("Y-m-d H:i:s", $eventObj->end_date); |
||
87 | |||
88 | return $event; |
||
89 | } |
||
90 | |||
157 |