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) |
|
24 | { |
||
25 | 23 | if ($client) { |
|
26 | 10 | $this->setClient($client); |
|
27 | 10 | } |
|
28 | 23 | } |
|
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() |
|
47 | { |
||
48 | //So, since we have to pass in URI's of everything we update, we need to fetch them |
||
49 | 4 | $reflection = new \ReflectionClass('jamesiarmes\PEWS\API\Enumeration\UnindexedFieldURIType'); |
|
50 | 4 | $constants = $reflection->getConstants(); |
|
51 | 4 | $constantsFound = array(); |
|
52 | |||
53 | //Loop through all URI's to list them in an array |
||
54 | 4 | foreach ($constants as $constant) { |
|
55 | 4 | $exploded = explode(":", $constant); |
|
56 | 4 | if (count($exploded) == 1) { |
|
57 | 4 | $exploded = ['item', $exploded[0]]; |
|
58 | 4 | } |
|
59 | |||
60 | 4 | $name = strtolower($exploded[1]); |
|
61 | 4 | $category = strtolower($exploded[0]); |
|
62 | |||
63 | 4 | if (!isset($constantsFound[$name])) { |
|
64 | 4 | $constantsFound[$name] = array(); |
|
65 | 4 | } |
|
66 | 4 | $constantsFound[$name][$category] = $constant; |
|
67 | 4 | } |
|
68 | |||
69 | 4 | $this->fieldUris = $constantsFound; |
|
70 | 4 | } |
|
71 | |||
72 | 4 | public function getFieldUriByName($fieldName, $preference = 'item') |
|
73 | { |
||
74 | 4 | $fieldName = strtolower($fieldName); |
|
75 | 4 | $preference = strtolower($preference); |
|
76 | |||
77 | 4 | if (empty($this->fieldUris)) { |
|
78 | 4 | $this->setupFieldUris(); |
|
79 | 4 | } |
|
80 | |||
81 | 4 | if (!isset($this->fieldUris[$fieldName])) { |
|
82 | 1 | return false; |
|
83 | } |
||
84 | |||
85 | 4 | if (!isset($this->fieldUris[$fieldName][$preference])) { |
|
86 | 1 | $preference = 'item'; |
|
87 | 1 | } |
|
88 | |||
89 | 4 | if (!isset($this->fieldUris[$fieldName][$preference])) { |
|
90 | throw new \Exception("Could not find uri $preference:$fieldName"); |
||
91 | } |
||
92 | |||
93 | 4 | return $this->fieldUris[$fieldName][$preference]; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get a calendar item |
||
98 | * |
||
99 | * @param string $name |
||
100 | * @return CalendarAPI |
||
101 | */ |
||
102 | 6 | public function getCalendar($name = null) |
|
103 | { |
||
104 | 6 | $calendar = new CalendarAPI(); |
|
105 | 6 | $calendar->setClient($this->getClient()); |
|
106 | 6 | $calendar->pickCalendar($name); |
|
107 | |||
108 | 6 | return $calendar; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $folderName |
||
113 | * @return MailAPI |
||
114 | */ |
||
115 | 8 | public function getMailbox($folderName = null) |
|
116 | { |
||
117 | 8 | $mailApi = new MailAPI(); |
|
118 | 6 | $mailApi->setClient($this->getClient()); |
|
119 | 6 | $mailApi->pickMailFolder($folderName); |
|
120 | |||
121 | 6 | return $mailApi; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Set the API client |
||
126 | * |
||
127 | * @param ExchangeWebServices $client |
||
128 | * @return $this |
||
129 | */ |
||
130 | 23 | public function setClient($client) |
|
131 | { |
||
132 | 23 | $this->client = $client; |
|
133 | 23 | return $this; |
|
134 | } |
||
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 string|Type\FolderIdType $parentFolderId |
||
381 | * @param array $options |
||
382 | * @return bool|Type\BaseFolderType |
||
383 | */ |
||
384 | 14 | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
|
408 | |||
409 | /** |
||
410 | * @param $folderName |
||
411 | * @param string|Type\FolderIdType $parentFolderId |
||
412 | * @param array $options |
||
413 | * @return bool|Type\BaseFolderType |
||
414 | */ |
||
415 | 14 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
427 | |||
428 | /** |
||
429 | * @param $itemId array|Type\ItemIdType |
||
430 | * @param array $options |
||
431 | * @return Type |
||
432 | */ |
||
433 | 1 | public function getItem($itemId, $options = array()) |
|
448 | |||
449 | /** |
||
450 | * Get a list of sync changes on a folder |
||
451 | * |
||
452 | * @param Type\FolderIdType $folderId |
||
453 | * @param null $syncState |
||
454 | * @param array $options |
||
455 | * @return mixed |
||
456 | */ |
||
457 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
477 | } |
||
478 |
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: