| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 216 |
| Code Lines | 139 |
| Lines | 132 |
| Ratio | 61.11 % |
| 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 |
||
| 21 | function get_newsfeedtest($user, $limit, $offset, $lang) |
||
| 22 | { |
||
| 23 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
| 24 | if (!$user_entity) { |
||
| 25 | return "User was not found. Please try a different GUID, username, or email address"; |
||
| 26 | } |
||
| 27 | if (!$user_entity instanceof ElggUser) { |
||
| 28 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
| 29 | } |
||
| 30 | |||
| 31 | if (!elgg_is_logged_in()) { |
||
| 32 | login($user_entity); |
||
| 33 | } |
||
| 34 | |||
| 35 | $db_prefix = elgg_get_config('dbprefix'); |
||
| 36 | |||
| 37 | View Code Duplication | if ($user_entity) { |
|
| 38 | // check if user exists and has friends or groups |
||
| 39 | $hasfriends = $user_entity->getFriends(); |
||
| 40 | $hasgroups = $user_entity->getGroups(); |
||
| 41 | if ($hasgroups) { |
||
| 42 | // loop through group guids |
||
| 43 | $groups = $user_entity->getGroups(array('limit'=>0)); |
||
| 44 | $group_guids = array(); |
||
| 45 | foreach ($groups as $group) { |
||
| 46 | $group_guids[] = $group->getGUID(); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } else { |
||
| 50 | $hasfriends = false; |
||
| 51 | $hasgroups = false; |
||
| 52 | $group_guids = array(); |
||
| 53 | } |
||
| 54 | |||
| 55 | $actionTypes = array('comment', 'create', 'join', 'update', 'friend', 'reply'); |
||
| 56 | |||
| 57 | View Code Duplication | if (!$hasgroups) { |
|
| 58 | if(!$hasfriends) { |
||
| 59 | // no friends and no groups :( |
||
| 60 | $activity = ''; |
||
| 61 | } else { |
||
| 62 | // has friends but no groups |
||
| 63 | $optionsf = array(); |
||
| 64 | $optionsf['relationship_guid'] = $user_entity->guid; |
||
| 65 | $optionsf['relationship'] = 'friend'; |
||
| 66 | $optionsf['pagination'] = true; |
||
| 67 | |||
| 68 | // turn off friend connections |
||
| 69 | // remove friend connections from action types |
||
| 70 | // load user's preference |
||
| 71 | $filteredItems = array($user_entity->colleagueNotif); |
||
| 72 | // filter out preference |
||
| 73 | $optionsf['action_types'] = array_diff($actionTypes, $filteredItems); |
||
| 74 | |||
| 75 | $activity = json_decode(newsfeed_list_river($optionsf)); |
||
| 76 | } |
||
| 77 | } elseif (!$hasfriends) { |
||
| 78 | // if no friends but groups |
||
| 79 | $guids_in = implode(',', array_unique(array_filter($group_guids))); |
||
|
|
|||
| 80 | |||
| 81 | // display created content and replies and comments |
||
| 82 | $optionsg = array(); |
||
| 83 | $optionsg['wheres'] = array("( oe.container_guid IN({$guids_in}) OR te.container_guid IN({$guids_in}) )"); |
||
| 84 | $optionsg['pagination'] = true; |
||
| 85 | $activity = json_decode(newsfeed_list_river($optionsg)); |
||
| 86 | } else { |
||
| 87 | // if friends and groups :3 |
||
| 88 | // turn off friend connections |
||
| 89 | // remove friend connections from action types |
||
| 90 | // load user's preference |
||
| 91 | $filteredItems = array($user_entity->colleagueNotif); |
||
| 92 | // filter out preference |
||
| 93 | $optionsfg = array(); |
||
| 94 | $optionsfg['action_types'] = array_diff($actionTypes, $filteredItems); |
||
| 95 | |||
| 96 | $guids_in = implode(',', array_unique(array_filter($group_guids))); |
||
| 97 | |||
| 98 | // Groups + Friends activity query |
||
| 99 | // This query grabs new created content and comments and replies in the groups the user is a member of *** te.container_guid grabs comments and replies |
||
| 100 | $optionsfg['wheres'] = array( |
||
| 101 | "( oe.container_guid IN({$guids_in}) |
||
| 102 | OR te.container_guid IN({$guids_in}) ) |
||
| 103 | OR rv.subject_guid IN (SELECT guid_two FROM {$db_prefix}entity_relationships WHERE guid_one=$user_entity->guid AND relationship='friend')" |
||
| 104 | ); |
||
| 105 | $optionsfg['pagination'] = true; |
||
| 106 | $activity = json_decode(newsfeed_list_river($optionsfg)); |
||
| 107 | } |
||
| 108 | |||
| 109 | foreach ($activity as $event) { |
||
| 110 | $subject = get_user($event->subject_guid); |
||
| 111 | $object = get_entity($event->object_guid); |
||
| 112 | $event->userDetails = get_user_block($event->subject_guid, $lang); |
||
| 113 | |||
| 114 | $likes = elgg_get_annotations(array( |
||
| 115 | 'guid' => $event->object_guid, |
||
| 116 | 'annotation_name' => 'likes' |
||
| 117 | )); |
||
| 118 | $event->likes = count($likes); |
||
| 119 | |||
| 120 | $liked = elgg_get_annotations(array( |
||
| 121 | 'guid' => $event->object_guid, |
||
| 122 | 'annotation_owner_guid' => $user_entity->guid, |
||
| 123 | 'annotation_name' => 'likes' |
||
| 124 | )); |
||
| 125 | $event->liked = count($liked) > 0; |
||
| 126 | |||
| 127 | if ($object->description) { |
||
| 128 | $object->description = str_replace("<p> </p>", '', $object->description); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($object instanceof ElggUser) { |
||
| 132 | $event->object = get_user_block($event->object_guid, $lang); |
||
| 133 | $event->object['type'] = 'user'; |
||
| 134 | View Code Duplication | } elseif ($object instanceof ElggWire) { |
|
| 135 | $event->object['type'] = 'wire'; |
||
| 136 | $event->object['wire'] = wire_filter($object->description); |
||
| 137 | |||
| 138 | $thread_id = $object->wire_thread; |
||
| 139 | $reshare = $object->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0]; |
||
| 140 | |||
| 141 | $url = ""; |
||
| 142 | if (!empty($reshare)) { |
||
| 143 | $url = $reshare->getURL(); |
||
| 144 | } |
||
| 145 | |||
| 146 | $text = ""; |
||
| 147 | if (!empty($reshare->title)) { |
||
| 148 | $text = $reshare->title; |
||
| 149 | } elseif (!empty($reshare->name)) { |
||
| 150 | $text = $reshare->name; |
||
| 151 | } elseif (!empty($reshare->description)) { |
||
| 152 | $text = elgg_get_excerpt($reshare->description, 140); |
||
| 153 | } |
||
| 154 | |||
| 155 | $event->shareURL = $url; |
||
| 156 | $event->shareText = gc_explode_translation($text, $lang); |
||
| 157 | } elseif ($object instanceof ElggGroup) { |
||
| 158 | $event->object['type'] = 'group'; |
||
| 159 | $event->object['name'] = gc_explode_translation($object->name, $lang); |
||
| 160 | $event->object['description'] = gc_explode_translation($object->name, $lang); |
||
| 161 | |||
| 162 | if (is_callable(array($object, 'getURL'))) { |
||
| 163 | $event->object['url'] = $object->getURL(); |
||
| 164 | } |
||
| 165 | } elseif ($object instanceof ElggDiscussionReply) { |
||
| 166 | $event->object['type'] = 'discussion-reply'; |
||
| 167 | $original_discussion = get_entity($object->container_guid); |
||
| 168 | $event->object['name'] = gc_explode_translation($original_discussion->title, $lang); |
||
| 169 | $event->object['description'] = gc_explode_translation($object->description, $lang); |
||
| 170 | |||
| 171 | $group = get_entity($original_discussion->container_guid); |
||
| 172 | $event->object['group_guid'] = $group->guid; |
||
| 173 | $event->object['group_title'] = gc_explode_translation($group->title, $lang); |
||
| 174 | |||
| 175 | if (is_callable(array($original_discussion, 'getURL'))) { |
||
| 176 | $event->object['url'] = $original_discussion->getURL(); |
||
| 177 | } |
||
| 178 | View Code Duplication | } elseif ($object instanceof ElggFile) { |
|
| 179 | $event->object['type'] = 'file'; |
||
| 180 | $event->object['name'] = gc_explode_translation($object->title, $lang); |
||
| 181 | $event->object['description'] = gc_explode_translation($object->description, $lang); |
||
| 182 | $event->object['url'] = $object->getURL(); |
||
| 183 | } elseif ($object instanceof ElggObject) { |
||
| 184 | $subtype = $object->getSubtype(); |
||
| 185 | $event->object['subtype'] = $subtype; |
||
| 186 | $event->object['type'] = 'object'; |
||
| 187 | |||
| 188 | $name = ($object->title) ? $object->title : $object->name; |
||
| 189 | View Code Duplication | if (empty(trim($name))) { |
|
| 190 | $otherEntity = get_entity($object->container_guid); |
||
| 191 | $name = ($otherEntity->title) ? $otherEntity->title : $otherEntity->name; |
||
| 192 | } |
||
| 193 | $event->object['name'] = $name; |
||
| 194 | |||
| 195 | if (is_callable(array($object, 'getURL'))) { |
||
| 196 | $event->object['url'] = $object->getURL(); |
||
| 197 | } |
||
| 198 | |||
| 199 | $event->object['description'] = gc_explode_translation($object->description, $lang); |
||
| 200 | |||
| 201 | $other = get_entity($object->container_guid); |
||
| 202 | View Code Duplication | if ($other instanceof ElggGroup) { |
|
| 203 | if (!isset($event->object['type'])) { |
||
| 204 | $event->object['name'] = ($other->title) ? $other->title : $other->name; |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | if (!isset($event->object['type'])) { |
||
| 208 | $event->object['name'] = ($other->title) ? $other->title : $other->name; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | View Code Duplication | if (strpos($event->object['name'], '"en":') !== false) { |
|
| 213 | $event->object['name'] = gc_explode_translation($event->object['name'], $lang); |
||
| 214 | } |
||
| 215 | View Code Duplication | } else { |
|
| 216 | //@TODO handle any unknown events |
||
| 217 | if (strpos($object->title, '"en":') !== false) { |
||
| 218 | $event->object['name'] = gc_explode_translation($object->title, $lang); |
||
| 219 | } else { |
||
| 220 | $event->object['name'] = $object->title; |
||
| 221 | } |
||
| 222 | |||
| 223 | if (strpos($object->description, '"en":') !== false) { |
||
| 224 | $event->object['description'] = gc_explode_translation($object->description, $lang); |
||
| 225 | } else { |
||
| 226 | $event->object['description'] = $object->description; |
||
| 227 | } |
||
| 228 | |||
| 229 | if (is_callable(array($object, 'getURL'))) { |
||
| 230 | $event->object['url'] = $object->getURL(); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | return $activity; |
||
| 236 | } |
||
| 237 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: