Conditions | 17 |
Paths | 288 |
Total Lines | 89 |
Code Lines | 53 |
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 | $this->takeOutCurrentUser($users); |
||
87 | |||
88 | if (!$this->shareeEnumeration || sizeof($users) < $limit) { |
||
89 | $hasMoreResults = true; |
||
90 | } |
||
91 | |||
92 | $foundUserById = false; |
||
93 | $lowerSearch = strtolower($search); |
||
94 | foreach ($users as $uid => $userDisplayName) { |
||
95 | if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) { |
||
96 | if (strtolower($uid) === $lowerSearch) { |
||
97 | $foundUserById = true; |
||
98 | } |
||
99 | $result['exact'][] = [ |
||
100 | 'label' => $userDisplayName, |
||
101 | 'value' => [ |
||
102 | 'shareType' => Share::SHARE_TYPE_USER, |
||
103 | 'shareWith' => $uid, |
||
104 | ], |
||
105 | ]; |
||
106 | } else { |
||
107 | $result['wide'][] = [ |
||
108 | 'label' => $userDisplayName, |
||
109 | 'value' => [ |
||
110 | 'shareType' => Share::SHARE_TYPE_USER, |
||
111 | 'shareWith' => $uid, |
||
112 | ], |
||
113 | ]; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | if ($offset === 0 && !$foundUserById) { |
||
118 | // On page one we try if the search result has a direct hit on the |
||
119 | // user id and if so, we add that to the exact match list |
||
120 | $user = $this->userManager->get($search); |
||
121 | if ($user instanceof IUser) { |
||
122 | $addUser = true; |
||
123 | |||
124 | if ($this->shareWithGroupOnly) { |
||
125 | // Only add, if we have a common group |
||
126 | $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user)); |
||
127 | $addUser = !empty($commonGroups); |
||
128 | } |
||
129 | |||
130 | if ($addUser) { |
||
131 | array_push($result['exact'], [ |
||
132 | 'label' => $user->getDisplayName(), |
||
133 | 'value' => [ |
||
134 | 'shareType' => Share::SHARE_TYPE_USER, |
||
135 | 'shareWith' => $user->getUID(), |
||
136 | ], |
||
137 | ]); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | if (!$this->shareeEnumeration) { |
||
143 | $result['wide'] = []; |
||
144 | } |
||
145 | |||
146 | $type = new SearchResultType('users'); |
||
147 | $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
||
148 | |||
149 | return $hasMoreResults; |
||
150 | } |
||
151 | |||
161 |
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: