Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ThreemaGateway_DataWriter_Messages 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_DataWriter_Messages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ThreemaGateway_DataWriter_Messages extends XenForo_DataWriter |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string extra data - files |
||
| 15 | */ |
||
| 16 | const DATA_FILES = 'files'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string extra data - acknowledged message IDs |
||
| 20 | */ |
||
| 21 | const DATA_ACKED_MSG_IDS = 'ack_message_id'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Gets the fields that are defined for the table. See parent for explanation. |
||
| 25 | * |
||
| 26 | * @see XenForo_DataWriter::_getFields() |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | protected function _getFields() |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Generalises the receive date to reduce the amount of stored meta data. |
||
| 157 | * |
||
| 158 | * Generally you may also want to call this if the data you are inserting |
||
| 159 | * is only placeholder data (aka the message ID + receive date). |
||
| 160 | * All existing data should already be set when calling this function. |
||
| 161 | */ |
||
| 162 | public function roundReceiveDate() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Normalizes the file path returned by the PHP SDK to a common format. |
||
| 169 | * |
||
| 170 | * Currently this removes the directory structure, so that only the file |
||
| 171 | * name is saved. |
||
| 172 | * |
||
| 173 | * @param string $filepath |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function normalizeFilePath($filepath) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the actual existing data out of data that was passed in. See parent for explanation. |
||
| 183 | * |
||
| 184 | * The implementation is incomplete as it only builds an array with message |
||
| 185 | * ids and no real data. This is however done on purpose as this function is |
||
| 186 | * currently only used for deleting data. Updates can never happen in any |
||
| 187 | * message table. |
||
| 188 | * |
||
| 189 | * @param mixed $data |
||
| 190 | * @see XenForo_DataWriter::_getExistingData() |
||
| 191 | * @return array |
||
|
|
|||
| 192 | */ |
||
| 193 | protected function _getExistingData($data) |
||
| 194 | { |
||
| 195 | /** @var string $messageId */ |
||
| 196 | if (!$messageId = $this->_getExistingPrimaryKey($data, 'message_id')) { |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** @var array $existing Array of existing data. (filled below) */ |
||
| 201 | $existing = []; |
||
| 202 | |||
| 203 | $this->_getMessagesModel()->setMessageId($messageId); |
||
| 204 | /** @var array $metaData */ |
||
| 205 | $metaData = $this->_getMessagesModel()->getMessageMetaData(); |
||
| 206 | |||
| 207 | // add main table to array (this is the only complete table using) |
||
| 208 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_MESSAGES] = reset($metaData); |
||
| 209 | |||
| 210 | /** @var int $messageType Extracted message type from metadata. */ |
||
| 211 | $messageType = reset($metaData)['message_type_code']; |
||
| 212 | |||
| 213 | // conditionally add data from other tables depending on message |
||
| 214 | // type |
||
| 215 | switch ($messageType) { |
||
| 216 | View Code Duplication | case ThreemaGateway_Model_Messages::TYPE_DELIVERY_MESSAGE: |
|
| 217 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_MESSAGES . '_delivery_receipt'] = [ |
||
| 218 | 'message_id' => $messageId |
||
| 219 | ]; |
||
| 220 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_DELIVERY_RECEIPT] = [ |
||
| 221 | 'message_id' => $messageId |
||
| 222 | ]; |
||
| 223 | break; |
||
| 224 | |||
| 225 | View Code Duplication | case ThreemaGateway_Model_Messages::TYPE_FILE_MESSAGE: |
|
| 226 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_MESSAGES . '_file'] = [ |
||
| 227 | 'message_id' => $messageId |
||
| 228 | ]; |
||
| 229 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_FILES] = [ |
||
| 230 | 'message_id' => $messageId |
||
| 231 | ]; |
||
| 232 | break; |
||
| 233 | |||
| 234 | View Code Duplication | case ThreemaGateway_Model_Messages::TYPE_IMAGE_MESSAGE: |
|
| 235 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_MESSAGES . '_image'] = [ |
||
| 236 | 'message_id' => $messageId |
||
| 237 | ]; |
||
| 238 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_FILES] = [ |
||
| 239 | 'message_id' => $messageId |
||
| 240 | ]; |
||
| 241 | break; |
||
| 242 | |||
| 243 | View Code Duplication | case ThreemaGateway_Model_Messages::TYPE_TEXT_MESSAGE: |
|
| 244 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_MESSAGES . '_text'] = [ |
||
| 245 | 'message_id' => $messageId |
||
| 246 | ]; |
||
| 247 | $existing[ThreemaGateway_Model_Messages::DB_TABLE_FILES] = [ |
||
| 248 | 'message_id' => $messageId |
||
| 249 | ]; |
||
| 250 | break; |
||
| 251 | |||
| 252 | default: |
||
| 253 | throw new XenForo_Exception(new XenForo_Phrase('threemagw_unknown_message_type')); |
||
| 254 | } |
||
| 255 | |||
| 256 | return $existing; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets SQL condition to update the existing record. |
||
| 261 | * |
||
| 262 | * @param string $tableName |
||
| 263 | * @see XenForo_DataWriter::_getUpdateCondition() |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | protected function _getUpdateCondition($tableName) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Pre-save: Removes tables, which should not be touched. |
||
| 273 | * |
||
| 274 | * The function searches for invalid tables and removes them from the query. |
||
| 275 | * This is necessary as a message can only be an instance of one message |
||
| 276 | * type and as by default all tables (& therefore types) are included in the |
||
| 277 | * fields, we have to confitionally remove them. |
||
| 278 | * Additionally it ses the correct character encoding. |
||
| 279 | * |
||
| 280 | * @see XenForo_DataWriter::_preSave() |
||
| 281 | */ |
||
| 282 | protected function _preSave() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Pre-delete: Remove main table & unused tables from selected existing data. |
||
| 352 | * |
||
| 353 | * The reason for the deletion is, that the message ID should stay in the |
||
| 354 | * database and must not be deleted. |
||
| 355 | * |
||
| 356 | * @see XenForo_DataWriter::_preDelete() |
||
| 357 | */ |
||
| 358 | protected function _preDelete() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Post-save: Add additional data supplied as extra data. |
||
| 381 | * |
||
| 382 | * This function writes the missing datasets into the files and the |
||
| 383 | * acknowleged messages table. |
||
| 384 | * |
||
| 385 | * @see XenForo_DataWriter::_postSave() |
||
| 386 | */ |
||
| 387 | protected function _postSave() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Post-delete: Remove all data from main table, except of message ID & |
||
| 422 | * the receive date. |
||
| 423 | * |
||
| 424 | * The reason for the deletion is, that the message ID should stay in the |
||
| 425 | * database and must not be deleted as this prevents replay attacks |
||
| 426 | * ({@see ThreemaGateway_Handler_Action_Receiver->removeMessage()}). |
||
| 427 | * |
||
| 428 | * @see XenForo_DataWriter::_postDelete() |
||
| 429 | */ |
||
| 430 | protected function _postDelete() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Gets the receive date in a rounded way. |
||
| 464 | * |
||
| 465 | * @return int |
||
| 466 | */ |
||
| 467 | protected function getRoundedReceiveDate() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the messages model. |
||
| 486 | * |
||
| 487 | * @return ThreemaGateway_Model_Messages |
||
| 488 | */ |
||
| 489 | protected function _getMessagesModel() |
||
| 493 | } |
||
| 494 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.