| Conditions | 15 |
| Paths | 42 |
| Total Lines | 58 |
| Code Lines | 44 |
| 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 |
||
| 46 | public function createCardFromUser(IUser $user): ?VCard { |
||
| 47 | $userProperties = $this->accountManager->getAccount($user)->getProperties(); |
||
| 48 | |||
| 49 | $uid = $user->getUID(); |
||
| 50 | $cloudId = $user->getCloudId(); |
||
| 51 | $image = $this->getAvatarImage($user); |
||
| 52 | |||
| 53 | $vCard = new VCard(); |
||
| 54 | $vCard->VERSION = '3.0'; |
||
|
|
|||
| 55 | $vCard->UID = $uid; |
||
| 56 | |||
| 57 | $publish = false; |
||
| 58 | |||
| 59 | foreach ($userProperties as $property) { |
||
| 60 | $shareWithTrustedServers = |
||
| 61 | $property->getScope() === IAccountManager::SCOPE_FEDERATED || |
||
| 62 | $property->getScope() === IAccountManager::SCOPE_PUBLISHED; |
||
| 63 | |||
| 64 | $emptyValue = $property->getValue() === ''; |
||
| 65 | |||
| 66 | if ($shareWithTrustedServers && !$emptyValue) { |
||
| 67 | $publish = true; |
||
| 68 | switch ($property->getName()) { |
||
| 69 | case IAccountManager::PROPERTY_DISPLAYNAME: |
||
| 70 | $vCard->add(new Text($vCard, 'FN', $property->getValue())); |
||
| 71 | $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()))); |
||
| 72 | break; |
||
| 73 | case IAccountManager::PROPERTY_AVATAR: |
||
| 74 | if ($image !== null) { |
||
| 75 | $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
||
| 76 | } |
||
| 77 | break; |
||
| 78 | case IAccountManager::PROPERTY_EMAIL: |
||
| 79 | $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER'])); |
||
| 80 | break; |
||
| 81 | case IAccountManager::PROPERTY_WEBSITE: |
||
| 82 | $vCard->add(new Text($vCard, 'URL', $property->getValue())); |
||
| 83 | break; |
||
| 84 | case IAccountManager::PROPERTY_PHONE: |
||
| 85 | $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER'])); |
||
| 86 | break; |
||
| 87 | case IAccountManager::PROPERTY_ADDRESS: |
||
| 88 | $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER'])); |
||
| 89 | break; |
||
| 90 | case IAccountManager::PROPERTY_TWITTER: |
||
| 91 | $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER'])); |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($publish && !empty($cloudId)) { |
||
| 98 | $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
||
| 99 | $vCard->validate(); |
||
| 100 | return $vCard; |
||
| 101 | } |
||
| 102 | |||
| 103 | return null; |
||
| 104 | } |
||
| 134 |