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 |
||
| 39 | class Guard |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * Empty string. |
||
| 43 | */ |
||
| 44 | const SUCCESS_EMPTY_RESPONSE = 'success'; |
||
| 45 | |||
| 46 | const TEXT_MSG = 2; |
||
| 47 | const IMAGE_MSG = 4; |
||
| 48 | const VOICE_MSG = 8; |
||
| 49 | const VIDEO_MSG = 16; |
||
| 50 | const SHORT_VIDEO_MSG = 32; |
||
| 51 | const LOCATION_MSG = 64; |
||
| 52 | const LINK_MSG = 128; |
||
| 53 | const EVENT_MSG = 1048576; |
||
| 54 | const ALL_MSG = 1048830; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Request |
||
| 58 | */ |
||
| 59 | protected $request; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $token; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Encryptor |
||
| 68 | */ |
||
| 69 | protected $encryptor; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string|callable |
||
| 73 | */ |
||
| 74 | protected $messageHandler; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | protected $messageFilter; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $messageTypeMapping = [ |
||
| 85 | 'text' => 2, |
||
| 86 | 'image' => 4, |
||
| 87 | 'voice' => 8, |
||
| 88 | 'video' => 16, |
||
| 89 | 'shortvideo' => 32, |
||
| 90 | 'location' => 64, |
||
| 91 | 'link' => 128, |
||
| 92 | 'event' => 1048576, |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $debug = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Constructor. |
||
| 102 | * |
||
| 103 | * @param string $token |
||
| 104 | * @param Request $request |
||
| 105 | */ |
||
| 106 | 7 | public function __construct($token, Request $request = null) |
|
| 107 | { |
||
| 108 | 7 | $this->token = $token; |
|
| 109 | 7 | $this->request = $request ?: Request::createFromGlobals(); |
|
| 110 | 7 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Enable/Disable debug mode. |
||
| 114 | * |
||
| 115 | * @param bool $debug |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function debug($debug = true) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Handle and return response. |
||
| 128 | * |
||
| 129 | * @return Response |
||
| 130 | * |
||
| 131 | * @throws BadRequestException |
||
| 132 | */ |
||
| 133 | 6 | public function serve() |
|
| 134 | { |
||
| 135 | 6 | Log::debug('Request received:', [ |
|
| 136 | 6 | 'Method' => $this->request->getMethod(), |
|
| 137 | 6 | 'URI' => $this->request->getRequestUri(), |
|
| 138 | 6 | 'Query' => $this->request->getQueryString(), |
|
| 139 | 6 | 'Protocal' => $this->request->server->get('SERVER_PROTOCOL'), |
|
| 140 | 6 | 'Content' => $this->request->getContent(), |
|
| 141 | 6 | ]); |
|
| 142 | |||
| 143 | 6 | $this->validate($this->token); |
|
| 144 | |||
| 145 | 6 | if ($str = $this->request->get('echostr')) { |
|
| 146 | 1 | Log::debug("Output 'echostr' is '$str'."); |
|
| 147 | |||
| 148 | 1 | return new Response($str); |
|
| 149 | } |
||
| 150 | |||
| 151 | 6 | $result = $this->handleRequest(); |
|
| 152 | |||
| 153 | 6 | $response = $this->buildResponse($result['to'], $result['from'], $result['response']); |
|
| 154 | |||
| 155 | 6 | Log::debug('Server response created:', compact('response')); |
|
| 156 | |||
| 157 | 6 | return new Response($response); |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Validation request params. |
||
| 162 | * |
||
| 163 | * @param string $token |
||
| 164 | * |
||
| 165 | * @throws FaultException |
||
| 166 | */ |
||
| 167 | 6 | public function validate($token) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Add a event listener. |
||
| 182 | * |
||
| 183 | * @param callable $callback |
||
| 184 | * @param int $option |
||
| 185 | * |
||
| 186 | * @return Guard |
||
| 187 | * |
||
| 188 | * @throws InvalidArgumentException |
||
| 189 | */ |
||
| 190 | 6 | public function setMessageHandler($callback = null, $option = self::ALL_MSG) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Return the message listener. |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 1 | public function getMessageHandler() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Request getter. |
||
| 214 | * |
||
| 215 | * @return Request |
||
| 216 | */ |
||
| 217 | public function getRequest() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Request setter. |
||
| 224 | * |
||
| 225 | * @param Request $request |
||
| 226 | * |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | public function setRequest(Request $request) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set Encryptor. |
||
| 238 | * |
||
| 239 | * @param Encryptor $encryptor |
||
| 240 | * |
||
| 241 | * @return Guard |
||
| 242 | */ |
||
| 243 | 1 | public function setEncryptor(Encryptor $encryptor) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Return the encryptor instance. |
||
| 252 | * |
||
| 253 | * @return Encryptor |
||
| 254 | */ |
||
| 255 | public function getEncryptor() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Build response. |
||
| 262 | * |
||
| 263 | * @param $to |
||
| 264 | * @param $from |
||
| 265 | * @param mixed $message |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | * |
||
| 269 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
| 270 | */ |
||
| 271 | 6 | protected function buildResponse($to, $from, $message) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Whether response is message. |
||
| 306 | * |
||
| 307 | * @param mixed $message |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 4 | protected function isMessage($message) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get request message. |
||
| 328 | * |
||
| 329 | * @return object |
||
| 330 | */ |
||
| 331 | 6 | public function getMessage() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Handle request. |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | * |
||
| 347 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
| 348 | * @throws \EasyWeChat\Server\BadRequestException |
||
| 349 | */ |
||
| 350 | 6 | protected function handleRequest() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Handle message. |
||
| 364 | * |
||
| 365 | * @param array $message |
||
| 366 | * |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | 6 | protected function handleMessage($message) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Build reply XML. |
||
| 396 | * |
||
| 397 | * @param string $to |
||
| 398 | * @param string $from |
||
| 399 | * @param AbstractMessage $message |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | 4 | protected function buildReply($to, $from, $message) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get signature. |
||
| 419 | * |
||
| 420 | * @param array $request |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | 6 | protected function signature($request) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Parse message array from raw php input. |
||
| 433 | * |
||
| 434 | * @param string|resource $content |
||
| 435 | * |
||
| 436 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
| 437 | * @throws \EasyWeChat\Encryption\EncryptionException |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | 6 | protected function parseMessageFromRequest($content) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Check the request message safe mode. |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | 6 | private function isSafeMode() |
|
| 472 | } |
||
| 473 |
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: