| Conditions | 41 | 
| Paths | > 20000 | 
| Total Lines | 169 | 
| Code Lines | 112 | 
| 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 | ||
| 93 | 	public function search($search, $limit, $offset, ISearchResult $searchResult) { | ||
| 94 | $result = ['wide' => [], 'exact' => []]; | ||
| 95 | $users = []; | ||
| 96 | $hasMoreResults = false; | ||
| 97 | |||
| 98 | $currentUserId = $this->userSession->getUser()->getUID(); | ||
| 99 | $currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); | ||
| 100 | 		if ($this->shareWithGroupOnly) { | ||
| 101 | // Search in all the groups this user is part of | ||
| 102 | 			foreach ($currentUserGroups as $userGroupId) { | ||
| 103 | $usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset); | ||
| 104 | 				foreach ($usersInGroup as $userId => $displayName) { | ||
| 105 | $userId = (string) $userId; | ||
| 106 | $user = $this->userManager->get($userId); | ||
| 107 | 					if (!$user->isEnabled()) { | ||
| 108 | // Ignore disabled users | ||
| 109 | continue; | ||
| 110 | } | ||
| 111 | $users[$userId] = $user; | ||
| 112 | } | ||
| 113 | 				if (count($usersInGroup) >= $limit) { | ||
| 114 | $hasMoreResults = true; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | 		} else { | ||
| 118 | // Search in all users | ||
| 119 | $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset); | ||
| 120 | 			foreach ($usersTmp as $user) { | ||
| 121 | 				if ($user->isEnabled()) { // Don't keep deactivated users | ||
| 122 | $users[$user->getUID()] = $user; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | $this->takeOutCurrentUser($users); | ||
| 128 | |||
| 129 | 		if (!$this->shareeEnumeration || count($users) < $limit) { | ||
| 130 | $hasMoreResults = true; | ||
| 131 | } | ||
| 132 | |||
| 133 | $foundUserById = false; | ||
| 134 | $lowerSearch = strtolower($search); | ||
| 135 | $userStatuses = $this->userStatusManager->getUserStatuses(array_keys($users)); | ||
| 136 | 		foreach ($users as $uid => $user) { | ||
| 137 | $userDisplayName = $user->getDisplayName(); | ||
| 138 | $userEmail = $user->getEMailAddress(); | ||
| 139 | $uid = (string) $uid; | ||
| 140 | |||
| 141 | $status = []; | ||
| 142 | 			if (array_key_exists($uid, $userStatuses)) { | ||
| 143 | $userStatus = $userStatuses[$uid]; | ||
| 144 | $status = [ | ||
| 145 | 'status' => $userStatus->getStatus(), | ||
| 146 | 'message' => $userStatus->getMessage(), | ||
| 147 | 'icon' => $userStatus->getIcon(), | ||
| 148 | 'clearAt' => $userStatus->getClearAt() | ||
| 149 | 						? (int)$userStatus->getClearAt()->format('U') | ||
| 150 | : null, | ||
| 151 | ]; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | if ( | ||
| 156 | $this->shareeEnumerationFullMatch && | ||
| 157 | $lowerSearch !== '' && (strtolower($uid) === $lowerSearch || | ||
| 158 | strtolower($userDisplayName) === $lowerSearch || | ||
| 159 | strtolower($userEmail) === $lowerSearch) | ||
| 160 | 			) { | ||
| 161 | 				if (strtolower($uid) === $lowerSearch) { | ||
| 162 | $foundUserById = true; | ||
| 163 | } | ||
| 164 | $result['exact'][] = [ | ||
| 165 | 'label' => $userDisplayName, | ||
| 166 | 'subline' => $status['message'] ?? '', | ||
| 167 | 'icon' => 'icon-user', | ||
| 168 | 'value' => [ | ||
| 169 | 'shareType' => IShare::TYPE_USER, | ||
| 170 | 'shareWith' => $uid, | ||
| 171 | ], | ||
| 172 | 'shareWithDisplayNameUnique' => !empty($userEmail) ? $userEmail : $uid, | ||
| 173 | 'status' => $status, | ||
| 174 | ]; | ||
| 175 | 			} else { | ||
| 176 | $addToWideResults = false; | ||
| 177 | if ($this->shareeEnumeration && | ||
| 178 | 					!($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone)) { | ||
| 179 | $addToWideResults = true; | ||
| 180 | } | ||
| 181 | |||
| 182 | 				if ($this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $user->getUID())) { | ||
| 183 | $addToWideResults = true; | ||
| 184 | } | ||
| 185 | |||
| 186 | 				if (!$addToWideResults && $this->shareeEnumerationInGroupOnly) { | ||
| 187 | $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user)); | ||
| 188 | 					if (!empty($commonGroups)) { | ||
| 189 | $addToWideResults = true; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | 				if ($addToWideResults) { | ||
| 194 | $result['wide'][] = [ | ||
| 195 | 'label' => $userDisplayName, | ||
| 196 | 'subline' => $status['message'] ?? '', | ||
| 197 | 'icon' => 'icon-user', | ||
| 198 | 'value' => [ | ||
| 199 | 'shareType' => IShare::TYPE_USER, | ||
| 200 | 'shareWith' => $uid, | ||
| 201 | ], | ||
| 202 | 'shareWithDisplayNameUnique' => !empty($userEmail) ? $userEmail : $uid, | ||
| 203 | 'status' => $status, | ||
| 204 | ]; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | 		if ($this->shareeEnumerationFullMatch && $offset === 0 && !$foundUserById) { | ||
| 210 | // On page one we try if the search result has a direct hit on the | ||
| 211 | // user id and if so, we add that to the exact match list | ||
| 212 | $user = $this->userManager->get($search); | ||
| 213 | 			if ($user instanceof IUser) { | ||
| 214 | $addUser = true; | ||
| 215 | |||
| 216 | 				if ($this->shareWithGroupOnly) { | ||
| 217 | // Only add, if we have a common group | ||
| 218 | $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user)); | ||
| 219 | $addUser = !empty($commonGroups); | ||
| 220 | } | ||
| 221 | |||
| 222 | 				if ($addUser) { | ||
| 223 | $status = []; | ||
| 224 | $uid = $user->getUID(); | ||
| 225 | $userEmail = $user->getEMailAddress(); | ||
| 226 | 					if (array_key_exists($user->getUID(), $userStatuses)) { | ||
| 227 | $userStatus = $userStatuses[$user->getUID()]; | ||
| 228 | $status = [ | ||
| 229 | 'status' => $userStatus->getStatus(), | ||
| 230 | 'message' => $userStatus->getMessage(), | ||
| 231 | 'icon' => $userStatus->getIcon(), | ||
| 232 | 'clearAt' => $userStatus->getClearAt() | ||
| 233 | 								? (int)$userStatus->getClearAt()->format('U') | ||
| 234 | : null, | ||
| 235 | ]; | ||
| 236 | } | ||
| 237 | |||
| 238 | $result['exact'][] = [ | ||
| 239 | 'label' => $user->getDisplayName(), | ||
| 240 | 'icon' => 'icon-user', | ||
| 241 | 'subline' => $status['message'] ?? '', | ||
| 242 | 'value' => [ | ||
| 243 | 'shareType' => IShare::TYPE_USER, | ||
| 244 | 'shareWith' => $user->getUID(), | ||
| 245 | ], | ||
| 246 | 'shareWithDisplayNameUnique' => $userEmail !== null && $userEmail !== '' ? $userEmail : $uid, | ||
| 247 | 'status' => $status, | ||
| 248 | ]; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | 		$type = new SearchResultType('users'); | ||
| 256 | $searchResult->addResultSet($type, $result['wide'], $result['exact']); | ||
| 257 | 		if (count($result['exact'])) { | ||
| 258 | $searchResult->markExactIdMatch($type); | ||
| 259 | } | ||
| 260 | |||
| 261 | return $hasMoreResults; | ||
| 262 | } | ||
| 273 |