Complex classes like XmppClient 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 XmppClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 80 | class XmppClient extends XmlStream implements ContainerInterface |
||
| 81 | { |
||
| 82 | use ServiceManager, Accessors; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Connector used to instantiate stream connection to server. |
||
| 86 | * |
||
| 87 | * @var Connector |
||
| 88 | */ |
||
| 89 | private $_connector; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Client's jid (Jabber Identifier) address. |
||
| 93 | * |
||
| 94 | * @var Jid |
||
| 95 | */ |
||
| 96 | private $_jid; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Dependency container used as service manager. |
||
| 100 | * |
||
| 101 | * @var Container |
||
| 102 | */ |
||
| 103 | private $_container; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Features provided by that stream |
||
| 107 | * |
||
| 108 | * @var Features |
||
| 109 | */ |
||
| 110 | private $_features; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Current client state. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private $_state = 'disconnected'; |
||
| 118 | private $_lang; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * XmppClient constructor. |
||
| 122 | * @param Jid $jid |
||
| 123 | * @param array $options { |
||
| 124 | * @var XmlParser $parser Parser used for interpreting streams. |
||
| 125 | * @var Component[] $modules Additional modules registered when creating client. |
||
| 126 | * @var string $language Stream language (reflects xml:language attribute) |
||
| 127 | * @var ContainerInterface $container Dependency container used for module management. |
||
| 128 | * @var bool $default-modules Load default modules or not |
||
| 129 | * } |
||
| 130 | */ |
||
| 131 | 9 | public function __construct(Jid $jid, array $options = []) |
|
| 166 | |||
| 167 | 8 | public function applyOptions(array $options) |
|
| 190 | |||
| 191 | public function start(array $attributes = []) |
||
| 199 | |||
| 200 | public function connect() |
||
| 206 | |||
| 207 | public function bind($jid) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Registers module in client's dependency container. |
||
| 224 | * |
||
| 225 | * @param ComponentInterface $module Module to be registered |
||
| 226 | * @param bool|string|array $alias Module alias, class name by default. |
||
| 227 | * `true` for aliasing interfaces and parents too, |
||
| 228 | * `false` for aliasing as class name only |
||
| 229 | * array for multiple aliases, |
||
| 230 | * and any string for alias name |
||
| 231 | */ |
||
| 232 | 8 | public function register(ComponentInterface $module, $alias = true) |
|
| 245 | |||
| 246 | 8 | private function _addToContainer(ComponentInterface $module, array $aliases) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Sends stanza to server and returns promise with server response. |
||
| 256 | * |
||
| 257 | * @param Stanza $stanza |
||
| 258 | * @return ExtendedPromiseInterface |
||
| 259 | */ |
||
| 260 | 3 | public function send(Stanza $stanza) : ExtendedPromiseInterface |
|
| 275 | |||
| 276 | private function handleConnect($stream) |
||
| 290 | |||
| 291 | //region Features |
||
| 292 | public function getFeatures() |
||
| 296 | //endregion |
||
| 297 | |||
| 298 | //region Parser |
||
| 299 | 8 | public function setParser(XmlParser $parser) |
|
| 308 | |||
| 309 | public function getParser() |
||
| 313 | //endregion |
||
| 314 | |||
| 315 | //region Connector |
||
| 316 | 8 | protected function setConnector($connector) |
|
| 333 | |||
| 334 | 2 | public function getConnector() |
|
| 338 | //endregion |
||
| 339 | |||
| 340 | //region Resource |
||
| 341 | public function setResource(string $resource) |
||
| 345 | |||
| 346 | public function getResource() |
||
| 350 | //endregion |
||
| 351 | |||
| 352 | //region Password |
||
| 353 | public function setPassword(string $password) |
||
| 357 | |||
| 358 | public function getPassword() |
||
| 362 | //endregion |
||
| 363 | |||
| 364 | //region Modules |
||
| 365 | 8 | public function setModules(array $modules) |
|
| 371 | //endregion |
||
| 372 | |||
| 373 | //region State |
||
| 374 | public function setState($state) |
||
| 379 | |||
| 380 | 8 | public function getState() |
|
| 384 | //endregion |
||
| 385 | |||
| 386 | //region Container |
||
| 387 | 8 | protected function getContainer() : ContainerInterface |
|
| 391 | |||
| 392 | 8 | protected function setContainer(Container $container) |
|
| 396 | //endregion |
||
| 397 | |||
| 398 | //region Language |
||
| 399 | public function getLanguage(): string |
||
| 403 | |||
| 404 | 8 | public function setLanguage(string $language) |
|
| 408 | //endregion |
||
| 409 | |||
| 410 | //region JID |
||
| 411 | public function getJid() |
||
| 415 | |||
| 416 | 8 | protected function setJid(Jid $jid) |
|
| 420 | //endregion |
||
| 421 | |||
| 422 | //region Roster |
||
| 423 | /** |
||
| 424 | * @return Roster |
||
| 425 | */ |
||
| 426 | public function getRoster(): Roster |
||
| 430 | //endregion |
||
| 431 | } |
||
| 432 |