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 | 26 | public function getPrimarySmtpMailbox() |
|
| 166 | { |
||
| 167 | 26 | 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 | 34 | protected function __construct($server = null, $username = null, $password = null, $options = array()) |
|
| 221 | { |
||
| 222 | 34 | if ($server !== null) { |
|
| 223 | $this->createClient( |
||
| 224 | $server, |
||
| 225 | ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), |
||
| 226 | $options |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | |||
| 230 | 34 | $this->buildMiddlewareStack(); |
|
| 231 | 34 | } |
|
| 232 | |||
| 233 | 33 | public static function fromUsernameAndPassword($server, $username, $password, $options) |
|
| 234 | { |
||
| 235 | 33 | $self = new self(); |
|
| 236 | 33 | $self->createClient($server, ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), $options); |
|
| 237 | 33 | $self->options = $options; |
|
| 238 | |||
| 239 | 33 | 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 | 34 | protected function createClient($server, $auth, $options) |
|
| 252 | { |
||
| 253 | 34 | $location = 'https://' . $this->cleanServerUrl($server) . '/EWS/Exchange.asmx'; |
|
| 254 | |||
| 255 | 34 | $options = array_replace_recursive([ |
|
| 256 | 34 | 'version' => self::VERSION_2007, |
|
| 257 | 34 | 'trace' => 1, |
|
| 258 | 34 | 'exceptions' => true, |
|
| 259 | 34 | 'classmap' => ClassMap::getClassMap(), |
|
| 260 | 'drillDownResponses' => true |
||
| 261 | 34 | ], $options); |
|
| 262 | |||
| 263 | 34 | $this->server = $server; |
|
| 264 | 34 | $this->version = $options['version']; |
|
| 265 | |||
| 266 | 34 | $this->soap = new NTLMSoapClient( |
|
| 267 | 34 | $location, |
|
| 268 | 34 | $auth, |
|
| 269 | 34 | dirname(__FILE__) . '/../../Resources/wsdl/services.wsdl', |
|
| 270 | $options |
||
| 271 | 34 | ); |
|
| 272 | |||
| 273 | 34 | if (isset($options['primarySmtpEmailAddress'])) { |
|
| 274 | 1 | $this->setPrimarySmtpEmailAddress($options['primarySmtpEmailAddress']); |
|
| 275 | 1 | } |
|
| 276 | |||
| 277 | 34 | if (isset($options['impersonation'])) { |
|
| 278 | 1 | $this->setPrimarySmtpEmailAddress($options['impersonation']); |
|
| 279 | 1 | } |
|
| 280 | |||
| 281 | 34 | $this->drillDownResponses = $options['drillDownResponses']; |
|
| 282 | 34 | } |
|
| 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 | 2 | public function getClient() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Cleans the server URL for usage |
||
| 313 | * |
||
| 314 | * @param $server |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | 41 | public function cleanServerUrl($server) |
|
| 318 | { |
||
| 319 | 41 | $url = parse_url($server); |
|
| 320 | 41 | if (!isset($url['host']) && isset($url['path'])) { |
|
| 321 | 36 | $url['host'] = $url['path']; |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Process a response to verify that it succeeded and take the appropriate |
||
| 341 | * action |
||
| 342 | * |
||
| 343 | * @param \garethp\ews\API\Message\BaseResponseMessageType $response |
||
| 344 | * @return Type[] |
||
| 345 | * @throws \garethp\ews\API\Exception |
||
| 346 | */ |
||
| 347 | protected function processResponse($response) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param $response |
||
| 369 | * @return array |
||
| 370 | * @throws \garethp\ews\API\Exception |
||
| 371 | */ |
||
| 372 | public function drillDownResponseLevels($response) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param $response |
||
| 399 | * @return array |
||
| 400 | * @throws ExchangeException |
||
| 401 | */ |
||
| 402 | protected function getItemsFromResponse($response) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param Message\BaseResponseMessageType $response |
||
| 427 | * @param $code |
||
| 428 | * @throws ExchangeException |
||
| 429 | * @throws NoResponseReturnedException |
||
| 430 | * @throws ServiceUnavailableException |
||
| 431 | * @throws UnauthorizedException |
||
| 432 | */ |
||
| 433 | protected function handleNonSuccessfulResponses($response, $code) |
||
| 451 | |||
| 452 | 34 | protected function buildMiddlewareStack() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @param array $middlewareStack |
||
| 476 | * @param MiddlewareRequest $request |
||
| 477 | * @return MiddlewareResponse |
||
| 478 | */ |
||
| 479 | 28 | protected function executeMiddlewareStack(array $middlewareStack, MiddlewareRequest $request) |
|
| 504 | } |
||
| 505 |
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: