| Conditions | 19 |
| Paths | 162 |
| Total Lines | 71 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 48 | public function createCardFromUser(IUser $user): ?VCard { |
||
| 49 | $account = $this->accountManager->getAccount($user); |
||
| 50 | $userProperties = $account->getProperties(); |
||
| 51 | try { |
||
| 52 | $additionalEmailsCollection = $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL); |
||
| 53 | $userProperties = array_merge($userProperties, $additionalEmailsCollection->getProperties()); |
||
| 54 | } catch (PropertyDoesNotExistException $e) { |
||
|
|
|||
| 55 | } |
||
| 56 | |||
| 57 | $uid = $user->getUID(); |
||
| 58 | $cloudId = $user->getCloudId(); |
||
| 59 | $image = $this->getAvatarImage($user); |
||
| 60 | |||
| 61 | $vCard = new VCard(); |
||
| 62 | $vCard->VERSION = '3.0'; |
||
| 63 | $vCard->UID = $uid; |
||
| 64 | |||
| 65 | $publish = false; |
||
| 66 | |||
| 67 | foreach ($userProperties as $property) { |
||
| 68 | $shareWithTrustedServers = |
||
| 69 | $property->getScope() === IAccountManager::SCOPE_FEDERATED || |
||
| 70 | $property->getScope() === IAccountManager::SCOPE_PUBLISHED; |
||
| 71 | |||
| 72 | $emptyValue = $property->getValue() === ''; |
||
| 73 | |||
| 74 | if ($shareWithTrustedServers && !$emptyValue) { |
||
| 75 | $publish = true; |
||
| 76 | switch ($property->getName()) { |
||
| 77 | case IAccountManager::PROPERTY_DISPLAYNAME: |
||
| 78 | $vCard->add(new Text($vCard, 'FN', $property->getValue())); |
||
| 79 | $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()))); |
||
| 80 | break; |
||
| 81 | case IAccountManager::PROPERTY_AVATAR: |
||
| 82 | if ($image !== null) { |
||
| 83 | $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
||
| 84 | } |
||
| 85 | break; |
||
| 86 | case IAccountManager::COLLECTION_EMAIL: |
||
| 87 | case IAccountManager::PROPERTY_EMAIL: |
||
| 88 | $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER'])); |
||
| 89 | break; |
||
| 90 | case IAccountManager::PROPERTY_WEBSITE: |
||
| 91 | $vCard->add(new Text($vCard, 'URL', $property->getValue())); |
||
| 92 | break; |
||
| 93 | case IAccountManager::PROPERTY_PHONE: |
||
| 94 | $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER'])); |
||
| 95 | break; |
||
| 96 | case IAccountManager::PROPERTY_ADDRESS: |
||
| 97 | $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER'])); |
||
| 98 | break; |
||
| 99 | case IAccountManager::PROPERTY_TWITTER: |
||
| 100 | $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER'])); |
||
| 101 | break; |
||
| 102 | case IAccountManager::PROPERTY_ORGANISATION: |
||
| 103 | $vCard->add(new Text($vCard, 'ORG', $property->getValue())); |
||
| 104 | break; |
||
| 105 | case IAccountManager::PROPERTY_ROLE: |
||
| 106 | $vCard->add(new Text($vCard, 'TITLE', $property->getValue())); |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($publish && !empty($cloudId)) { |
||
| 113 | $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
||
| 114 | $vCard->validate(); |
||
| 115 | return $vCard; |
||
| 116 | } |
||
| 117 | |||
| 118 | return null; |
||
| 119 | } |
||
| 149 |