Conditions | 11 |
Paths | 152 |
Total Lines | 71 |
Code Lines | 46 |
Lines | 31 |
Ratio | 43.66 % |
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 |
||
104 | function get_discussions($user, $limit, $offset, $filters, $lang) |
||
105 | { |
||
106 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
107 | if (!$user_entity) { |
||
108 | return "User was not found. Please try a different GUID, username, or email address"; |
||
109 | } |
||
110 | if (!$user_entity instanceof ElggUser) { |
||
111 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
112 | } |
||
113 | |||
114 | if (!elgg_is_logged_in()) { |
||
115 | login($user_entity); |
||
116 | } |
||
117 | |||
118 | $filter_data = json_decode($filters); |
||
119 | View Code Duplication | if (!empty($filter_data)) { |
|
120 | $params = array( |
||
121 | 'type' => 'object', |
||
122 | 'subtype' => 'groupforumtopic', |
||
123 | 'limit' => $limit, |
||
124 | 'offset' => $offset |
||
125 | ); |
||
126 | |||
127 | if ($filter_data->mine) { |
||
128 | $params['owner_guid'] = $user_entity->guid; |
||
129 | } |
||
130 | |||
131 | if ($filter_data->name) { |
||
132 | $db_prefix = elgg_get_config('dbprefix'); |
||
133 | $params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid"); |
||
134 | $params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')"); |
||
135 | } |
||
136 | |||
137 | if ($filter_data->mine) { |
||
138 | $all_discussions = elgg_list_entities_from_relationship($params); |
||
139 | } else { |
||
140 | $all_discussions = elgg_list_entities_from_metadata($params); |
||
141 | } |
||
142 | } else { |
||
143 | $all_discussions = elgg_list_entities(array( |
||
144 | 'type' => 'object', |
||
145 | 'subtype' => 'groupforumtopic', |
||
146 | 'limit' => $limit, |
||
147 | 'offset' => $offset |
||
148 | )); |
||
149 | } |
||
150 | |||
151 | $discussions = json_decode($all_discussions); |
||
152 | |||
153 | foreach ($discussions as $discussion) { |
||
154 | $discussion->name = gc_explode_translation($discussion->name, $lang); |
||
155 | |||
156 | $likes = elgg_get_annotations(array( |
||
157 | 'guid' => $discussion->guid, |
||
158 | 'annotation_name' => 'likes' |
||
159 | )); |
||
160 | $discussion->likes = count($likes); |
||
161 | |||
162 | $liked = elgg_get_annotations(array( |
||
163 | 'guid' => $discussion->guid, |
||
164 | 'annotation_owner_guid' => $user_entity->guid, |
||
165 | 'annotation_name' => 'likes' |
||
166 | )); |
||
167 | $discussion->liked = count($liked) > 0; |
||
168 | |||
169 | $discussion->userDetails = get_user_block($discussion->owner_guid, $lang); |
||
170 | $discussion->description = clean_text(gc_explode_translation($discussion->description, $lang)); |
||
171 | } |
||
172 | |||
173 | return $discussions; |
||
174 | } |
||
175 |