| Conditions | 17 |
| Paths | 288 |
| Total Lines | 87 |
| Code Lines | 52 |
| 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 |
||
| 62 | public function search($search, $limit, $offset, ISearchResult $searchResult) { |
||
| 63 | $result = ['wide' => [], 'exact' => []]; |
||
| 64 | $users = []; |
||
| 65 | $hasMoreResults = false; |
||
| 66 | |||
| 67 | $userGroups = []; |
||
| 68 | if ($this->shareWithGroupOnly) { |
||
| 69 | // Search in all the groups this user is part of |
||
| 70 | $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
||
|
|
|||
| 71 | foreach ($userGroups as $userGroup) { |
||
| 72 | $usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset); |
||
| 73 | foreach ($usersTmp as $uid => $userDisplayName) { |
||
| 74 | $users[$uid] = $userDisplayName; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | // Search in all users |
||
| 79 | $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset); |
||
| 80 | |||
| 81 | foreach ($usersTmp as $user) { |
||
| 82 | $users[$user->getUID()] = $user->getDisplayName(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!$this->shareeEnumeration || sizeof($users) < $limit) { |
||
| 87 | $hasMoreResults = true; |
||
| 88 | } |
||
| 89 | |||
| 90 | $foundUserById = false; |
||
| 91 | $lowerSearch = strtolower($search); |
||
| 92 | foreach ($users as $uid => $userDisplayName) { |
||
| 93 | if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) { |
||
| 94 | if (strtolower($uid) === $lowerSearch) { |
||
| 95 | $foundUserById = true; |
||
| 96 | } |
||
| 97 | $result['exact'][] = [ |
||
| 98 | 'label' => $userDisplayName, |
||
| 99 | 'value' => [ |
||
| 100 | 'shareType' => Share::SHARE_TYPE_USER, |
||
| 101 | 'shareWith' => $uid, |
||
| 102 | ], |
||
| 103 | ]; |
||
| 104 | } else { |
||
| 105 | $result['wide'][] = [ |
||
| 106 | 'label' => $userDisplayName, |
||
| 107 | 'value' => [ |
||
| 108 | 'shareType' => Share::SHARE_TYPE_USER, |
||
| 109 | 'shareWith' => $uid, |
||
| 110 | ], |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($offset === 0 && !$foundUserById) { |
||
| 116 | // On page one we try if the search result has a direct hit on the |
||
| 117 | // user id and if so, we add that to the exact match list |
||
| 118 | $user = $this->userManager->get($search); |
||
| 119 | if ($user instanceof IUser) { |
||
| 120 | $addUser = true; |
||
| 121 | |||
| 122 | if ($this->shareWithGroupOnly) { |
||
| 123 | // Only add, if we have a common group |
||
| 124 | $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user)); |
||
| 125 | $addUser = !empty($commonGroups); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($addUser) { |
||
| 129 | array_push($result['exact'], [ |
||
| 130 | 'label' => $user->getDisplayName(), |
||
| 131 | 'value' => [ |
||
| 132 | 'shareType' => Share::SHARE_TYPE_USER, |
||
| 133 | 'shareWith' => $user->getUID(), |
||
| 134 | ], |
||
| 135 | ]); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | if (!$this->shareeEnumeration) { |
||
| 141 | $result['wide'] = []; |
||
| 142 | } |
||
| 143 | |||
| 144 | $type = new SearchResultType('users'); |
||
| 145 | $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
||
| 146 | |||
| 147 | return $hasMoreResults; |
||
| 148 | } |
||
| 149 | } |
||
| 150 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: