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:
Complex classes like API often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use API, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class API |
||
22 | { |
||
23 | protected static $defaultClientOptions = array( |
||
24 | 'version' => ExchangeWebServices::VERSION_2010 |
||
25 | ); |
||
26 | |||
27 | 30 | public function __construct(ExchangeWebServices $client = null) |
|
28 | { |
||
29 | 30 | if ($client) { |
|
30 | 18 | $this->setClient($client); |
|
31 | 18 | } |
|
32 | 30 | } |
|
33 | |||
34 | /** |
||
35 | * @return Type\EmailAddressType |
||
36 | */ |
||
37 | 21 | public function getPrimarySmtpMailbox() |
|
41 | |||
42 | private $unIndexedFieldUris = array(); |
||
43 | private $dictionaryFieldUris = array(); |
||
44 | |||
45 | /** |
||
46 | * Storing the API client |
||
47 | * @var ExchangeWebServices |
||
48 | */ |
||
49 | private $client; |
||
50 | |||
51 | /** |
||
52 | * @param $className |
||
53 | * @return array |
||
54 | */ |
||
55 | 5 | public function getFieldUrisFromClass($className) |
|
89 | |||
90 | 5 | public function setupFieldUris() |
|
98 | |||
99 | 5 | public function getFieldUriByName($fieldName, $preference = 'item') |
|
122 | |||
123 | 1 | public function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false) |
|
124 | { |
||
125 | 1 | $fieldName = strtolower($fieldName); |
|
126 | 1 | $preference = strtolower($preference); |
|
127 | |||
128 | 1 | if (empty($this->dictionaryFieldUris)) { |
|
129 | $this->setupFieldUris(); |
||
130 | } |
||
131 | |||
132 | 1 | if (!isset($this->dictionaryFieldUris[$fieldName])) { |
|
133 | return false; |
||
134 | } |
||
135 | |||
136 | 1 | if (!isset($this->dictionaryFieldUris[$fieldName][$preference])) { |
|
137 | $preference = 'item'; |
||
138 | } |
||
139 | |||
140 | 1 | if (!isset($this->dictionaryFieldUris[$fieldName][$preference])) { |
|
141 | throw new ExchangeException("Could not find uri $preference:$fieldName"); |
||
142 | } |
||
143 | |||
144 | 1 | $fieldUri = $this->dictionaryFieldUris[$fieldName][$preference]; |
|
145 | 1 | if (is_array($fieldUri)) { |
|
146 | 1 | if (!$entryKey) { |
|
147 | throw new ExchangeException("Please enter a specific entry key for this fieldURI"); |
||
148 | } |
||
149 | |||
150 | 1 | $entryKey = strtolower($entryKey); |
|
151 | 1 | if (!isset($fieldUri[$entryKey])) { |
|
152 | throw new ExchangeException("Could not find uri $preference:$fieldName:$entryKey"); |
||
153 | } |
||
154 | |||
155 | 1 | $fieldUri = $fieldUri[$entryKey]; |
|
156 | 1 | } |
|
157 | |||
158 | 1 | return $fieldUri; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get a calendar item |
||
163 | * |
||
164 | * @param string $name |
||
165 | * @return CalendarAPI |
||
166 | */ |
||
167 | 6 | public function getCalendar($name = null) |
|
175 | |||
176 | /** |
||
177 | * @param string $folderName |
||
178 | * @return MailAPI |
||
179 | */ |
||
180 | 6 | public function getMailbox($folderName = null) |
|
188 | |||
189 | /** |
||
190 | * Set the API client |
||
191 | * |
||
192 | * @param ExchangeWebServices $client |
||
193 | * @return $this |
||
194 | */ |
||
195 | 30 | public function setClient($client) |
|
200 | |||
201 | /** |
||
202 | * Get the API client |
||
203 | * |
||
204 | * @return ExchangeWebServices |
||
205 | */ |
||
206 | 28 | public function getClient() |
|
210 | |||
211 | /** |
||
212 | * Instantiate and set a client (ExchangeWebServices) based on the parameters given |
||
213 | * |
||
214 | * @deprecated Since 0.6.3 |
||
215 | * @param $server |
||
216 | * @param $username |
||
217 | * @param $password |
||
218 | * @param array $options |
||
219 | * @return $this |
||
220 | */ |
||
221 | 12 | public function buildClient( |
|
234 | |||
235 | 17 | public static function withUsernameAndPassword($server, $username, $password, $options = [ ]) |
|
244 | |||
245 | 1 | public static function withCallbackToken($server, $token, $options = [ ]) |
|
253 | |||
254 | 1 | public function getPrimarySmptEmailAddress() |
|
262 | |||
263 | 1 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
269 | |||
270 | /** |
||
271 | * Create items through the API client |
||
272 | * |
||
273 | * @param $items |
||
274 | * @param array $options |
||
275 | * @return API\CreateItemResponseType |
||
276 | */ |
||
277 | 13 | View Code Duplication | public function createItems($items, $options = array()) |
294 | |||
295 | 3 | public function updateItems($items, $options = array()) |
|
309 | |||
310 | 3 | protected function getFieldURI($uriType, $key = null, $value = null) |
|
343 | |||
344 | 3 | protected function buildUpdateItemChanges($itemType, $uriType, $changes) |
|
376 | |||
377 | 3 | protected function splitDictionaryUpdateEntries($value) |
|
402 | |||
403 | 2 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
|
426 | |||
427 | 2 | View Code Duplication | public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
439 | |||
440 | View Code Duplication | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
|
451 | |||
452 | /** |
||
453 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
454 | * @param array $options |
||
455 | * @return bool |
||
456 | */ |
||
457 | 13 | public function deleteItems($items, $options = array()) |
|
487 | |||
488 | /** |
||
489 | * @param $identifier |
||
490 | * @return Type\BaseFolderType |
||
491 | */ |
||
492 | 20 | public function getFolder($identifier) |
|
505 | |||
506 | /** |
||
507 | * Get a folder by it's distinguishedId |
||
508 | * |
||
509 | * @param string $distinguishedId |
||
510 | * @return Type\BaseFolderType |
||
511 | */ |
||
512 | 20 | public function getFolderByDistinguishedId($distinguishedId) |
|
521 | |||
522 | /** |
||
523 | * @param $folderId |
||
524 | * @return Type\BaseFolderType |
||
525 | */ |
||
526 | 4 | public function getFolderByFolderId($folderId) |
|
532 | |||
533 | /** |
||
534 | * @param string|Type\FolderIdType $parentFolderId |
||
535 | * @param array $options |
||
536 | * @return bool|Type\BaseFolderType |
||
537 | */ |
||
538 | 19 | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
|
562 | |||
563 | /** |
||
564 | * @param $folderName |
||
565 | * @param string|Type\FolderIdType $parentFolderId |
||
566 | * @param array $options |
||
567 | * @return bool|Type\BaseFolderType |
||
568 | */ |
||
569 | 19 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
581 | |||
582 | /** |
||
583 | * @param $itemId array|Type\ItemIdType |
||
584 | * @param array $options |
||
585 | * @return Type |
||
586 | */ |
||
587 | 4 | View Code Duplication | public function getItem($itemId, $options = array()) |
602 | |||
603 | /** |
||
604 | * Get a list of sync changes on a folder |
||
605 | * |
||
606 | * @param Type\FolderIdType $folderId |
||
607 | * @param null $syncState |
||
608 | * @param array $options |
||
609 | * @return SyncFolderItemsResponseMessageType |
||
610 | */ |
||
611 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
631 | |||
632 | View Code Duplication | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
|
651 | |||
652 | /** |
||
653 | * @param Type\ItemIdType $itemId |
||
654 | * @param $fromType |
||
655 | * @param $destinationType |
||
656 | * @param $mailbox |
||
657 | * |
||
658 | * @return Type\ItemIdType |
||
659 | */ |
||
660 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
677 | } |
||
678 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.