Complex classes like Guard 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 Guard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Guard |
||
22 | { |
||
23 | /** |
||
24 | * Empty string. |
||
25 | */ |
||
26 | const SUCCESS_EMPTY_RESPONSE = 'success'; |
||
27 | |||
28 | const TEXT_MSG = 2; |
||
29 | const IMAGE_MSG = 4; |
||
30 | const VOICE_MSG = 8; |
||
31 | const VIDEO_MSG = 16; |
||
32 | const SHORT_VIDEO_MSG = 32; |
||
33 | const LOCATION_MSG = 64; |
||
34 | const LINK_MSG = 128; |
||
35 | const DEVICE_EVENT_MSG = 256; |
||
36 | const DEVICE_TEXT_MSG = 512; |
||
37 | const EVENT_MSG = 1048576; |
||
38 | const ALL_MSG = 1049598; |
||
39 | |||
40 | /** |
||
41 | * @var Request |
||
42 | */ |
||
43 | protected $request; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $token; |
||
49 | |||
50 | /** |
||
51 | * @var Encryptor |
||
52 | */ |
||
53 | protected $encryptor; |
||
54 | |||
55 | /** |
||
56 | * @var string|callable |
||
57 | */ |
||
58 | protected $messageHandler; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $messageFilter; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $messageTypeMapping = [ |
||
69 | 'text' => 2, |
||
70 | 'image' => 4, |
||
71 | 'voice' => 8, |
||
72 | 'video' => 16, |
||
73 | 'shortvideo' => 32, |
||
74 | 'location' => 64, |
||
75 | 'link' => 128, |
||
76 | 'device_event' => 256, |
||
77 | 'device_text' => 512, |
||
78 | 'event' => 1048576, |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $debug = false; |
||
85 | |||
86 | /** |
||
87 | * Constructor. |
||
88 | * |
||
89 | * @param string $token |
||
90 | * @param Request $request |
||
91 | */ |
||
92 | public function __construct($token, Request $request = null) |
||
97 | |||
98 | /** |
||
99 | * Enable/Disable debug mode. |
||
100 | * |
||
101 | * @param bool $debug |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function debug($debug = true) |
||
111 | |||
112 | /** |
||
113 | * Handle and return response. |
||
114 | * |
||
115 | * @throws BadRequestException |
||
116 | * |
||
117 | * @return Response |
||
118 | */ |
||
119 | public function serve() |
||
151 | |||
152 | /** |
||
153 | * Validation request params. |
||
154 | * |
||
155 | * @param string $token |
||
156 | * |
||
157 | * @throws FaultException |
||
158 | */ |
||
159 | public function validate($token) |
||
171 | |||
172 | /** |
||
173 | * Add a event listener. |
||
174 | * |
||
175 | * @param callable $callback |
||
176 | * @param int $option |
||
177 | * |
||
178 | * @throws InvalidArgumentException |
||
179 | * |
||
180 | * @return Guard |
||
181 | */ |
||
182 | public function setMessageHandler($callback = null, $option = self::ALL_MSG) |
||
193 | |||
194 | /** |
||
195 | * Return the message listener. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getMessageHandler() |
||
203 | |||
204 | /** |
||
205 | * Request getter. |
||
206 | * |
||
207 | * @return Request |
||
208 | */ |
||
209 | public function getRequest() |
||
213 | |||
214 | /** |
||
215 | * Request setter. |
||
216 | * |
||
217 | * @param Request $request |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setRequest(Request $request) |
||
227 | |||
228 | /** |
||
229 | * Set Encryptor. |
||
230 | * |
||
231 | * @param Encryptor $encryptor |
||
232 | * |
||
233 | * @return Guard |
||
234 | */ |
||
235 | public function setEncryptor(Encryptor $encryptor) |
||
241 | |||
242 | /** |
||
243 | * Return the encryptor instance. |
||
244 | * |
||
245 | * @return Encryptor |
||
246 | */ |
||
247 | public function getEncryptor() |
||
251 | |||
252 | /** |
||
253 | * Build response. |
||
254 | * |
||
255 | * @param $to |
||
256 | * @param $from |
||
257 | * @param mixed $message |
||
258 | * |
||
259 | * @throws \EntWeChat\Core\Exceptions\InvalidArgumentException |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function buildResponse($to, $from, $message) |
||
295 | |||
296 | /** |
||
297 | * Whether response is message. |
||
298 | * |
||
299 | * @param mixed $message |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | protected function isMessage($message) |
||
317 | |||
318 | /** |
||
319 | * Get request message. |
||
320 | * |
||
321 | * @throws BadRequestException |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getMessage() |
||
335 | |||
336 | /** |
||
337 | * Handle request. |
||
338 | * |
||
339 | * @throws \EntWeChat\Core\Exceptions\RuntimeException |
||
340 | * @throws \EntWeChat\Server\BadRequestException |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | protected function handleRequest() |
||
355 | |||
356 | /** |
||
357 | * Handle message. |
||
358 | * |
||
359 | * @param array $message |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | protected function handleMessage(array $message) |
||
387 | |||
388 | /** |
||
389 | * Build reply XML. |
||
390 | * |
||
391 | * @param string $to |
||
392 | * @param string $from |
||
393 | * @param AbstractMessage $message |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | protected function buildReply($to, $from, $message) |
||
410 | |||
411 | /** |
||
412 | * Get signature. |
||
413 | * |
||
414 | * @param array $request |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | protected function signature($request) |
||
424 | |||
425 | /** |
||
426 | * Parse message array from raw php input. |
||
427 | * |
||
428 | * @param string|resource $content |
||
429 | * |
||
430 | * @throws \EntWeChat\Core\Exceptions\RuntimeException |
||
431 | * @throws \EntWeChat\Encryption\EncryptionException |
||
432 | * |
||
433 | * @return array |
||
434 | */ |
||
435 | protected function parseMessageFromRequest($content) |
||
461 | |||
462 | /** |
||
463 | * Check the request message safe mode. |
||
464 | * |
||
465 | * @return bool |
||
466 | */ |
||
467 | private function isSafeMode() |
||
471 | } |
||
472 |