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