| Conditions | 10 |
| Paths | 80 |
| Total Lines | 74 |
| 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 |
||
| 89 | function get_docs($user, $limit, $offset, $filters, $lang) |
||
| 90 | { |
||
| 91 | $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
||
| 92 | if (!$user_entity) { |
||
| 93 | return "User was not found. Please try a different GUID, username, or email address"; |
||
| 94 | } |
||
| 95 | if (!$user_entity instanceof ElggUser) { |
||
| 96 | return "Invalid user. Please try a different GUID, username, or email address"; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!elgg_is_logged_in()) { |
||
| 100 | login($user_entity); |
||
| 101 | } |
||
| 102 | |||
| 103 | $filter_data = json_decode($filters); |
||
| 104 | if (!empty($filter_data)) { |
||
| 105 | $params = array( |
||
| 106 | 'type' => 'object', |
||
| 107 | 'subtypes' => array('etherpad', 'subpad'), |
||
| 108 | 'limit' => $limit, |
||
| 109 | 'offset' => $offset |
||
| 110 | ); |
||
| 111 | |||
| 112 | if ($filter_data->name) { |
||
| 113 | $db_prefix = elgg_get_config('dbprefix'); |
||
| 114 | $params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid"); |
||
| 115 | $params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')"); |
||
| 116 | } |
||
| 117 | |||
| 118 | $all_docs = elgg_list_entities_from_metadata($params); |
||
| 119 | } else { |
||
| 120 | $all_docs = elgg_list_entities(array( |
||
| 121 | 'type' => 'object', |
||
| 122 | 'subtypes' => array('etherpad', 'subpad'), |
||
| 123 | 'limit' => $limit, |
||
| 124 | 'offset' => $offset |
||
| 125 | )); |
||
| 126 | } |
||
| 127 | |||
| 128 | $docs = json_decode($all_docs); |
||
| 129 | |||
| 130 | foreach ($docs as $doc) { |
||
| 131 | $doc->name = gc_explode_translation($doc->name, $lang); |
||
| 132 | |||
| 133 | $likes = elgg_get_annotations(array( |
||
| 134 | 'guid' => $doc->guid, |
||
| 135 | 'annotation_name' => 'likes' |
||
| 136 | )); |
||
| 137 | $doc->likes = count($likes); |
||
| 138 | |||
| 139 | $liked = elgg_get_annotations(array( |
||
| 140 | 'guid' => $doc->guid, |
||
| 141 | 'annotation_owner_guid' => $user_entity->guid, |
||
| 142 | 'annotation_name' => 'likes' |
||
| 143 | )); |
||
| 144 | $doc->liked = count($liked) > 0; |
||
| 145 | |||
| 146 | $docObj = new ElggPad($doc->guid); |
||
| 147 | $doc->owner = ($docObj->getOwnerEntity() == $user_entity); |
||
| 148 | $doc->iconURL = $docObj->getIconURL(); |
||
| 149 | $doc->url = $docObj->getPadPath(); |
||
| 150 | |||
| 151 | $doc->userDetails = get_user_block($doc->owner_guid, $lang); |
||
| 152 | $doc->description = clean_text(gc_explode_translation($doc->description, $lang)); |
||
| 153 | |||
| 154 | if ($doc->container_guid != $doc->owner_guid){ |
||
| 155 | $doc->group_guid = $doc->container_guid; |
||
| 156 | $docGroup = get_entity($doc->group_guid); |
||
| 157 | $doc->group = gc_explode_translation($docGroup->name, $lang); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return $docs; |
||
| 162 | } |
||
| 163 |