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); |
||
| 38 | class Client |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | const VERSION = '4.0.0'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $token; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var LoggerInterface |
||
| 52 | */ |
||
| 53 | protected $logger; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var LoopInterface |
||
| 57 | */ |
||
| 58 | protected $loop; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $storage = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Client constructor. |
||
| 67 | * @param string $token |
||
| 68 | * @param LoggerInterface $logger |
||
| 69 | */ |
||
| 70 | public function __construct(string $token, LoggerInterface $logger = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function clear() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return SyncAdapterInterface|AdapterInterface |
||
| 91 | */ |
||
| 92 | public function viaHttp(): SyncAdapterInterface |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return StreamAdapterInterface|AdapterInterface |
||
| 99 | */ |
||
| 100 | public function viaStream(): StreamAdapterInterface |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function connect() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $hookId |
||
| 115 | * @return WebHook |
||
| 116 | * @throws \InvalidArgumentException |
||
| 117 | */ |
||
| 118 | public function notify(string $hookId): WebHook |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $resource |
||
| 125 | * @return Groups|Messages|Rooms|Users|null|LoggerInterface|LoopInterface|string |
||
| 126 | */ |
||
| 127 | public function __get(string $resource) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $name |
||
| 162 | * @param $value |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function __set(string $name, $value) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return array |
||
| 188 | * @throws \RuntimeException |
||
| 189 | * @throws \InvalidArgumentException |
||
| 190 | */ |
||
| 191 | public function authUser(): array |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | * @throws \RuntimeException |
||
| 199 | * @throws \InvalidArgumentException |
||
| 200 | */ |
||
| 201 | public function authId(): string |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param LoopInterface|null $loop |
||
| 208 | * @return LoopInterface |
||
| 209 | */ |
||
| 210 | public function loop(LoopInterface $loop = null): LoopInterface |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string|null $token |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function token(string $token = null): string |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param LoggerInterface|null $logger |
||
| 234 | * @return LoggerInterface |
||
| 235 | */ |
||
| 236 | public function logger(LoggerInterface $logger = null): LoggerInterface |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $name |
||
| 247 | * @throws \LogicException |
||
| 248 | */ |
||
| 249 | public function __unset(string $name) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $name |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function __isset(string $name): bool |
||
| 281 | } |
||
| 282 |