Conditions | 17 |
Paths | 288 |
Total Lines | 80 |
Code Lines | 47 |
Lines | 12 |
Ratio | 15 % |
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 |
||
151 | |||
152 | if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
||
153 | $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
||
154 | } |
||
155 | } else { |
||
156 | $shareTypes[] = Share::SHARE_TYPE_GROUP; |
||
157 | $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
||
158 | } |
||
159 | |||
160 | // FIXME: DI |
||
161 | if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
||
162 | $shareTypes[] = Share::SHARE_TYPE_CIRCLE; |
||
163 | } |
||
164 | |||
165 | if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
||
166 | $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
||
167 | sort($shareTypes); |
||
168 | } else if (is_numeric($shareType)) { |
||
169 | $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
||
170 | sort($shareTypes); |
||
171 | } |
||
172 | |||
173 | $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
||
174 | $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
||
175 | $this->limit = (int) $perPage; |
||
176 | $this->offset = $perPage * ($page - 1); |
||
177 | |||
178 | list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
||
179 | |||
180 | // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
||
181 | if(isset($result['exact'])) { |
||
182 | $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
||
183 | } |
||
184 | $this->result = array_merge($this->result, $result); |
||
185 | $response = new DataResponse($this->result); |
||
186 | |||
187 | if ($hasMoreResults) { |
||
188 | $response->addHeader('Link', $this->getPaginationLink($page, [ |
||
189 | 'search' => $search, |
||
190 | 'itemType' => $itemType, |
||
191 | 'shareType' => $shareTypes, |
||
192 | 'perPage' => $perPage, |
||
193 | ])); |
||
194 | } |
||
195 | |||
196 | return $response; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Method to get out the static call for better testing |
||
201 | * |
||
202 | * @param string $itemType |
||
203 | * @return bool |
||
204 | */ |
||
205 | protected function isRemoteSharingAllowed($itemType) { |
||
206 | try { |
||
207 | // FIXME: static foo makes unit testing unnecessarily difficult |
||
208 | $backend = \OC\Share\Share::getBackend($itemType); |
||
209 | return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); |
||
210 | } catch (\Exception $e) { |
||
211 | return false; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | |||
216 | /** |
||
217 | * Generates a bunch of pagination links for the current page |
||
218 | * |
||
219 | * @param int $page Current page |
||
220 | * @param array $params Parameters for the URL |
||
221 | * @return string |
||
222 | */ |
||
223 | protected function getPaginationLink($page, array $params) { |
||
224 | if ($this->isV2()) { |
||
225 | $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
||
226 | } else { |
||
227 | $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
||
228 | } |
||
229 | $params['page'] = $page + 1; |
||
230 | $link = '<' . $url . http_build_query($params) . '>; rel="next"'; |
||
231 | |||
242 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: