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 |
||
40 | class Guard |
||
41 | { |
||
42 | /** |
||
43 | * Empty string. |
||
44 | */ |
||
45 | const SUCCESS_EMPTY_RESPONSE = 'success'; |
||
46 | |||
47 | const TEXT_MSG = 2; |
||
48 | const IMAGE_MSG = 4; |
||
49 | const VOICE_MSG = 8; |
||
50 | const VIDEO_MSG = 16; |
||
51 | const SHORT_VIDEO_MSG = 32; |
||
52 | const LOCATION_MSG = 64; |
||
53 | const LINK_MSG = 128; |
||
54 | const DEVICE_EVENT_MSG = 256; |
||
55 | const DEVICE_TEXT_MSG = 512; |
||
56 | const EVENT_MSG = 1048576; |
||
57 | const ALL_MSG = 1049598; |
||
58 | |||
59 | /** |
||
60 | * @var Request |
||
61 | */ |
||
62 | protected $request; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $token; |
||
68 | |||
69 | /** |
||
70 | * @var Encryptor |
||
71 | */ |
||
72 | protected $encryptor; |
||
73 | |||
74 | /** |
||
75 | * @var string|callable |
||
76 | */ |
||
77 | protected $messageHandler; |
||
78 | |||
79 | /** |
||
80 | * @var int |
||
81 | */ |
||
82 | protected $messageFilter; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $messageTypeMapping = [ |
||
88 | 'text' => 2, |
||
89 | 'image' => 4, |
||
90 | 'voice' => 8, |
||
91 | 'video' => 16, |
||
92 | 'shortvideo' => 32, |
||
93 | 'location' => 64, |
||
94 | 'link' => 128, |
||
95 | 'device_event' => 256, |
||
96 | 'device_text' => 512, |
||
97 | 'event' => 1048576, |
||
98 | ]; |
||
99 | |||
100 | /** |
||
101 | * @var bool |
||
102 | */ |
||
103 | protected $debug = false; |
||
104 | |||
105 | /** |
||
106 | * Constructor. |
||
107 | * |
||
108 | * @param string $token |
||
109 | * @param Request $request |
||
110 | */ |
||
111 | 12 | public function __construct($token, Request $request = null) |
|
116 | |||
117 | /** |
||
118 | * Enable/Disable debug mode. |
||
119 | * |
||
120 | * @param bool $debug |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | 1 | public function debug($debug = true) |
|
125 | { |
||
126 | 1 | $this->debug = $debug; |
|
127 | |||
128 | 1 | return $this; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Handle and return response. |
||
133 | * |
||
134 | * @return Response |
||
135 | * |
||
136 | * @throws BadRequestException |
||
137 | */ |
||
138 | 8 | public function serve() |
|
164 | |||
165 | /** |
||
166 | * Validation request params. |
||
167 | * |
||
168 | * @param string $token |
||
169 | * |
||
170 | * @throws FaultException |
||
171 | */ |
||
172 | 8 | public function validate($token) |
|
184 | |||
185 | /** |
||
186 | * Add a event listener. |
||
187 | * |
||
188 | * @param callable $callback |
||
189 | * @param int $option |
||
190 | * |
||
191 | * @return Guard |
||
192 | * |
||
193 | * @throws InvalidArgumentException |
||
194 | */ |
||
195 | 7 | public function setMessageHandler($callback = null, $option = self::ALL_MSG) |
|
206 | |||
207 | /** |
||
208 | * Return the message listener. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | 2 | public function getMessageHandler() |
|
216 | |||
217 | /** |
||
218 | * Request getter. |
||
219 | * |
||
220 | * @return Request |
||
221 | */ |
||
222 | 1 | public function getRequest() |
|
223 | { |
||
224 | 1 | return $this->request; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Request setter. |
||
229 | * |
||
230 | * @param Request $request |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setRequest(Request $request) |
||
240 | |||
241 | /** |
||
242 | * Set Encryptor. |
||
243 | * |
||
244 | * @param Encryptor $encryptor |
||
245 | * |
||
246 | * @return Guard |
||
247 | */ |
||
248 | 2 | public function setEncryptor(Encryptor $encryptor) |
|
254 | |||
255 | /** |
||
256 | * Return the encryptor instance. |
||
257 | * |
||
258 | * @return Encryptor |
||
259 | */ |
||
260 | public function getEncryptor() |
||
264 | |||
265 | /** |
||
266 | * Build response. |
||
267 | * |
||
268 | * @param $to |
||
269 | * @param $from |
||
270 | * @param mixed $message |
||
271 | * |
||
272 | * @return string |
||
273 | * |
||
274 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
275 | */ |
||
276 | 7 | protected function buildResponse($to, $from, $message) |
|
308 | |||
309 | /** |
||
310 | * Whether response is message. |
||
311 | * |
||
312 | * @param mixed $message |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | 4 | protected function isMessage($message) |
|
330 | |||
331 | /** |
||
332 | * Get request message. |
||
333 | * |
||
334 | * @return array |
||
335 | * |
||
336 | * @throws BadRequestException |
||
337 | */ |
||
338 | 7 | public function getMessage() |
|
348 | |||
349 | /** |
||
350 | * Handle request. |
||
351 | * |
||
352 | * @return array |
||
353 | * |
||
354 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
355 | * @throws \EasyWeChat\Server\BadRequestException |
||
356 | */ |
||
357 | 7 | protected function handleRequest() |
|
368 | |||
369 | /** |
||
370 | * Handle message. |
||
371 | * |
||
372 | * @param array $message |
||
373 | * |
||
374 | * @return mixed |
||
375 | */ |
||
376 | 7 | protected function handleMessage($message) |
|
400 | |||
401 | /** |
||
402 | * Build reply XML. |
||
403 | * |
||
404 | * @param string $to |
||
405 | * @param string $from |
||
406 | * @param AbstractMessage $message |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | 4 | protected function buildReply($to, $from, $message) |
|
423 | |||
424 | /** |
||
425 | * Get signature. |
||
426 | * |
||
427 | * @param array $request |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | 8 | protected function signature($request) |
|
437 | |||
438 | /** |
||
439 | * Parse message array from raw php input. |
||
440 | * |
||
441 | * @param string|resource $content |
||
442 | * |
||
443 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
444 | * @throws \EasyWeChat\Encryption\EncryptionException |
||
445 | * |
||
446 | * @return array |
||
447 | */ |
||
448 | 7 | protected function parseMessageFromRequest($content) |
|
474 | |||
475 | /** |
||
476 | * Check the request message safe mode. |
||
477 | * |
||
478 | * @return bool |
||
479 | */ |
||
480 | 7 | private function isSafeMode() |
|
484 | } |
||
485 |
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: