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 |
||
| 8 | class ContactsAPI extends API |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var Type\FolderIdType |
||
| 12 | */ |
||
| 13 | protected $folderId; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Pick a Contacts based on it's folder name |
||
| 17 | * |
||
| 18 | * @param string|null $displayName |
||
| 19 | * @return $this |
||
| 20 | * @throws Exception |
||
| 21 | */ |
||
| 22 | public function pickContactsFolder($displayName = null) |
||
| 23 | { |
||
| 24 | View Code Duplication | if ($displayName == 'default.contacts' || $displayName == null) { |
|
| 25 | $folder = $this->getFolderByDistinguishedId('contacts'); |
||
| 26 | } else { |
||
| 27 | $folder = $this->getFolderByDisplayName($displayName, 'contacts'); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (!$folder) { |
||
| 31 | throw new Exception('Folder does not exist'); |
||
| 32 | } |
||
| 33 | |||
| 34 | $this->folderId = $folder->getFolderId(); |
||
| 35 | return $this; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return Type\FolderIdType |
||
| 40 | */ |
||
| 41 | 5 | public function getFolderId() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param Type\FolderIdType $folderId |
||
| 52 | */ |
||
| 53 | 5 | public function setFolderId($folderId) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param Type\FolderIdType $folderId |
||
| 60 | * @param array $options |
||
| 61 | * @return Type\ContactItemType[] |
||
| 62 | */ |
||
| 63 | 2 | View Code Duplication | public function getContacts($folderId = null, $options = array()) |
| 85 | |||
| 86 | /** |
||
| 87 | * @param Type\ItemIdType $itemId |
||
| 88 | * @return Type\ContactItemType |
||
| 89 | */ |
||
| 90 | 3 | public function getContact($itemId) |
|
| 91 | { |
||
| 92 | 3 | return $this->getItem($itemId); |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param $contacts |
||
| 97 | * @param array $options |
||
| 98 | * @return Type\ItemIdType[] |
||
| 99 | */ |
||
| 100 | 5 | View Code Duplication | public function createContacts($contacts, $options = array()) |
| 118 | |||
| 119 | 2 | View Code Duplication | public function updateContactItem(Type\ItemIdType $itemId, $changes) |
| 139 | } |
||
| 140 |