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