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 |
||
40 | class Client |
||
41 | { |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | const VERSION = '4.0.7'; |
||
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 | * @var int |
||
69 | */ |
||
70 | private $retries = 100; |
||
71 | |||
72 | /** |
||
73 | * @var HttpAdapter|null |
||
74 | */ |
||
75 | private $http; |
||
76 | |||
77 | /** |
||
78 | * @var StreamAdapter|null |
||
79 | */ |
||
80 | private $streaming; |
||
81 | |||
82 | /** |
||
83 | * Client constructor. |
||
84 | * @param string $token |
||
85 | * @param LoggerInterface $logger |
||
86 | */ |
||
87 | public function __construct(string $token, LoggerInterface $logger = null) |
||
88 | { |
||
89 | $this->token = $token; |
||
90 | $this->loop = Factory::create(); |
||
91 | |||
92 | if (null === ($this->logger = $logger)) { |
||
93 | $this->logger = new NullLogger(); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $token |
||
99 | * @return Client |
||
100 | */ |
||
101 | public function updateToken(string $token): Client |
||
102 | { |
||
103 | $this->token = $token; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param int $count |
||
110 | * @return Client |
||
111 | */ |
||
112 | public function retries(int $count): Client |
||
113 | { |
||
114 | $this->retries = $count; |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return int |
||
121 | */ |
||
122 | public function getRetriesCount(): int |
||
123 | { |
||
124 | return $this->retries; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return void |
||
129 | */ |
||
130 | public function clear() |
||
131 | { |
||
132 | $this->loop->stop(); |
||
133 | $this->storage = []; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return SyncAdapterInterface|AdapterInterface |
||
138 | * @throws \InvalidArgumentException |
||
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 | * @param LoopInterface|null $loop |
||
244 | * @return LoopInterface |
||
245 | */ |
||
246 | public function loop(LoopInterface $loop = null): LoopInterface |
||
247 | { |
||
248 | if ($loop !== null) { |
||
249 | $this->loop = $loop; |
||
250 | } |
||
251 | |||
252 | return $this->loop; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param string|null $token |
||
257 | * @return string |
||
258 | */ |
||
259 | public function token(string $token = null): string |
||
260 | { |
||
261 | if ($token !== null) { |
||
262 | $this->token = $token; |
||
263 | } |
||
264 | |||
265 | return $this->token; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param LoggerInterface|null $logger |
||
270 | * @return LoggerInterface |
||
271 | */ |
||
272 | public function logger(LoggerInterface $logger = null): LoggerInterface |
||
273 | { |
||
274 | if ($logger !== null) { |
||
275 | $this->logger = $logger; |
||
276 | } |
||
277 | |||
278 | return $this->logger; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @return array |
||
283 | * @throws \Throwable |
||
284 | * @throws \GuzzleHttp\Exception\ClientException |
||
285 | * @throws \Exception |
||
286 | * @throws \RuntimeException |
||
287 | * @throws \InvalidArgumentException |
||
288 | */ |
||
289 | public function authUser(): array |
||
290 | { |
||
291 | return $this->users->current(); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | * @throws \Throwable |
||
297 | * @throws \GuzzleHttp\Exception\ClientException |
||
298 | * @throws \Exception |
||
299 | * @throws \RuntimeException |
||
300 | * @throws \InvalidArgumentException |
||
301 | */ |
||
302 | public function authId(): string |
||
303 | { |
||
304 | return $this->users->currentUserId(); |
||
305 | } |
||
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 |
||
334 | { |
||
335 | if (\in_array($name, ['users', 'groups', 'messages', 'rooms', 'loop', 'token'], true)) { |
||
336 | return true; |
||
337 | } |
||
338 | |||
345 | } |
||
346 |