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 | * Get the collected request message. |
||
| 351 | * |
||
| 352 | * @return Collection |
||
| 353 | */ |
||
| 354 | public function getCollectedMessage() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Handle request. |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | * |
||
| 364 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
| 365 | * @throws \EasyWeChat\Server\BadRequestException |
||
| 366 | */ |
||
| 367 | 7 | protected function handleRequest() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Handle message. |
||
| 381 | * |
||
| 382 | * @param array $message |
||
| 383 | * |
||
| 384 | * @return mixed |
||
| 385 | */ |
||
| 386 | 7 | protected function handleMessage($message) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Build reply XML. |
||
| 413 | * |
||
| 414 | * @param string $to |
||
| 415 | * @param string $from |
||
| 416 | * @param AbstractMessage $message |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | 4 | protected function buildReply($to, $from, $message) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Get signature. |
||
| 436 | * |
||
| 437 | * @param array $request |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 8 | protected function signature($request) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Parse message array from raw php input. |
||
| 450 | * |
||
| 451 | * @param string|resource $content |
||
| 452 | * |
||
| 453 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
| 454 | * @throws \EasyWeChat\Encryption\EncryptionException |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | 7 | protected function parseMessageFromRequest($content) |
|
| 459 | { |
||
| 460 | 7 | $content = strval($content); |
|
| 461 | |||
| 462 | 7 | $arrayable = json_decode($content, true); |
|
| 463 | 7 | if (json_last_error() === JSON_ERROR_NONE) { |
|
| 464 | 1 | return $arrayable; |
|
| 465 | } |
||
| 466 | |||
| 467 | 7 | if ($this->isSafeMode()) { |
|
| 468 | 1 | if (!$this->encryptor) { |
|
| 469 | throw new RuntimeException('Safe mode Encryptor is necessary, please use Guard::setEncryptor(Encryptor $encryptor) set the encryptor instance.'); |
||
| 470 | } |
||
| 471 | |||
| 472 | 1 | $message = $this->encryptor->decryptMsg( |
|
| 473 | 1 | $this->request->get('msg_signature'), |
|
| 474 | 1 | $this->request->get('nonce'), |
|
| 475 | 1 | $this->request->get('timestamp'), |
|
| 476 | $content |
||
| 477 | 1 | ); |
|
| 478 | 1 | } else { |
|
| 479 | 6 | $message = XML::parse($content); |
|
| 480 | } |
||
| 481 | |||
| 482 | 7 | return $message; |
|
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Check the request message safe mode. |
||
| 487 | * |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | 7 | 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: