Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 39 | class Client |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | const VERSION = '4.0.0'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $token; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var LoggerInterface |
||
| 53 | */ |
||
| 54 | protected $logger; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var LoopInterface |
||
| 58 | */ |
||
| 59 | protected $loop; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $storage = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $retries = Evacuator::INFINITY_RETRIES; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var HttpAdapter|null |
||
| 73 | */ |
||
| 74 | private $http; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var StreamAdapter|null |
||
| 78 | */ |
||
| 79 | private $streaming; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Client constructor. |
||
| 83 | * @param string $token |
||
| 84 | * @param LoggerInterface $logger |
||
| 85 | */ |
||
| 86 | public function __construct(string $token, LoggerInterface $logger = null) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param int $count |
||
| 98 | * @return Client |
||
| 99 | */ |
||
| 100 | public function retries(int $count): Client |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function getRetriesCount(): int |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | public function clear() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return SyncAdapterInterface|AdapterInterface |
||
| 126 | */ |
||
| 127 | public function viaHttp(): SyncAdapterInterface |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return StreamAdapterInterface|AdapterInterface |
||
| 138 | */ |
||
| 139 | public function viaStream(): StreamAdapterInterface |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function connect() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $hookId |
||
| 158 | * @return WebHook |
||
| 159 | * @throws \InvalidArgumentException |
||
| 160 | */ |
||
| 161 | public function notify(string $hookId): WebHook |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $resource |
||
| 168 | * @return Groups|Messages|Rooms|Users|null|LoggerInterface|LoopInterface|string |
||
| 169 | */ |
||
| 170 | public function __get(string $resource) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $name |
||
| 205 | * @param $value |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function __set(string $name, $value) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return array |
||
| 231 | * @throws \RuntimeException |
||
| 232 | * @throws \InvalidArgumentException |
||
| 233 | */ |
||
| 234 | public function authUser(): array |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return string |
||
| 241 | * @throws \RuntimeException |
||
| 242 | * @throws \InvalidArgumentException |
||
| 243 | */ |
||
| 244 | public function authId(): string |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param LoopInterface|null $loop |
||
| 251 | * @return LoopInterface |
||
| 252 | */ |
||
| 253 | public function loop(LoopInterface $loop = null): LoopInterface |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string|null $token |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function token(string $token = null): string |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param LoggerInterface|null $logger |
||
| 277 | * @return LoggerInterface |
||
| 278 | */ |
||
| 279 | public function logger(LoggerInterface $logger = null): LoggerInterface |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $name |
||
| 290 | * @throws \LogicException |
||
| 291 | */ |
||
| 292 | public function __unset(string $name) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $name |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function __isset(string $name): bool |
||
| 324 | } |
||
| 325 |