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 |
||
| 41 | class Client |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const VERSION = '4.0.6'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $token; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var LoggerInterface |
||
| 55 | */ |
||
| 56 | protected $logger; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var LoopInterface |
||
| 60 | */ |
||
| 61 | protected $loop; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $storage = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $retries = 100; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var HttpAdapter|null |
||
| 75 | */ |
||
| 76 | private $http; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var StreamAdapter|null |
||
| 80 | */ |
||
| 81 | private $streaming; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Client constructor. |
||
| 85 | * @param string $token |
||
| 86 | * @param LoggerInterface $logger |
||
| 87 | */ |
||
| 88 | public function __construct(string $token, LoggerInterface $logger = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $token |
||
| 100 | * @return Client |
||
| 101 | */ |
||
| 102 | public function updateToken(string $token): Client |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param int $count |
||
| 111 | * @return Client |
||
| 112 | */ |
||
| 113 | public function retries(int $count): Client |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return int |
||
| 122 | */ |
||
| 123 | public function getRetriesCount(): int |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function clear() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return SyncAdapterInterface|AdapterInterface |
||
| 139 | */ |
||
| 140 | public function viaHttp(): SyncAdapterInterface |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return StreamAdapterInterface|AdapterInterface |
||
| 151 | */ |
||
| 152 | public function viaStream(): StreamAdapterInterface |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | public function connect() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $hookId |
||
| 171 | * @return WebHook |
||
| 172 | * @throws \InvalidArgumentException |
||
| 173 | */ |
||
| 174 | public function notify(string $hookId): WebHook |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $resource |
||
| 181 | * @return Groups|Messages|Rooms|Users|null|LoggerInterface|LoopInterface|string |
||
| 182 | */ |
||
| 183 | public function __get(string $resource) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $name |
||
| 218 | * @param $value |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | public function __set(string $name, $value) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return array |
||
| 244 | * @throws \Throwable |
||
| 245 | * @throws \GuzzleHttp\Exception\ClientException |
||
| 246 | * @throws \Exception |
||
| 247 | * @throws \RuntimeException |
||
| 248 | * @throws \InvalidArgumentException |
||
| 249 | */ |
||
| 250 | public function authUser(): array |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return string |
||
| 257 | * @throws \Throwable |
||
| 258 | * @throws \GuzzleHttp\Exception\ClientException |
||
| 259 | * @throws \Exception |
||
| 260 | * @throws \RuntimeException |
||
| 261 | * @throws \InvalidArgumentException |
||
| 262 | */ |
||
| 263 | public function authId(): string |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param LoopInterface|null $loop |
||
| 270 | * @return LoopInterface |
||
| 271 | */ |
||
| 272 | public function loop(LoopInterface $loop = null): LoopInterface |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string|null $token |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public function token(string $token = null): string |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param LoggerInterface|null $logger |
||
| 296 | * @return LoggerInterface |
||
| 297 | */ |
||
| 298 | public function logger(LoggerInterface $logger = null): LoggerInterface |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $name |
||
| 309 | * @throws \LogicException |
||
| 310 | */ |
||
| 311 | public function __unset(string $name) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $name |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function __isset(string $name): bool |
||
| 343 | } |
||
| 344 |