| Conditions | 18 |
| Paths | 439 |
| Total Lines | 81 |
| Code Lines | 49 |
| 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 |
||
| 117 | public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) { |
||
|
|
|||
| 118 | |||
| 119 | // only search for string larger than a given threshold |
||
| 120 | $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0); |
||
| 121 | if (strlen($search) < $threshold) { |
||
| 122 | return new DataResponse($this->result); |
||
| 123 | } |
||
| 124 | |||
| 125 | // never return more than the max. number of results configured in the config.php |
||
| 126 | $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0); |
||
| 127 | if ($maxResults > 0) { |
||
| 128 | $perPage = min($perPage, $maxResults); |
||
| 129 | } |
||
| 130 | if ($perPage <= 0) { |
||
| 131 | throw new OCSBadRequestException('Invalid perPage argument'); |
||
| 132 | } |
||
| 133 | if ($page <= 0) { |
||
| 134 | throw new OCSBadRequestException('Invalid page'); |
||
| 135 | } |
||
| 136 | |||
| 137 | $shareTypes = [ |
||
| 138 | Share::SHARE_TYPE_USER, |
||
| 139 | ]; |
||
| 140 | |||
| 141 | if ($itemType === null) { |
||
| 142 | throw new OCSBadRequestException('Missing itemType'); |
||
| 143 | } elseif ($itemType === 'file' || $itemType === 'folder') { |
||
| 144 | if ($this->shareManager->allowGroupSharing()) { |
||
| 145 | $shareTypes[] = Share::SHARE_TYPE_GROUP; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($this->isRemoteSharingAllowed($itemType)) { |
||
| 149 | $shareTypes[] = Share::SHARE_TYPE_REMOTE; |
||
| 150 | } |
||
| 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 | |||
| 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: