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 |
||
19 | class API |
||
20 | { |
||
21 | protected static $defaultClientOptions = array( |
||
22 | 'version' => ExchangeWebServices::VERSION_2010 |
||
23 | ); |
||
24 | |||
25 | 35 | public function __construct(ExchangeWebServices $client = null) |
|
31 | |||
32 | /** |
||
33 | * @return Type\EmailAddressType |
||
34 | */ |
||
35 | 27 | public function getPrimarySmtpMailbox() |
|
39 | |||
40 | /** |
||
41 | * Storing the API client |
||
42 | * @var ExchangeWebServices |
||
43 | */ |
||
44 | private $client; |
||
45 | |||
46 | /** |
||
47 | * Get a calendar item |
||
48 | * |
||
49 | * @param string $name |
||
50 | * @return CalendarAPI |
||
51 | */ |
||
52 | 6 | public function getCalendar($name = null) |
|
60 | |||
61 | /** |
||
62 | * @param string $folderName |
||
63 | * @return MailAPI |
||
64 | */ |
||
65 | 7 | public function getMailbox($folderName = null) |
|
73 | |||
74 | /** |
||
75 | * Set the API client |
||
76 | * |
||
77 | * @param ExchangeWebServices $client |
||
78 | * @return $this |
||
79 | */ |
||
80 | 35 | public function setClient($client) |
|
86 | |||
87 | /** |
||
88 | * Get the API client |
||
89 | * |
||
90 | * @return ExchangeWebServices |
||
91 | */ |
||
92 | 34 | public function getClient() |
|
96 | |||
97 | 34 | public static function withUsernameAndPassword($server, $username, $password, $options = []) |
|
106 | |||
107 | 1 | public static function withCallbackToken($server, $token, $options = []) |
|
115 | |||
116 | 1 | public static function withCustomAuthentication($server, $authentication, $options = []) |
|
124 | |||
125 | 1 | public function getPrimarySmptEmailAddress() |
|
133 | |||
134 | public function setPrimarySmtpEmailAddress($emailAddress) |
||
140 | |||
141 | 16 | /** |
|
142 | * Create items through the API client |
||
143 | * |
||
144 | 16 | * @param $items |
|
145 | * @param array $options |
||
146 | 16 | * @return Type |
|
147 | 16 | */ |
|
148 | public function createItems($items, $options = array()) |
||
162 | 4 | ||
163 | public function updateItems($items, $options = array()) |
||
182 | |||
183 | View Code Duplication | public function createCalendars($names, BaseFolderIdType $parentFolder = null, $options = array()) |
|
191 | |||
192 | 4 | View Code Duplication | public function createContactsFolder($names, BaseFolderIdType $parentFolder = null, $options = array()) |
200 | 4 | ||
201 | 4 | public function createFolders($names, BaseFolderIdType $parentFolder, $options = array(), $folderClass = null) |
|
221 | |||
222 | 3 | /** |
|
223 | * @deprecated Please use API::deleteFolders() instead |
||
224 | * |
||
225 | 4 | * @param BaseFolderIdType $folderId |
|
226 | * @param array $options |
||
227 | 4 | * @return Type |
|
228 | */ |
||
229 | public function deleteFolder(BaseFolderIdType $folderId, $options = array()) |
||
233 | |||
234 | 4 | public function deleteFolders($folders, $options = array()) |
|
246 | |||
247 | public function moveItem(Type\ItemIdType $itemId, BaseFolderIdType $folderId, $options = array()) |
||
258 | |||
259 | 16 | /** |
|
260 | 16 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
|
261 | * @param array $options |
||
262 | 16 | * @return bool |
|
263 | 16 | */ |
|
264 | public function deleteItems($items, $options = array()) |
||
286 | |||
287 | 8 | /** |
|
288 | 8 | * @param $identifier |
|
289 | * @param array $options |
||
290 | 8 | * @return Type\BaseFolderType |
|
291 | */ |
||
292 | 8 | View Code Duplication | public function getFolder($identifier, $options = []) |
309 | |||
310 | 4 | /** |
|
311 | * Get a folder by it's distinguishedId |
||
312 | 4 | * |
|
313 | 4 | * @param string $distinguishedId |
|
314 | 4 | * @param array $options |
|
315 | 4 | * @return Type\BaseFolderType |
|
316 | */ |
||
317 | public function getFolderByDistinguishedId($distinguishedId, $options = []) |
||
326 | 4 | ||
327 | /** |
||
328 | * @param string|BaseFolderIdType $folderId |
||
329 | 4 | * @param array $options |
|
330 | * @return Type\BaseFolderType |
||
331 | * @throws API\Exception |
||
332 | 4 | */ |
|
333 | public function getFolderByFolderId($folderId, $options = []) |
||
343 | 17 | ||
344 | 17 | /** |
|
345 | * @param string|BaseFolderIdType $parentFolderId |
||
346 | * @param array $options |
||
347 | 25 | * @return Type\BaseFolderType[] |
|
348 | */ |
||
349 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', array $options = array()) |
|
370 | 25 | ||
371 | /** |
||
372 | 25 | * @param string $folderName |
|
373 | 25 | * @param string|BaseFolderIdType $parentFolderId |
|
374 | 24 | * @param array $options |
|
375 | * @return bool|Type\BaseFolderType |
||
376 | 17 | */ |
|
377 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
||
389 | 4 | ||
390 | 4 | /** |
|
391 | * @param $itemId array|Type\ItemIdType |
||
392 | * @param array $options |
||
393 | 5 | * @return Type |
|
394 | 5 | */ |
|
395 | 5 | View Code Duplication | public function getItem($itemId, $options = array()) |
410 | 2 | ||
411 | /** |
||
412 | * Get a list of sync changes on a folder |
||
413 | 2 | * |
|
414 | 2 | * @param BaseFolderIdType $folderId |
|
415 | 2 | * @param null $syncState |
|
416 | * @param array $options |
||
417 | 2 | * @return SyncFolderItemsResponseMessageType |
|
418 | */ |
||
419 | 2 | public function listItemChanges($folderId, $syncState = null, array $options = array()) |
|
440 | |||
441 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
456 | |||
457 | /** |
||
458 | * @param Type\ItemIdType $itemId |
||
459 | * @param $fromType |
||
460 | * @param $destinationType |
||
461 | * @param $mailbox |
||
462 | * |
||
463 | * @return Type\ItemIdType |
||
464 | */ |
||
465 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
482 | |||
483 | /** |
||
484 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
485 | 2 | * |
|
486 | 2 | * @return Type\FindItemParentType|Type\FindFolderParentType |
|
487 | */ |
||
488 | 2 | public function getNextPage($result) |
|
506 | |||
507 | /** |
||
508 | * @param BaseFolderIdType $folderId |
||
509 | * @param string $deleteType |
||
510 | * @param bool $deleteSubFolders |
||
511 | * @param array $options |
||
512 | * @return EmptyFolderResponseType |
||
513 | */ |
||
514 | public function emptyFolder( |
||
530 | |||
531 | protected function getDistinguishedFolderId($id = null, $changeKey = null) |
||
539 | } |
||
540 |