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 |
||
41 | class Guard |
||
42 | { |
||
43 | /** |
||
44 | * Empty string. |
||
45 | */ |
||
46 | const SUCCESS_EMPTY_RESPONSE = 'success'; |
||
47 | |||
48 | const TEXT_MSG = 2; |
||
49 | const IMAGE_MSG = 4; |
||
50 | const VOICE_MSG = 8; |
||
51 | const VIDEO_MSG = 16; |
||
52 | const SHORT_VIDEO_MSG = 32; |
||
53 | const LOCATION_MSG = 64; |
||
54 | const LINK_MSG = 128; |
||
55 | const DEVICE_EVENT_MSG = 256; |
||
56 | const DEVICE_TEXT_MSG = 512; |
||
57 | const EVENT_MSG = 1048576; |
||
58 | const ALL_MSG = 1049598; |
||
59 | |||
60 | /** |
||
61 | * @var Request |
||
62 | */ |
||
63 | protected $request; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $token; |
||
69 | |||
70 | /** |
||
71 | * @var Encryptor |
||
72 | */ |
||
73 | protected $encryptor; |
||
74 | |||
75 | /** |
||
76 | * @var string|callable |
||
77 | */ |
||
78 | protected $messageHandler; |
||
79 | |||
80 | /** |
||
81 | * @var int |
||
82 | */ |
||
83 | protected $messageFilter; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $messageTypeMapping = [ |
||
89 | 'text' => 2, |
||
90 | 'image' => 4, |
||
91 | 'voice' => 8, |
||
92 | 'video' => 16, |
||
93 | 'shortvideo' => 32, |
||
94 | 'location' => 64, |
||
95 | 'link' => 128, |
||
96 | 'device_event' => 256, |
||
97 | 'device_text' => 512, |
||
98 | 'event' => 1048576, |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * @var bool |
||
103 | */ |
||
104 | protected $debug = false; |
||
105 | |||
106 | /** |
||
107 | * Constructor. |
||
108 | * |
||
109 | * @param string $token |
||
110 | * @param Request $request |
||
111 | */ |
||
112 | 7 | public function __construct($token, Request $request = null) |
|
117 | |||
118 | /** |
||
119 | * Enable/Disable debug mode. |
||
120 | * |
||
121 | * @param bool $debug |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function debug($debug = true) |
||
131 | |||
132 | /** |
||
133 | * Handle and return response. |
||
134 | * |
||
135 | * @return Response |
||
136 | * |
||
137 | * @throws BadRequestException |
||
138 | */ |
||
139 | 6 | public function serve() |
|
165 | |||
166 | /** |
||
167 | * Validation request params. |
||
168 | * |
||
169 | * @param string $token |
||
170 | * |
||
171 | * @throws FaultException |
||
172 | */ |
||
173 | 6 | public function validate($token) |
|
185 | |||
186 | /** |
||
187 | * Add a event listener. |
||
188 | * |
||
189 | * @param callable $callback |
||
190 | * @param int $option |
||
191 | * |
||
192 | * @return Guard |
||
193 | * |
||
194 | * @throws InvalidArgumentException |
||
195 | */ |
||
196 | 6 | public function setMessageHandler($callback = null, $option = self::ALL_MSG) |
|
207 | |||
208 | /** |
||
209 | * Return the message listener. |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 1 | public function getMessageHandler() |
|
217 | |||
218 | /** |
||
219 | * Request getter. |
||
220 | * |
||
221 | * @return Request |
||
222 | */ |
||
223 | public function getRequest() |
||
227 | |||
228 | /** |
||
229 | * Request setter. |
||
230 | * |
||
231 | * @param Request $request |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setRequest(Request $request) |
||
241 | |||
242 | /** |
||
243 | * Set Encryptor. |
||
244 | * |
||
245 | * @param Encryptor $encryptor |
||
246 | * |
||
247 | * @return Guard |
||
248 | */ |
||
249 | 1 | public function setEncryptor(Encryptor $encryptor) |
|
255 | |||
256 | /** |
||
257 | * Return the encryptor instance. |
||
258 | * |
||
259 | * @return Encryptor |
||
260 | */ |
||
261 | public function getEncryptor() |
||
265 | |||
266 | /** |
||
267 | * Build response. |
||
268 | * |
||
269 | * @param $to |
||
270 | * @param $from |
||
271 | * @param mixed $message |
||
272 | * |
||
273 | * @return string |
||
274 | * |
||
275 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
276 | */ |
||
277 | 6 | protected function buildResponse($to, $from, $message) |
|
309 | |||
310 | /** |
||
311 | * Whether response is message. |
||
312 | * |
||
313 | * @param mixed $message |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | 4 | protected function isMessage($message) |
|
331 | |||
332 | /** |
||
333 | * Get request message. |
||
334 | * |
||
335 | * @return array |
||
336 | * |
||
337 | * @throws BadRequestException |
||
338 | */ |
||
339 | 6 | public function getMessage() |
|
349 | |||
350 | /** |
||
351 | * Get the collected request message. |
||
352 | * |
||
353 | * @return Collection |
||
354 | */ |
||
355 | public function getCollectedMessage() |
||
359 | |||
360 | /** |
||
361 | * Handle request. |
||
362 | * |
||
363 | * @return array |
||
364 | * |
||
365 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
366 | * @throws \EasyWeChat\Server\BadRequestException |
||
367 | */ |
||
368 | 6 | protected function handleRequest() |
|
379 | |||
380 | /** |
||
381 | * Handle message. |
||
382 | * |
||
383 | * @param array $message |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | 6 | protected function handleMessage($message) |
|
411 | |||
412 | /** |
||
413 | * Build reply XML. |
||
414 | * |
||
415 | * @param string $to |
||
416 | * @param string $from |
||
417 | * @param AbstractMessage $message |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | 4 | protected function buildReply($to, $from, $message) |
|
434 | |||
435 | /** |
||
436 | * Get signature. |
||
437 | * |
||
438 | * @param array $request |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | 6 | protected function signature($request) |
|
448 | |||
449 | /** |
||
450 | * Parse message array from raw php input. |
||
451 | * |
||
452 | * @param string|resource $content |
||
453 | * |
||
454 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
455 | * @throws \EasyWeChat\Encryption\EncryptionException |
||
456 | * |
||
457 | * @return array |
||
458 | */ |
||
459 | 6 | protected function parseMessageFromRequest($content) |
|
484 | |||
485 | /** |
||
486 | * Check the request message safe mode. |
||
487 | * |
||
488 | * @return bool |
||
489 | */ |
||
490 | 6 | private function isSafeMode() |
|
494 | } |
||
495 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: