Conditions | 21 |
Paths | 145 |
Total Lines | 83 |
Code Lines | 51 |
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 |
||
67 | public function search($search, $limit, $offset, ISearchResult $searchResult) { |
||
68 | if ($this->groupSharingDisabled) { |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | $hasMoreResults = false; |
||
73 | $result = ['wide' => [], 'exact' => []]; |
||
74 | |||
75 | $groups = $this->groupManager->search($search, $limit, $offset); |
||
76 | $groupIds = array_map(function (IGroup $group) { |
||
77 | return $group->getGID(); |
||
78 | }, $groups); |
||
79 | |||
80 | if (!$this->shareeEnumeration || count($groups) < $limit) { |
||
81 | $hasMoreResults = true; |
||
82 | } |
||
83 | |||
84 | $userGroups = []; |
||
85 | if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) { |
||
86 | // Intersect all the groups that match with the groups this user is a member of |
||
87 | $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser()); |
||
88 | $userGroups = array_map(function (IGroup $group) { |
||
89 | return $group->getGID(); |
||
90 | }, $userGroups); |
||
91 | $groupIds = array_intersect($groupIds, $userGroups); |
||
92 | } |
||
93 | |||
94 | $lowerSearch = strtolower($search); |
||
95 | foreach ($groups as $group) { |
||
96 | if ($group->hideFromCollaboration()) { |
||
97 | continue; |
||
98 | } |
||
99 | |||
100 | // FIXME: use a more efficient approach |
||
101 | $gid = $group->getGID(); |
||
102 | if (!in_array($gid, $groupIds)) { |
||
103 | continue; |
||
104 | } |
||
105 | if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) { |
||
106 | $result['exact'][] = [ |
||
107 | 'label' => $group->getDisplayName(), |
||
108 | 'value' => [ |
||
109 | 'shareType' => IShare::TYPE_GROUP, |
||
110 | 'shareWith' => $gid, |
||
111 | ], |
||
112 | ]; |
||
113 | } else { |
||
114 | if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) { |
||
115 | continue; |
||
116 | } |
||
117 | $result['wide'][] = [ |
||
118 | 'label' => $group->getDisplayName(), |
||
119 | 'value' => [ |
||
120 | 'shareType' => IShare::TYPE_GROUP, |
||
121 | 'shareWith' => $gid, |
||
122 | ], |
||
123 | ]; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if ($offset === 0 && empty($result['exact'])) { |
||
128 | // On page one we try if the search result has a direct hit on the |
||
129 | // user id and if so, we add that to the exact match list |
||
130 | $group = $this->groupManager->get($search); |
||
131 | if ($group instanceof IGroup && !$group->hideFromCollaboration() && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) { |
||
132 | $result['exact'][] = [ |
||
133 | 'label' => $group->getDisplayName(), |
||
134 | 'value' => [ |
||
135 | 'shareType' => IShare::TYPE_GROUP, |
||
136 | 'shareWith' => $group->getGID(), |
||
137 | ], |
||
138 | ]; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | if (!$this->shareeEnumeration) { |
||
143 | $result['wide'] = []; |
||
144 | } |
||
145 | |||
146 | $type = new SearchResultType('groups'); |
||
147 | $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
||
148 | |||
149 | return $hasMoreResults; |
||
150 | } |
||
152 |