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 |
||
17 | class API |
||
18 | { |
||
19 | protected static $defaultClientOptions = array( |
||
20 | 'version' => ExchangeWebServices::VERSION_2010 |
||
21 | ); |
||
22 | |||
23 | 23 | public function __construct(ExchangeWebServices $client = null) |
|
29 | |||
30 | /** |
||
31 | * @return Type\EmailAddressType |
||
32 | */ |
||
33 | 15 | public function getPrimarySmtpMailbox() |
|
37 | |||
38 | private $fieldUris = array(); |
||
39 | |||
40 | /** |
||
41 | * Storing the API client |
||
42 | * @var ExchangeWebServices |
||
43 | */ |
||
44 | private $client; |
||
45 | |||
46 | 4 | public function setupFieldUris() |
|
71 | |||
72 | 4 | public function getFieldUriByName($fieldName, $preference = 'item') |
|
95 | |||
96 | /** |
||
97 | * Get a calendar item |
||
98 | * |
||
99 | * @param string $name |
||
100 | * @return CalendarAPI |
||
101 | */ |
||
102 | 6 | public function getCalendar($name = null) |
|
110 | |||
111 | /** |
||
112 | * @param string $folderName |
||
113 | * @return MailAPI |
||
114 | */ |
||
115 | 8 | public function getMailbox($folderName = null) |
|
123 | |||
124 | /** |
||
125 | * Set the API client |
||
126 | * |
||
127 | * @param ExchangeWebServices $client |
||
128 | * @return $this |
||
129 | */ |
||
130 | 23 | public function setClient($client) |
|
135 | |||
136 | /** |
||
137 | * Get the API client |
||
138 | * |
||
139 | * @return ExchangeWebServices |
||
140 | */ |
||
141 | 22 | public function getClient() |
|
145 | |||
146 | /** |
||
147 | * Instantiate and set a client (ExchangeWebServices) based on the parameters given |
||
148 | * |
||
149 | * @deprecated Since 0.6.3 |
||
150 | * @param $server |
||
151 | * @param $username |
||
152 | * @param $password |
||
153 | * @param array $options |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function buildClient( |
||
169 | |||
170 | 9 | public static function withUsernameAndPassword($server, $username, $password, $options = [ ]) |
|
179 | |||
180 | 1 | public static function withCallbackToken($server, $token, $options = [ ]) |
|
188 | |||
189 | public function getPrimarySmptEmailAddress() |
||
197 | |||
198 | public function setPrimarySmtpEmailAddress($emailAddress) |
||
204 | |||
205 | /** |
||
206 | * Create items through the API client |
||
207 | * |
||
208 | * @param $items |
||
209 | * @param array $options |
||
210 | * @return API\CreateItemResponseType |
||
211 | */ |
||
212 | 9 | public function createItems($items, $options = array()) |
|
229 | |||
230 | 2 | public function updateItems($items, $options = array()) |
|
244 | |||
245 | 2 | protected function buildUpdateItemChanges($itemType, $uriType, $changes) |
|
261 | |||
262 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
||
285 | |||
286 | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
||
297 | |||
298 | /** |
||
299 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
300 | * @param array $options |
||
301 | * @return bool |
||
302 | */ |
||
303 | 8 | public function deleteItems($items, $options = array()) |
|
333 | |||
334 | /** |
||
335 | * @param $identifier |
||
336 | * @return Type\BaseFolderType |
||
337 | */ |
||
338 | 15 | public function getFolder($identifier) |
|
351 | |||
352 | /** |
||
353 | * Get a folder by it's distinguishedId |
||
354 | * |
||
355 | * @param string $distinguishedId |
||
356 | * @return Type\BaseFolderType |
||
357 | */ |
||
358 | 15 | public function getFolderByDistinguishedId($distinguishedId) |
|
367 | |||
368 | /** |
||
369 | * @param $folderId |
||
370 | * @return Type\BaseFolderType |
||
371 | */ |
||
372 | 4 | public function getFolderByFolderId($folderId) |
|
378 | |||
379 | /** |
||
380 | * @param $folderName |
||
381 | * @param string|Type\FolderIdType $parentFolderId |
||
382 | * @param array $options |
||
383 | * @return bool|Type\BaseFolderType |
||
384 | */ |
||
385 | 14 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
417 | |||
418 | /** |
||
419 | * @param $itemId array|Type\ItemIdType |
||
420 | * @param array $options |
||
421 | * @return Type |
||
422 | */ |
||
423 | 1 | public function getItem($itemId, $options = array()) |
|
438 | |||
439 | /** |
||
440 | * Get a list of sync changes on a folder |
||
441 | * |
||
442 | * @param Type\FolderIdType $folderId |
||
443 | * @param null $syncState |
||
444 | * @param array $options |
||
445 | * @return mixed |
||
446 | */ |
||
447 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
467 | } |
||
468 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: