| Conditions | 21 |
| Paths | 462 |
| Total Lines | 106 |
| Code Lines | 68 |
| 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 |
||
| 63 | public function search($search, $limit, $offset, ISearchResult $searchResult) { |
||
| 64 | $result = ['wide' => [], 'exact' => []]; |
||
| 65 | $resultType = new SearchResultType('remotes'); |
||
| 66 | |||
| 67 | // Search in contacts |
||
| 68 | //@todo Pagination missing |
||
| 69 | $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']); |
||
| 70 | foreach ($addressBookContacts as $contact) { |
||
| 71 | if (isset($contact['isLocalSystemBook'])) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | if (isset($contact['CLOUD'])) { |
||
| 75 | $cloudIds = $contact['CLOUD']; |
||
| 76 | if (is_string($cloudIds)) { |
||
| 77 | $cloudIds = [$cloudIds]; |
||
| 78 | } |
||
| 79 | $lowerSearch = strtolower($search); |
||
| 80 | foreach ($cloudIds as $cloudId) { |
||
| 81 | $cloudIdType = ''; |
||
| 82 | if (\is_array($cloudId)) { |
||
| 83 | $cloudIdData = $cloudId; |
||
| 84 | $cloudId = $cloudIdData['value']; |
||
| 85 | $cloudIdType = $cloudIdData['type']; |
||
| 86 | } |
||
| 87 | try { |
||
| 88 | list($remoteUser, $serverUrl) = $this->splitUserRemote($cloudId); |
||
| 89 | } catch (\InvalidArgumentException $e) { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | $localUser = $this->userManager->get($remoteUser); |
||
| 94 | /** |
||
| 95 | * Add local share if remote cloud id matches a local user ones |
||
| 96 | */ |
||
| 97 | if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId() ) { |
||
| 98 | $result['wide'][] = [ |
||
| 99 | 'label' => $contact['FN'], |
||
| 100 | 'uuid' => $contact['UID'], |
||
| 101 | 'value' => [ |
||
| 102 | 'shareType' => Share::SHARE_TYPE_USER, |
||
| 103 | 'shareWith' => $remoteUser |
||
| 104 | ] |
||
| 105 | ]; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { |
||
| 109 | if (strtolower($cloudId) === $lowerSearch) { |
||
| 110 | $searchResult->markExactIdMatch($resultType); |
||
| 111 | } |
||
| 112 | $result['exact'][] = [ |
||
| 113 | 'label' => $contact['FN'] . " ($cloudId)", |
||
| 114 | 'uuid' => $contact['UID'], |
||
| 115 | 'name' => $contact['FN'], |
||
| 116 | 'type' => $cloudIdType, |
||
| 117 | 'value' => [ |
||
| 118 | 'shareType' => Share::SHARE_TYPE_REMOTE, |
||
| 119 | 'shareWith' => $cloudId, |
||
| 120 | 'server' => $serverUrl, |
||
| 121 | ], |
||
| 122 | ]; |
||
| 123 | } else { |
||
| 124 | $result['wide'][] = [ |
||
| 125 | 'label' => $contact['FN'] . " ($cloudId)", |
||
| 126 | 'uuid' => $contact['UID'], |
||
| 127 | 'name' => $contact['FN'], |
||
| 128 | 'type' => $cloudIdType, |
||
| 129 | 'value' => [ |
||
| 130 | 'shareType' => Share::SHARE_TYPE_REMOTE, |
||
| 131 | 'shareWith' => $cloudId, |
||
| 132 | 'server' => $serverUrl, |
||
| 133 | ], |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | if (!$this->shareeEnumeration) { |
||
| 141 | $result['wide'] = []; |
||
| 142 | } else { |
||
| 143 | $result['wide'] = array_slice($result['wide'], $offset, $limit); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add generic share with remote item for valid cloud ids that are not users of the local instance |
||
| 148 | */ |
||
| 149 | if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { |
||
| 150 | try { |
||
| 151 | list($remoteUser, $serverUrl) = $this->splitUserRemote($search); |
||
| 152 | $localUser = $this->userManager->get($remoteUser); |
||
| 153 | if ($localUser === null || $search !== $localUser->getCloudId()) { |
||
| 154 | $result['exact'][] = [ |
||
| 155 | 'label' => $search, |
||
| 156 | 'value' => [ |
||
| 157 | 'shareType' => Share::SHARE_TYPE_REMOTE, |
||
| 158 | 'shareWith' => $search, |
||
| 159 | ], |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | } catch (\InvalidArgumentException $e) { |
||
|
|
|||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); |
||
| 167 | |||
| 168 | return true; |
||
| 169 | } |
||
| 187 |