Complex classes like ExchangeWebServices 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 ExchangeWebServices, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
86 | class ExchangeWebServices |
||
87 | { |
||
88 | const VERSION_2007 = 'Exchange2007'; |
||
89 | |||
90 | const VERSION_2007_SP1 = 'Exchange2007_SP1'; |
||
91 | |||
92 | const VERSION_2010 = 'Exchange2010'; |
||
93 | |||
94 | const VERSION_2010_SP1 = 'Exchange2010_SP1'; |
||
95 | |||
96 | const VERSION_2010_SP2 = 'Exchange2010_SP2'; |
||
97 | |||
98 | const VERSION_2013 = 'Exchange2013'; |
||
99 | |||
100 | const VERSION_2013_SP1 = 'Exchange2013_SP1'; |
||
101 | |||
102 | /** |
||
103 | * Password to use when connecting to the Exchange server. |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $password = null; |
||
108 | |||
109 | /** |
||
110 | * Location of the Exchange server. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | protected $server = null; |
||
115 | |||
116 | /** |
||
117 | * SOAP client used to make the request |
||
118 | * |
||
119 | * @var NTLMSoapClient |
||
120 | */ |
||
121 | protected $soap = null; |
||
122 | |||
123 | /** |
||
124 | * Username to use when connecting to the Exchange server. |
||
125 | * |
||
126 | * @var string |
||
127 | */ |
||
128 | protected $username = null; |
||
129 | |||
130 | /** |
||
131 | * @var EmailAddressType |
||
132 | */ |
||
133 | protected $primarySmtpMailbox = null; |
||
134 | |||
135 | protected static $middlewareStack = false; |
||
136 | |||
137 | /** |
||
138 | * A setting to check whether or not responses should be drilled down before being |
||
139 | * returned. Setting this to false |
||
140 | * will return the raw responses without any filtering |
||
141 | * |
||
142 | * @var bool |
||
143 | */ |
||
144 | protected $drillDownResponses = true; |
||
145 | |||
146 | /** |
||
147 | * Miscrosoft Exchange version that we are going to connect to |
||
148 | * |
||
149 | * @var string |
||
150 | */ |
||
151 | protected $version = null; |
||
152 | |||
153 | protected $options = null; |
||
154 | |||
155 | /** |
||
156 | * The timezone for the client |
||
157 | * |
||
158 | * @var bool |
||
159 | */ |
||
160 | protected $timezone = false; |
||
161 | |||
162 | /** |
||
163 | * @return EmailAddressType |
||
164 | */ |
||
165 | 25 | public function getPrimarySmtpMailbox() |
|
166 | { |
||
167 | 25 | return $this->primarySmtpMailbox; |
|
168 | } |
||
169 | |||
170 | 1 | public function getPrimarySmtpEmailAddress() |
|
171 | { |
||
172 | 1 | if ($this->primarySmtpMailbox == null) { |
|
173 | 1 | return null; |
|
174 | } |
||
175 | |||
176 | 1 | return $this->primarySmtpMailbox->getEmailAddress(); |
|
177 | } |
||
178 | |||
179 | 2 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
180 | { |
||
181 | 2 | $mailbox = new EmailAddressType(); |
|
182 | 2 | $mailbox->setEmailAddress($emailAddress); |
|
183 | 2 | $this->primarySmtpMailbox = $mailbox; |
|
184 | |||
185 | 2 | return $this; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param boolean $timezone |
||
190 | */ |
||
191 | public function setTimezone($timezone) |
||
192 | { |
||
193 | $this->timezone = $timezone; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getVersion() |
||
200 | { |
||
201 | return $this->version; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getServer() |
||
208 | { |
||
209 | return $this->server; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Constructor for the ExchangeWebServices class |
||
214 | * |
||
215 | * @param string $server |
||
216 | * @param string $username |
||
217 | * @param string $password |
||
218 | * @param array $options |
||
219 | */ |
||
220 | 33 | protected function __construct($server = null, $username = null, $password = null, $options = array()) |
|
221 | { |
||
222 | 33 | if ($server !== null) { |
|
223 | $this->createClient( |
||
224 | $server, |
||
225 | ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), |
||
226 | $options |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | 33 | $this->buildMiddlewareStack(); |
|
231 | 33 | } |
|
232 | |||
233 | 32 | public static function fromUsernameAndPassword($server, $username, $password, $options) |
|
234 | { |
||
235 | 32 | $self = new self(); |
|
236 | 32 | $self->createClient($server, ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), $options); |
|
237 | 32 | $self->options = $options; |
|
238 | |||
239 | 32 | return $self; |
|
240 | } |
||
241 | |||
242 | 1 | public static function fromCallbackToken($server, $token, $options) |
|
243 | { |
||
244 | 1 | $self = new self(); |
|
245 | 1 | $self->createClient($server, ExchangeWebServicesAuth::fromCallbackToken($token), $options); |
|
246 | 1 | $self->options = $options; |
|
247 | |||
248 | 1 | return $self; |
|
249 | } |
||
250 | |||
251 | 33 | protected function createClient($server, $auth, $options) |
|
252 | { |
||
253 | 33 | $location = 'https://' . $this->cleanServerUrl($server) . '/EWS/Exchange.asmx'; |
|
254 | |||
255 | 33 | $options = array_replace_recursive([ |
|
256 | 33 | 'version' => self::VERSION_2007, |
|
257 | 33 | 'trace' => 1, |
|
258 | 33 | 'exceptions' => true, |
|
259 | 33 | 'classmap' => ClassMap::getClassMap(), |
|
260 | 'drillDownResponses' => true |
||
261 | 33 | ], $options); |
|
262 | |||
263 | 33 | $this->server = $server; |
|
264 | 33 | $this->version = $options['version']; |
|
265 | |||
266 | 33 | $this->soap = new NTLMSoapClient( |
|
267 | 33 | $location, |
|
268 | 33 | $auth, |
|
269 | 33 | dirname(__FILE__) . '/../../Resources/wsdl/services.wsdl', |
|
270 | $options |
||
271 | 33 | ); |
|
272 | |||
273 | 33 | if (isset($options['primarySmtpEmailAddress'])) { |
|
274 | 1 | $this->setPrimarySmtpEmailAddress($options['primarySmtpEmailAddress']); |
|
275 | 1 | } |
|
276 | |||
277 | 33 | if (isset($options['impersonation'])) { |
|
278 | 1 | $this->setPrimarySmtpEmailAddress($options['impersonation']); |
|
279 | 1 | } |
|
280 | |||
281 | 33 | $this->drillDownResponses = $options['drillDownResponses']; |
|
282 | 33 | } |
|
283 | |||
284 | /** |
||
285 | * @codeCoverageIgnore |
||
286 | * |
||
287 | * @param $name |
||
288 | * @param $arguments |
||
289 | * @return Type |
||
290 | * @throws \garethp\ews\API\Exception |
||
291 | */ |
||
292 | public function __call($name, $arguments) |
||
293 | { |
||
294 | $request = MiddlewareRequest::newRequest($name, $arguments, $this->options); |
||
295 | $response = $this->executeMiddlewareStack(self::$middlewareStack, $request); |
||
|
|||
296 | $response = $response->getResponse(); |
||
297 | return $response; |
||
298 | return $this->processResponse($response); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Returns the SOAP Client that may be used to make calls against the server |
||
303 | * |
||
304 | * @return NTLMSoapClient |
||
305 | */ |
||
306 | 31 | public function getClient() |
|
310 | |||
311 | /** |
||
312 | * Sets the client |
||
313 | * |
||
314 | * @param NTLMSoapClient $client |
||
315 | * @return $this |
||
316 | */ |
||
317 | 2 | public function setClient($client) |
|
323 | |||
324 | /** |
||
325 | * Cleans the server URL for usage |
||
326 | * |
||
327 | * @param $server |
||
328 | * @return string |
||
329 | */ |
||
330 | 40 | public function cleanServerUrl($server) |
|
351 | |||
352 | /** |
||
353 | * Process a response to verify that it succeeded and take the appropriate |
||
354 | * action |
||
355 | * |
||
356 | * @param \garethp\ews\API\Message\BaseResponseMessageType $response |
||
357 | * @return Type[] |
||
358 | * @throws \garethp\ews\API\Exception |
||
359 | */ |
||
360 | 29 | protected function processResponse($response) |
|
361 | { |
||
362 | // If the soap call failed then we need to thow an exception. |
||
379 | |||
380 | /** |
||
381 | * @param $response |
||
382 | * @return array |
||
383 | * @throws \garethp\ews\API\Exception |
||
384 | */ |
||
385 | public function drillDownResponseLevels($response) |
||
409 | |||
410 | /** |
||
411 | * @param $response |
||
412 | * @return array |
||
413 | * @throws ExchangeException |
||
414 | */ |
||
415 | protected function getItemsFromResponse($response) |
||
437 | |||
438 | /** |
||
439 | * @param Message\BaseResponseMessageType $response |
||
440 | * @param $code |
||
441 | * @throws ExchangeException |
||
442 | * @throws NoResponseReturnedException |
||
443 | * @throws ServiceUnavailableException |
||
444 | * @throws UnauthorizedException |
||
445 | */ |
||
446 | 29 | protected function handleNonSuccessfulResponses($response, $code) |
|
464 | |||
465 | 33 | protected function buildMiddlewareStack() |
|
531 | |||
532 | /** |
||
533 | * @param array $middlewareStack |
||
534 | * @param MiddlewareRequest $request |
||
535 | * @return MiddlewareResponse |
||
536 | */ |
||
537 | 27 | protected function executeMiddlewareStack(array $middlewareStack, MiddlewareRequest $request) |
|
562 | } |
||
563 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: