Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 9 | class MailAPI extends API |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var Type\FolderIdType |
||
| 13 | */ |
||
| 14 | protected $folderId; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @return Type\FolderIdType |
||
| 18 | */ |
||
| 19 | public function getFolderId() |
||
| 20 | { |
||
| 21 | if (!$this->folderId) { |
||
| 22 | $this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId(); |
||
|
|
|||
| 23 | } |
||
| 24 | |||
| 25 | return $this->folderId; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Type\FolderIdType $folderId |
||
| 30 | */ |
||
| 31 | public function setFolderId($folderId) |
||
| 32 | { |
||
| 33 | $this->folderId = $folderId; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $displayName |
||
| 38 | * @param string|Type\FolderIdType $parentFolder |
||
| 39 | */ |
||
| 40 | 6 | public function pickMailFolder($displayName = null, $parentFolder = 'inbox') |
|
| 41 | { |
||
| 42 | 6 | if ($displayName === null) { |
|
| 43 | $this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId(); |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | 6 | $folder = $this->getFolderByDisplayName($displayName, $parentFolder); |
|
| 48 | $this->folderId = $folder->getFolderId(); |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function formatRestrictions($restrictions) |
||
| 52 | { |
||
| 53 | foreach ($restrictions as $restrictionType => $query) { |
||
| 54 | $formattedRestrictionType = array(); |
||
| 55 | foreach ($query as $key => $value) { |
||
| 56 | if ($value === false) { |
||
| 57 | $value = 'false'; |
||
| 58 | } |
||
| 59 | $formattedRestrictionType[] = array( |
||
| 60 | 'FieldURI' => array('FieldURI' => API\FieldURIManager::getFieldUriByName($key, 'message')), |
||
| 61 | 'FieldURIOrConstant' => array('Constant' => array('Value' => (string) $value)) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | $restrictions[$restrictionType] = $formattedRestrictionType; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $restrictions; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get all mail items in the inbox |
||
| 73 | * |
||
| 74 | * @param Type\FolderIdType |
||
| 75 | * @param array $options |
||
| 76 | * @return Type\MessageType[] |
||
| 77 | */ |
||
| 78 | public function getMailItems($folderId = null, $options = array()) |
||
| 79 | { |
||
| 80 | if (!$folderId) { |
||
| 81 | $folderId = $this->getFolderId(); |
||
| 82 | } |
||
| 83 | |||
| 84 | $request = array( |
||
| 85 | 'Traversal' => 'Shallow', |
||
| 86 | 'ItemShape' => array( |
||
| 87 | 'BaseShape' => 'AllProperties' |
||
| 88 | ), |
||
| 89 | 'ParentFolderIds' => array( |
||
| 90 | 'FolderId' => $folderId->toXmlObject() |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | |||
| 94 | if (!empty($options['Restriction'])) { |
||
| 95 | $options['Restriction'] = $this->formatRestrictions($options['Restriction']); |
||
| 96 | } |
||
| 97 | |||
| 98 | $request = array_replace_recursive($request, $options); |
||
| 99 | |||
| 100 | $request = Type::buildFromArray($request); |
||
| 101 | return $this->getClient()->FindItem($request); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Type\FolderIdType $folderId |
||
| 106 | * @param array $options |
||
| 107 | * @return Type\MessageType[] |
||
| 108 | */ |
||
| 109 | public function getUnreadMailItems($folderId = null, $options = array()) |
||
| 110 | { |
||
| 111 | $unReadOption = array( |
||
| 112 | 'Restriction' => array( |
||
| 113 | 'IsEqualTo' => array( |
||
| 114 | 'IsRead' => false |
||
| 115 | ) |
||
| 116 | ) |
||
| 117 | ); |
||
| 118 | |||
| 119 | $options = array_replace_recursive($unReadOption, $options); |
||
| 120 | |||
| 121 | return $this->getMailItems($folderId, $options); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Updates a calendar item with changes |
||
| 126 | * |
||
| 127 | * @param $itemId Type\ItemIdType|Type |
||
| 128 | * @param $changes |
||
| 129 | * @return Type\MessageType[] |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function updateMailItem($itemId, $changes) |
|
| 132 | { |
||
| 133 | //Create the request |
||
| 134 | $request = array( |
||
| 135 | 'ItemChange' => array( |
||
| 136 | 'ItemId' => $itemId->toArray(), |
||
| 137 | 'Updates' => $this->buildUpdateItemChanges('Message', 'message', $changes) |
||
| 138 | ) |
||
| 139 | ); |
||
| 140 | |||
| 141 | $items = $this->updateItems($request); |
||
| 142 | |||
| 143 | if (!is_array($items)) { |
||
| 144 | $items = array($items); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $items; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param $mailItem Type\MessageType|Type\ItemIdType |
||
| 152 | * @param $isRead boolean |
||
| 153 | */ |
||
| 154 | public function markMailAsRead($mailItem, $isRead = true) |
||
| 155 | { |
||
| 156 | if ($mailItem instanceof Type\MessageType) { |
||
| 157 | $mailItem = $mailItem->getItemId(); |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->updateMailItem($mailItem, array( |
||
| 161 | 'IsRead' => $isRead |
||
| 162 | )); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function sendMail(MessageType $message, $options = array()) |
||
| 166 | { |
||
| 167 | $items = array('Message' => $message->toXmlObject()); |
||
| 168 | $defaultOptions = array( |
||
| 169 | 'MessageDisposition' => 'SendAndSaveCopy', |
||
| 170 | ); |
||
| 171 | |||
| 172 | if ($this->getPrimarySmtpMailbox() != null) { |
||
| 173 | $sentItems = $this->getFolderByDistinguishedId('sentitems')->getFolderId(); |
||
| 174 | $defaultOptions['SavedItemFolderId'] = |
||
| 175 | array('FolderId' => $sentItems->toXmlObject()); |
||
| 176 | } |
||
| 177 | |||
| 178 | $options = array_replace_recursive($defaultOptions, $options); |
||
| 179 | |||
| 180 | return $this->createItems($items, $options); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getAttachment(Type\AttachmentIdType $attachmentId) |
||
| 197 | } |
||
| 198 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: