Conditions | 26 |
Paths | 824 |
Total Lines | 84 |
Code Lines | 55 |
Lines | 12 |
Ratio | 14.29 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
197 | function post_discussion($user, $title, $message, $container_guid, $access, $open, $topic_guid, $lang) |
||
198 | { |
||
199 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
200 | if (!$user_entity) { |
||
201 | return "User was not found. Please try a different GUID, username, or email address"; |
||
202 | } |
||
203 | if (!$user_entity instanceof ElggUser) { |
||
204 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
205 | } |
||
206 | if (!elgg_is_logged_in()) { |
||
207 | login($user_entity); |
||
208 | } |
||
209 | |||
210 | //check required fields |
||
211 | $titles = json_decode($title); |
||
212 | $message = json_decode($message); |
||
213 | if (!$titles->en && !$titles->fr) { return elgg_echo("discussion:error:missing"); } |
||
214 | if (!$message->en && !$message->fr) { return elgg_echo("discussion:error:missing"); } |
||
215 | View Code Duplication | if (!($titles->en && $message->en) && !($titles->fr && $message->fr)) { return "require-same-lang"; } |
|
216 | |||
217 | $container = get_entity($container_guid); |
||
218 | if (!$container || !$container->canWriteToContainer(0, 'object', 'groupforumtopic')) { |
||
219 | return elgg_echo('discussion:error:permissions'); |
||
220 | } |
||
221 | |||
222 | //Check if new topic or edit |
||
223 | $new_topic = true; |
||
224 | if ($topic_guid > 0) { |
||
225 | $new_topic = false; |
||
226 | } |
||
227 | |||
228 | if ($new_topic) { |
||
229 | $topic = new ElggObject(); |
||
230 | $topic->subtype = 'groupforumtopic'; |
||
231 | } else { |
||
232 | $topic = get_entity($guid); |
||
|
|||
233 | if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) { |
||
234 | return elgg_echo('discussion:topic:notfound'); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | //french english setup |
||
239 | $title1 = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8'); |
||
240 | $title2 = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8'); |
||
241 | $title = gc_implode_translation($title1, $title2); |
||
242 | $desc = gc_implode_translation($message->en, $message->fr); |
||
243 | |||
244 | if ($access == 1 && !$container->isPublicMembership()){ |
||
245 | $access = 2; //Access cannot be public if group is not public. Default to group only. |
||
246 | } |
||
247 | $access_id = $access; |
||
248 | if ($access_id === 2){ |
||
249 | $access_id = $container->group_acl; //Sets access id to match group only id. |
||
250 | } |
||
251 | |||
252 | |||
253 | $topic->title = $title; |
||
254 | $topic->title2 = $title2; |
||
255 | $topic->description = $desc; |
||
256 | $topic->description2 = $message->fr; |
||
257 | $topic->status = ($open == 1) ? "open" : "closed"; |
||
258 | $topic->access_id = $access_id; |
||
259 | $topic->container_guid = $container_guid; |
||
260 | |||
261 | $result = $topic->save(); |
||
262 | if (!$result) { |
||
263 | return elgg_echo('discussion:error:notsaved'); |
||
264 | } |
||
265 | |||
266 | //handle results differently for new topics and topic edits |
||
267 | View Code Duplication | if ($new_topic) { |
|
268 | system_message(elgg_echo('discussion:topic:created')); |
||
269 | elgg_create_river_item(array( |
||
270 | 'view' => 'river/object/groupforumtopic/create', |
||
271 | 'action_type' => 'create', |
||
272 | 'subject_guid' => elgg_get_logged_in_user_guid(), |
||
273 | 'object_guid' => $topic->guid, |
||
274 | )); |
||
275 | } else { |
||
276 | system_message(elgg_echo('discussion:topic:updated')); |
||
277 | } |
||
278 | |||
279 | return $topic->getURL(); |
||
280 | } |
||
281 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.