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 | 6 | public function getFolderId() |
|
| 20 | { |
||
| 21 | 6 | if (!$this->folderId) { |
|
| 22 | 1 | $this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId(); |
|
|
|
|||
| 23 | 1 | } |
|
| 24 | |||
| 25 | 6 | return $this->folderId; |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Type\FolderIdType $folderId |
||
| 30 | */ |
||
| 31 | 1 | public function setFolderId($folderId) |
|
| 32 | { |
||
| 33 | 1 | $this->folderId = $folderId; |
|
| 34 | 1 | } |
|
| 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 | 1 | $this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId(); |
|
| 44 | 1 | return; |
|
| 45 | } |
||
| 46 | |||
| 47 | 6 | $folder = $this->getFolderByDisplayName($displayName, $parentFolder); |
|
| 48 | 6 | $this->folderId = $folder->getFolderId(); |
|
| 49 | 6 | } |
|
| 50 | |||
| 51 | 1 | protected function formatRestrictions($restrictions) |
|
| 52 | { |
||
| 53 | 1 | foreach ($restrictions as $restrictionType => $query) { |
|
| 54 | 1 | $formattedRestrictionType = array(); |
|
| 55 | 1 | foreach ($query as $key => $value) { |
|
| 56 | 1 | if ($value === false) { |
|
| 57 | 1 | $value = 'false'; |
|
| 58 | 1 | } |
|
| 59 | 1 | $formattedRestrictionType[] = array( |
|
| 60 | 1 | 'FieldURI' => array('FieldURI' => API\FieldURIManager::getFieldUriByName($key, 'message')), |
|
| 61 | 1 | 'FieldURIOrConstant' => array('Constant' => array('Value' => (string) $value)) |
|
| 62 | 1 | ); |
|
| 63 | 1 | } |
|
| 64 | |||
| 65 | 1 | $restrictions[$restrictionType] = $formattedRestrictionType; |
|
| 66 | 1 | } |
|
| 67 | |||
| 68 | 1 | 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 | 6 | public function getMailItems($folderId = null, $options = array()) |
|
| 79 | { |
||
| 80 | 6 | if (!$folderId) { |
|
| 81 | 6 | $folderId = $this->getFolderId(); |
|
| 82 | 6 | } |
|
| 83 | |||
| 84 | $request = array( |
||
| 85 | 6 | 'Traversal' => 'Shallow', |
|
| 86 | 'ItemShape' => array( |
||
| 87 | 'BaseShape' => 'AllProperties' |
||
| 88 | 6 | ), |
|
| 89 | 'ParentFolderIds' => array( |
||
| 90 | 6 | 'FolderId' => $folderId->toXmlObject() |
|
| 91 | 6 | ) |
|
| 92 | 6 | ); |
|
| 93 | |||
| 94 | 6 | if (!empty($options['Restriction'])) { |
|
| 95 | 1 | $options['Restriction'] = $this->formatRestrictions($options['Restriction']); |
|
| 96 | 1 | } |
|
| 97 | |||
| 98 | 6 | $request = array_replace_recursive($request, $options); |
|
| 99 | |||
| 100 | 6 | $request = Type::buildFromArray($request); |
|
| 101 | 6 | return $this->getClient()->FindItem($request); |
|
| 102 | 6 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param Type\FolderIdType $folderId |
||
| 106 | * @param array $options |
||
| 107 | * @return Type\MessageType[] |
||
| 108 | */ |
||
| 109 | 1 | public function getUnreadMailItems($folderId = null, $options = array()) |
|
| 110 | { |
||
| 111 | $unReadOption = array( |
||
| 112 | 'Restriction' => array( |
||
| 113 | 'IsEqualTo' => array( |
||
| 114 | 'IsRead' => false |
||
| 115 | 1 | ) |
|
| 116 | 1 | ) |
|
| 117 | 1 | ); |
|
| 118 | |||
| 119 | 1 | $options = array_replace_recursive($unReadOption, $options); |
|
| 120 | |||
| 121 | 1 | 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 | 1 | View Code Duplication | public function updateMailItem($itemId, $changes) |
| 149 | |||
| 150 | /** |
||
| 151 | * @param $mailItem Type\MessageType|Type\ItemIdType |
||
| 152 | * @param $isRead boolean |
||
| 153 | */ |
||
| 154 | 1 | public function markMailAsRead($mailItem, $isRead = true) |
|
| 155 | { |
||
| 164 | |||
| 165 | 3 | public function sendMail(MessageType $message, $options = array()) |
|
| 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: