| Conditions | 18 |
| Paths | 84 |
| Total Lines | 103 |
| Code Lines | 65 |
| Lines | 30 |
| Ratio | 29.13 % |
| 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 |
||
| 61 | public function search($search, $limit, $offset, ISearchResult $searchResult) { |
||
| 62 | $result = ['wide' => [], 'exact' => []]; |
||
| 63 | $userType = new SearchResultType('users'); |
||
| 64 | $emailType = new SearchResultType('emails'); |
||
| 65 | |||
| 66 | // Search in contacts |
||
| 67 | //@todo Pagination missing |
||
| 68 | $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']); |
||
| 69 | $lowerSearch = strtolower($search); |
||
| 70 | foreach ($addressBookContacts as $contact) { |
||
| 71 | if (isset($contact['EMAIL'])) { |
||
| 72 | $emailAddresses = $contact['EMAIL']; |
||
| 73 | if (!is_array($emailAddresses)) { |
||
| 74 | $emailAddresses = [$emailAddresses]; |
||
| 75 | } |
||
| 76 | foreach ($emailAddresses as $emailAddress) { |
||
| 77 | $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; |
||
| 78 | |||
| 79 | if (isset($contact['isLocalSystemBook'])) { |
||
| 80 | if ($exactEmailMatch) { |
||
| 81 | try { |
||
| 82 | $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
||
| 83 | } catch (\InvalidArgumentException $e) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | View Code Duplication | if (!$searchResult->hasResult($userType, $cloud->getUser())) { |
|
| 88 | $singleResult = [[ |
||
| 89 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
| 90 | 'value' => [ |
||
| 91 | 'shareType' => Share::SHARE_TYPE_USER, |
||
| 92 | 'shareWith' => $cloud->getUser(), |
||
| 93 | ], |
||
| 94 | ]]; |
||
| 95 | $searchResult->addResultSet($userType, [], $singleResult); |
||
| 96 | $searchResult->markExactIdMatch($emailType); |
||
| 97 | } |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($this->shareeEnumeration) { |
||
| 102 | try { |
||
| 103 | $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
||
| 104 | } catch (\InvalidArgumentException $e) { |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | View Code Duplication | if (!$searchResult->hasResult($userType, $cloud->getUser())) { |
|
| 109 | $singleResult = [[ |
||
| 110 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
| 111 | 'value' => [ |
||
| 112 | 'shareType' => Share::SHARE_TYPE_USER, |
||
| 113 | 'shareWith' => $cloud->getUser(), |
||
| 114 | ]], |
||
| 115 | ]; |
||
| 116 | $searchResult->addResultSet($userType, $singleResult, []); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { |
||
| 123 | if ($exactEmailMatch) { |
||
| 124 | $searchResult->markExactIdMatch($emailType); |
||
| 125 | } |
||
| 126 | $result['exact'][] = [ |
||
| 127 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
| 128 | 'value' => [ |
||
| 129 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
| 130 | 'shareWith' => $emailAddress, |
||
| 131 | ], |
||
| 132 | ]; |
||
| 133 | View Code Duplication | } else { |
|
| 134 | $result['wide'][] = [ |
||
| 135 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
| 136 | 'value' => [ |
||
| 137 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
| 138 | 'shareWith' => $emailAddress, |
||
| 139 | ], |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if (!$this->shareeEnumeration) { |
||
| 147 | $result['wide'] = []; |
||
| 148 | } |
||
| 149 | |||
| 150 | if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) { |
||
| 151 | $result['exact'][] = [ |
||
| 152 | 'label' => $search, |
||
| 153 | 'value' => [ |
||
| 154 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
| 155 | 'shareWith' => $search, |
||
| 156 | ], |
||
| 157 | ]; |
||
| 158 | } |
||
| 159 | |||
| 160 | $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); |
||
| 161 | |||
| 162 | return true; |
||
| 163 | } |
||
| 164 | } |
||
| 165 |