Conditions | 24 |
Paths | 256 |
Total Lines | 119 |
Code Lines | 74 |
Lines | 30 |
Ratio | 25.21 % |
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 |
||
75 | public function search($search, $limit, $offset, ISearchResult $searchResult) { |
||
76 | $result = ['wide' => [], 'exact' => []]; |
||
77 | $userType = new SearchResultType('users'); |
||
78 | $emailType = new SearchResultType('emails'); |
||
79 | |||
80 | // Search in contacts |
||
81 | //@todo Pagination missing |
||
82 | $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']); |
||
83 | $lowerSearch = strtolower($search); |
||
84 | foreach ($addressBookContacts as $contact) { |
||
85 | if (isset($contact['EMAIL'])) { |
||
86 | $emailAddresses = $contact['EMAIL']; |
||
87 | if (!is_array($emailAddresses)) { |
||
88 | $emailAddresses = [$emailAddresses]; |
||
89 | } |
||
90 | foreach ($emailAddresses as $emailAddress) { |
||
91 | $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; |
||
92 | |||
93 | if (isset($contact['isLocalSystemBook'])) { |
||
94 | if ($this->shareWithGroupOnly) { |
||
95 | /* |
||
96 | * Check if the user may share with the user associated with the e-mail of the just found contact |
||
97 | */ |
||
98 | $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
||
|
|||
99 | $found = false; |
||
100 | foreach ($userGroups as $userGroup) { |
||
101 | if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) { |
||
102 | $found = true; |
||
103 | break; |
||
104 | } |
||
105 | } |
||
106 | if (!$found) { |
||
107 | continue; |
||
108 | } |
||
109 | } |
||
110 | if ($exactEmailMatch) { |
||
111 | try { |
||
112 | $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
||
113 | } catch (\InvalidArgumentException $e) { |
||
114 | continue; |
||
115 | } |
||
116 | |||
117 | View Code Duplication | if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
|
118 | $singleResult = [[ |
||
119 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
120 | 'value' => [ |
||
121 | 'shareType' => Share::SHARE_TYPE_USER, |
||
122 | 'shareWith' => $cloud->getUser(), |
||
123 | ], |
||
124 | ]]; |
||
125 | $searchResult->addResultSet($userType, [], $singleResult); |
||
126 | $searchResult->markExactIdMatch($emailType); |
||
127 | } |
||
128 | return false; |
||
129 | } |
||
130 | |||
131 | if ($this->shareeEnumeration) { |
||
132 | try { |
||
133 | $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
||
134 | } catch (\InvalidArgumentException $e) { |
||
135 | continue; |
||
136 | } |
||
137 | |||
138 | View Code Duplication | if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
|
139 | $singleResult = [[ |
||
140 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
141 | 'value' => [ |
||
142 | 'shareType' => Share::SHARE_TYPE_USER, |
||
143 | 'shareWith' => $cloud->getUser(), |
||
144 | ]], |
||
145 | ]; |
||
146 | $searchResult->addResultSet($userType, $singleResult, []); |
||
147 | } |
||
148 | } |
||
149 | continue; |
||
150 | } |
||
151 | |||
152 | if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { |
||
153 | if ($exactEmailMatch) { |
||
154 | $searchResult->markExactIdMatch($emailType); |
||
155 | } |
||
156 | $result['exact'][] = [ |
||
157 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
158 | 'value' => [ |
||
159 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
160 | 'shareWith' => $emailAddress, |
||
161 | ], |
||
162 | ]; |
||
163 | View Code Duplication | } else { |
|
164 | $result['wide'][] = [ |
||
165 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
166 | 'value' => [ |
||
167 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
168 | 'shareWith' => $emailAddress, |
||
169 | ], |
||
170 | ]; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if (!$this->shareeEnumeration) { |
||
177 | $result['wide'] = []; |
||
178 | } |
||
179 | |||
180 | if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) { |
||
181 | $result['exact'][] = [ |
||
182 | 'label' => $search, |
||
183 | 'value' => [ |
||
184 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
185 | 'shareWith' => $search, |
||
186 | ], |
||
187 | ]; |
||
188 | } |
||
189 | |||
190 | $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); |
||
191 | |||
192 | return true; |
||
193 | } |
||
194 | |||
200 |
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: