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); |
||
40 | class Client implements Loggable |
||
41 | { |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | const VERSION = '4.0.0'; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $token; |
||
51 | |||
52 | /** |
||
53 | * @var LoggerInterface |
||
54 | */ |
||
55 | protected $logger; |
||
56 | |||
57 | /** |
||
58 | * @var LoopInterface |
||
59 | */ |
||
60 | protected $loop; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $storage = []; |
||
66 | |||
67 | /** |
||
68 | * Client constructor. |
||
69 | * @param string $token |
||
70 | * @param LoggerInterface $logger |
||
71 | */ |
||
72 | public function __construct(string $token, LoggerInterface $logger = null) |
||
81 | |||
82 | /** |
||
83 | * @return void |
||
84 | */ |
||
85 | public function clear() |
||
90 | |||
91 | /** |
||
92 | * @param string $message |
||
93 | * @param int $level |
||
94 | * @return Loggable|$this |
||
95 | */ |
||
96 | public function log(string $message, int $level = Logger::INFO): Loggable |
||
102 | |||
103 | /** |
||
104 | * @return SyncAdapterInterface|AdapterInterface |
||
105 | */ |
||
106 | public function viaHttp(): SyncAdapterInterface |
||
110 | |||
111 | /** |
||
112 | * @return StreamAdapterInterface|AdapterInterface |
||
113 | */ |
||
114 | public function viaStream(): StreamAdapterInterface |
||
118 | |||
119 | /** |
||
120 | * @return void |
||
121 | */ |
||
122 | public function connect() |
||
126 | |||
127 | /** |
||
128 | * @param string $hookId |
||
129 | * @return WebHook |
||
130 | * @throws \InvalidArgumentException |
||
131 | */ |
||
132 | public function notify(string $hookId): WebHook |
||
136 | |||
137 | /** |
||
138 | * @param string $resource |
||
139 | * @return Groups|Messages|Rooms|Users|null|LoggerInterface|LoopInterface|string |
||
140 | */ |
||
141 | public function __get(string $resource) |
||
173 | |||
174 | /** |
||
175 | * @param string $name |
||
176 | * @param $value |
||
177 | * @return void |
||
178 | */ |
||
179 | public function __set(string $name, $value) |
||
199 | |||
200 | /** |
||
201 | * @return array |
||
202 | * @throws \RuntimeException |
||
203 | * @throws \InvalidArgumentException |
||
204 | */ |
||
205 | public function auth(): array |
||
209 | |||
210 | /** |
||
211 | * @return string |
||
212 | * @throws \RuntimeException |
||
213 | * @throws \InvalidArgumentException |
||
214 | */ |
||
215 | public function authId(): string |
||
219 | |||
220 | /** |
||
221 | * @param LoopInterface|null $loop |
||
222 | * @return LoopInterface |
||
223 | */ |
||
224 | public function loop(LoopInterface $loop = null): LoopInterface |
||
232 | |||
233 | /** |
||
234 | * @param string|null $token |
||
235 | * @return string |
||
236 | */ |
||
237 | public function token(string $token = null): string |
||
245 | |||
246 | /** |
||
247 | * @param LoggerInterface|null $logger |
||
248 | * @return LoggerInterface |
||
249 | */ |
||
250 | public function logger(LoggerInterface $logger = null): LoggerInterface |
||
258 | |||
259 | /** |
||
260 | * @param string $name |
||
261 | * @throws \LogicException |
||
262 | */ |
||
263 | public function __unset(string $name) |
||
280 | |||
281 | /** |
||
282 | * @param string $name |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function __isset(string $name): bool |
||
295 | } |
||
296 |