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 |
||
84 | class ExchangeWebServices |
||
85 | { |
||
86 | const VERSION_2007 = 'Exchange2007'; |
||
87 | |||
88 | const VERSION_2007_SP1 = 'Exchange2007_SP1'; |
||
89 | |||
90 | const VERSION_2010 = 'Exchange2010'; |
||
91 | |||
92 | const VERSION_2010_SP1 = 'Exchange2010_SP1'; |
||
93 | |||
94 | const VERSION_2010_SP2 = 'Exchange2010_SP2'; |
||
95 | |||
96 | const VERSION_2013 = 'Exchange2013'; |
||
97 | |||
98 | const VERSION_2013_SP1 = 'Exchange2013_SP1'; |
||
99 | |||
100 | /** |
||
101 | * Password to use when connecting to the Exchange server. |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $password = null; |
||
106 | |||
107 | /** |
||
108 | * Location of the Exchange server. |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $server = null; |
||
113 | |||
114 | /** |
||
115 | * SOAP client used to make the request |
||
116 | * |
||
117 | * @var NTLMSoapClient |
||
118 | */ |
||
119 | protected $soap = null; |
||
120 | |||
121 | /** |
||
122 | * Username to use when connecting to the Exchange server. |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $username = null; |
||
127 | |||
128 | /** |
||
129 | * @var EmailAddressType |
||
130 | */ |
||
131 | protected $primarySmtpMailbox = null; |
||
132 | |||
133 | protected static $middlewareStack = []; |
||
134 | |||
135 | /** |
||
136 | * A setting to check whether or not responses should be drilled down before being |
||
137 | * returned. Setting this to false |
||
138 | * will return the raw responses without any filtering |
||
139 | * |
||
140 | * @var bool |
||
141 | */ |
||
142 | protected $drillDownResponses = true; |
||
143 | |||
144 | /** |
||
145 | * Miscrosoft Exchange version that we are going to connect to |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | protected $version = null; |
||
150 | |||
151 | protected $options = null; |
||
152 | |||
153 | /** |
||
154 | * The timezone for the client |
||
155 | * |
||
156 | * @var bool |
||
157 | */ |
||
158 | protected $timezone = false; |
||
159 | |||
160 | /** |
||
161 | * @return EmailAddressType |
||
162 | */ |
||
163 | 25 | public function getPrimarySmtpMailbox() |
|
167 | |||
168 | 1 | public function getPrimarySmtpEmailAddress() |
|
176 | |||
177 | 2 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
185 | |||
186 | /** |
||
187 | * @param boolean $timezone |
||
188 | */ |
||
189 | public function setTimezone($timezone) |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getVersion() |
||
198 | { |
||
199 | return $this->version; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getServer() |
||
209 | |||
210 | /** |
||
211 | * Constructor for the ExchangeWebServices class |
||
212 | * |
||
213 | * @param string $server |
||
214 | * @param string $username |
||
215 | * @param string $password |
||
216 | * @param array $options |
||
217 | */ |
||
218 | 33 | protected function __construct($server = null, $username = null, $password = null, $options = array()) |
|
230 | |||
231 | 32 | public static function fromUsernameAndPassword($server, $username, $password, $options) |
|
239 | |||
240 | 1 | public static function fromCallbackToken($server, $token, $options) |
|
248 | |||
249 | 33 | protected function createClient($server, $auth, $options) |
|
250 | { |
||
251 | 33 | $location = 'https://' . $this->cleanServerUrl($server) . '/EWS/Exchange.asmx'; |
|
252 | |||
253 | 33 | $options = array_replace_recursive([ |
|
254 | 33 | 'version' => self::VERSION_2007, |
|
255 | 33 | 'trace' => 1, |
|
256 | 33 | 'exceptions' => true, |
|
257 | 33 | 'classmap' => ClassMap::getClassMap(), |
|
258 | 'drillDownResponses' => true |
||
259 | 33 | ], $options); |
|
260 | |||
261 | 33 | $this->server = $server; |
|
262 | 33 | $this->version = $options['version']; |
|
263 | |||
264 | 33 | $this->soap = new NTLMSoapClient( |
|
265 | 33 | $location, |
|
266 | 33 | $auth, |
|
267 | 33 | dirname(__FILE__) . '/../../Resources/wsdl/services.wsdl', |
|
268 | $options |
||
269 | 33 | ); |
|
270 | |||
271 | 33 | if (isset($options['primarySmtpEmailAddress'])) { |
|
272 | 1 | $this->setPrimarySmtpEmailAddress($options['primarySmtpEmailAddress']); |
|
273 | 1 | } |
|
274 | |||
275 | 33 | if (isset($options['impersonation'])) { |
|
276 | 1 | $this->setPrimarySmtpEmailAddress($options['impersonation']); |
|
277 | 1 | } |
|
278 | |||
279 | 33 | $this->drillDownResponses = $options['drillDownResponses']; |
|
280 | 33 | } |
|
281 | |||
282 | /** |
||
283 | * @codeCoverageIgnore |
||
284 | * |
||
285 | * @param $name |
||
286 | * @param $arguments |
||
287 | * @return Type |
||
288 | * @throws \garethp\ews\API\Exception |
||
289 | */ |
||
290 | public function __call($name, $arguments) |
||
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) |
|
379 | |||
380 | /** |
||
381 | * @param $response |
||
382 | * @return array |
||
383 | * @throws \garethp\ews\API\Exception |
||
384 | */ |
||
385 | 27 | public function drillDownResponseLevels($response) |
|
409 | |||
410 | /** |
||
411 | * @param $response |
||
412 | * @return array |
||
413 | * @throws ExchangeException |
||
414 | */ |
||
415 | 27 | 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() |
|
466 | { |
||
491 | |||
492 | 27 | protected function executeMiddlewareStack(array $middlewareStack, $name, $request, $options) |
|
502 | } |
||
503 |
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: