| Conditions | 17 |
| Paths | 313 |
| Total Lines | 83 |
| Code Lines | 56 |
| 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 |
||
| 90 | function submit_comment($user, $guid, $message, $lang) |
||
| 91 | { |
||
| 92 | // No Empty Comments |
||
| 93 | if (empty($message)){ |
||
| 94 | return elgg_echo("generic_comment:blank"); |
||
| 95 | } |
||
| 96 | |||
| 97 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
| 98 | if (!$user_entity) { |
||
| 99 | return "User was not found. Please try a different GUID, username, or email address"; |
||
| 100 | } |
||
| 101 | if (!$user_entity instanceof ElggUser) { |
||
| 102 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
| 103 | } |
||
| 104 | if (!elgg_is_logged_in()) { |
||
| 105 | login($user_entity); |
||
| 106 | } |
||
| 107 | |||
| 108 | $entity = get_entity($guid); |
||
| 109 | $subtype = 'nothing'; |
||
| 110 | if (!$entity) { |
||
| 111 | return elgg_echo("generic_comment:notfound"); |
||
| 112 | } else if (elgg_instanceof($entity, "object", "groupforumtopic")){ |
||
| 113 | $subtype = 'discussion_reply'; |
||
| 114 | // Check if member of group |
||
| 115 | $group = $entity->getContainerEntity(); |
||
| 116 | if (!$group->canWriteToContainer()) { |
||
| 117 | return elgg_echo('groups:notmember'); |
||
| 118 | } |
||
| 119 | } else { |
||
| 120 | $subtype = 'comment'; |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | $reply = ''; |
||
| 125 | if ($subtype == 'discussion_reply'){ |
||
| 126 | $reply = new ElggDiscussionReply(); |
||
| 127 | } else if ($subtype == 'comment'){ |
||
| 128 | $reply = new ElggComment(); |
||
| 129 | } |
||
| 130 | |||
| 131 | $reply->description = $message; |
||
| 132 | $reply->owner_guid = $user_entity->getGUID(); |
||
| 133 | $reply->container_guid = $entity->getGUID(); |
||
| 134 | $reply->access_id = $entity->access_id; //idk |
||
| 135 | |||
| 136 | $reply_guid = $reply->save(); |
||
| 137 | // If save comment fails, return message about it |
||
| 138 | if ($reply_guid == false){ |
||
| 139 | if ($subtype == 'discussion_reply') { |
||
| 140 | return elgg_echo('groupspost:failure'); |
||
| 141 | } else if ($subtype == 'comment'){ |
||
| 142 | return elgg_echo("generic_comment:failure"); |
||
| 143 | } else { |
||
| 144 | return "reply failed - unknown - ref 00001 "; //this shouldnt happen |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | //River Item |
||
| 150 | if ($subtype == 'discussion_reply') { |
||
| 151 | elgg_create_river_item(array( |
||
| 152 | 'view' => 'river/object/discussion_reply/create', |
||
| 153 | 'action_type' => 'reply', |
||
| 154 | 'subject_guid' => $user_entity->guid, |
||
| 155 | 'object_guid' => $reply_guid, |
||
| 156 | 'target_guid' => $entity->guid, |
||
| 157 | )); |
||
| 158 | return elgg_echo('groupspost:success'); |
||
| 159 | } else if ($subtype == 'comment'){ |
||
| 160 | elgg_create_river_item(array( |
||
| 161 | 'view' => 'river/object/comment/create', |
||
| 162 | 'action_type' => 'comment', |
||
| 163 | 'subject_guid' => $user_entity->guid, |
||
| 164 | 'object_guid' => $reply_guid, |
||
| 165 | 'target_guid' => $entity->guid, |
||
| 166 | )); |
||
| 167 | return elgg_echo('generic_comment:posted'); |
||
| 168 | } |
||
| 169 | |||
| 170 | $result = "Should not reach this, but in-case, ref 00002"; |
||
| 171 | return $result; |
||
| 172 | } |
||
| 173 |