Complex classes like ThreemaGateway_Handler_Action_Receiver 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 ThreemaGateway_Handler_Action_Receiver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ThreemaGateway_Handler_Action_Receiver extends ThreemaGateway_Handler_Action_Abstract |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var bool whether the model is already prepared |
||
| 18 | */ |
||
| 19 | protected $isPrepared = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool whether the permissions are already checked |
||
| 23 | */ |
||
| 24 | protected $isPermissionChecked = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool whether the result should be grouped by the message type |
||
| 28 | */ |
||
| 29 | protected $groupByMessageType = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Startup. |
||
| 33 | * |
||
| 34 | * @param bool $alreadyPrepared If the Message Model is already prepared you |
||
| 35 | * may set this to true. |
||
| 36 | * @param bool $skipPermissionCheck Set to true to skip the permission check. |
||
| 37 | * (not recommend) |
||
| 38 | */ |
||
| 39 | public function __construct($alreadyPrepared = false, $skipPermissionCheck = false) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Sets whether the result should be grouped by the message type. |
||
| 48 | * |
||
| 49 | * The option is ignored when you specify the message type in a function |
||
| 50 | * as grouping in this case would not make any sense. |
||
| 51 | * |
||
| 52 | * @param bool $groupByMessageType |
||
| 53 | * @return null|array |
||
| 54 | */ |
||
| 55 | public function groupByMessageType($groupByMessageType) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns the single last message received. |
||
| 62 | * |
||
| 63 | * @param string $threemaId filter by Threema ID (optional) |
||
| 64 | * @param string $messageType filter by message type (optional, use Model constants) |
||
| 65 | * @param string $keyword filter by this string, which represents |
||
| 66 | * the text in a text message (Wildcards: * and ?) |
||
| 67 | * @return null|array |
||
| 68 | */ |
||
| 69 | public function getLastMessage($threemaId = null, $messageType = null, $keyword = null) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns all messages with the specified criterias. |
||
| 97 | * |
||
| 98 | * @param string $threemaId filter by Threema ID (optional) |
||
| 99 | * @param string $messageType filter by message type (optional, use Model constants) |
||
| 100 | * @param string $timeSpan a relative time (parsable by strtotime) to limit the query (e.g. '-7 days') |
||
| 101 | * @param string $keyword filter by this string, which represents |
||
| 102 | * the text in a text message (Wildcards: * and ?) |
||
| 103 | * @return null|array |
||
| 104 | */ |
||
| 105 | public function getMessages($threemaId = null, $messageType = null, $timeSpan = null, $keyword = null) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the message data for a particular message ID. |
||
| 133 | * |
||
| 134 | * Note that when the database is corrupt and e.g. for a message some |
||
| 135 | * datasets are missing, thsi will return null. |
||
| 136 | * |
||
| 137 | * @param string $messageId |
||
| 138 | * @param string $messageType If you know the message type it is very much |
||
| 139 | * recommend to specify it here. |
||
| 140 | * @return null|array |
||
| 141 | */ |
||
| 142 | public function getMessageData($messageId, $messageType = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Checks whether a message is saved in the database. |
||
| 161 | * |
||
| 162 | * Note that this does not guarantee that other methods return any data as |
||
| 163 | * a message is also considered "received" when the actual data has |
||
| 164 | * already been deleted. |
||
| 165 | * All other methods return "null" in this case as they cannot return all of |
||
| 166 | * the requested data. This function however would return true. |
||
| 167 | * |
||
| 168 | * @param string $messageId |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function messageIsReceived($messageId) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the list of all files. |
||
| 195 | * |
||
| 196 | * Grouping this result ({@see groupByMessageType()}) is not supported. |
||
| 197 | * Note that the result is still the usual array of all other message |
||
| 198 | * queries here. |
||
| 199 | * |
||
| 200 | * @param string $threemaId filter by Threema ID (optional) |
||
| 201 | * @param string $mimeType Filter by mime type (optional). |
||
| 202 | * @param string $fileType The file type, e.g. thumbnail/file or image (optional). |
||
| 203 | * This is a Threema-internal type and may not |
||
| 204 | * be particular useful. |
||
| 205 | * @param bool $queryMetaData Set to false, to prevent querying for meta |
||
| 206 | * data, which might speed up the query. (default: true) |
||
| 207 | * @return null|array |
||
| 208 | */ |
||
| 209 | public function getFileList($mimeType = null, $fileType = null, $threemaId = null, $queryMetaData = true) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the current state of a particular message. |
||
| 300 | * |
||
| 301 | * Only the state of *send* messages can be queried. |
||
| 302 | * Note: In contrast to most other methods here, this already returns the |
||
| 303 | * message/delivery state as an integer. |
||
| 304 | * |
||
| 305 | * @param string $messageSentId The ID of message, which has been send to a user |
||
| 306 | * @return null|int |
||
| 307 | */ |
||
| 308 | public function getMessageState($messageSentId) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns the history of all state changes of a particular message. |
||
| 340 | * |
||
| 341 | * Only the state of *send* messages can be queried. |
||
| 342 | * The result is already ordered from the not so important state to the most |
||
| 343 | * important one. |
||
| 344 | * |
||
| 345 | * @param string $messageSentId The ID of message, which has been send to a user |
||
| 346 | * @param bool $getMetaData Set to false, to speed up the query by not |
||
| 347 | * asking for meta data (when the state was received etc). |
||
| 348 | * (default: false) |
||
| 349 | * @param int $limitQuery When set, only the last x states are returned. |
||
| 350 | * @return null|array |
||
| 351 | */ |
||
| 352 | public function getMessageStateHistory($messageSentId, $getMetaData = true, $limitQuery = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns an array with all type codes currently supported. |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function getTypesArray() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Remove a message from the database. |
||
| 387 | * |
||
| 388 | * Note that the message is usually not completly removed and the message ID |
||
| 389 | * will stay in the database. The exact behaviour depends on the ACP "Harden |
||
| 390 | * against replay attacks" setting. |
||
| 391 | * This prevents replay attacks as otherwise a message with the same message |
||
| 392 | * ID could be inserted again into the database and would therefore be |
||
| 393 | * considered a new message, which has just been received although it |
||
| 394 | * actually had been received two times. |
||
| 395 | * However the message ID alone does not reveal any data (as all data & |
||
| 396 | * meta data, even the message type, is deleted). |
||
| 397 | * |
||
| 398 | * @param string $messageId |
||
| 399 | */ |
||
| 400 | public function removeMessage($messageId) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Checks whether the user is allowed to receive messages and prepares Model |
||
| 412 | * if necessary. |
||
| 413 | * |
||
| 414 | * @throws XenForo_Exception |
||
| 415 | */ |
||
| 416 | protected function initiate() |
||
| 417 | { |
||
| 418 | // check permission |
||
| 419 | if (!$this->isPermissionChecked) { |
||
| 420 | if (!$this->permissions->hasPermission('receive')) { |
||
| 421 | throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
||
| 422 | } |
||
| 423 | |||
| 424 | $this->isPermissionChecked = true; |
||
| 425 | } |
||
| 426 | |||
| 427 | if (!$this->isPrepared) { |
||
| 428 | /** @var ThreemaGateway_Model_Messages $model */ |
||
| 429 | $model = XenForo_Model::create('ThreemaGateway_Model_Messages'); |
||
| 430 | $model->preQuery(); |
||
| 431 | |||
| 432 | $this->isPrepared = true; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Queries the meta data and the main data of the messages itself. |
||
| 438 | * |
||
| 439 | * @param ThreemaGateway_Model_Messages $model |
||
| 440 | * @param int $messageType The type of the message (optional) |
||
| 441 | */ |
||
| 442 | protected function execute($model, $messageType = null) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Replace usual wildcards (?, *) with the ones used by MySQL (%, _). |
||
| 462 | * |
||
| 463 | * @param string $string The string to replace |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | protected function replaceWildcards($string) |
||
| 480 | } |
||
| 481 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: