Complex classes like Queue 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 Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Queue extends AbstractQueueTransport { |
||
| 6 | const ProducerThreadCount = 5; |
||
| 7 | const DefaultDBFileCount = 10; |
||
| 8 | const DefaultMessageFolderSubfolderCount = 10; |
||
| 9 | |||
| 10 | const StorageTemporary = 0; |
||
| 11 | const StoragePersistent = 1; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var QueueTransport |
||
| 15 | */ |
||
| 16 | protected $_general_queue = null; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var QueueTransport[] |
||
| 20 | */ |
||
| 21 | protected $_additional_queue = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var GeneralQueueConstructionSettings|object $_settings |
||
| 25 | */ |
||
| 26 | protected $_settings = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * SmartQueue constructor. |
||
| 30 | * |
||
| 31 | * @param GeneralQueueConstructionSettings|object $settings |
||
| 32 | * |
||
| 33 | * @throws QueueException |
||
| 34 | */ |
||
| 35 | 27 | function __construct($settings) { |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 3701 | static function generate_rnd_postfix() { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Формируем сообщение для очереди |
||
| 77 | * |
||
| 78 | * @param mixed $data |
||
| 79 | * @param string|null $name Название сообщения. Если null, то можно дублировать |
||
| 80 | * @param integer $sort |
||
| 81 | * |
||
| 82 | * @return iMessage|object |
||
| 83 | */ |
||
| 84 | 3702 | static function build_message($data, $name = null, $sort = 5) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param iMessage|object $object |
||
| 98 | * |
||
| 99 | * @return iMessage|object |
||
| 100 | * @throws QueueException |
||
| 101 | */ |
||
| 102 | 3699 | static function sanify_event_object($object) { |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Cloning sub queues |
||
| 123 | */ |
||
| 124 | 1266 | function __clone() { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * implements Transport |
||
| 134 | */ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param iMessage|object $message |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 3822 | static function get_real_key_for_message($message) { |
|
| 149 | |||
| 150 | 6 | function get_queue_name() { |
|
| 153 | |||
| 154 | 3 | static function is_support_sorted_events() { |
|
| 157 | |||
| 158 | 15 | function produce_message($data, $name = null, $sort = 5) { |
|
| 162 | |||
| 163 | 1245 | function save() { |
|
| 169 | |||
| 170 | function set_exclusive_mode($mode) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var iMessage[]|object[] |
||
| 179 | */ |
||
| 180 | protected $_next_messages = []; |
||
| 181 | |||
| 182 | 1254 | function consume_next_message($wait_time = -1) { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Обновляем сообщение и сразу же сохраняем всё |
||
| 228 | * |
||
| 229 | * Эта функция не рейзит ошибку, если сообщение не найдено |
||
| 230 | * |
||
| 231 | * @param iMessage|object $message |
||
| 232 | * @param string|null $key форсированно задаём ключ сообщения |
||
| 233 | * |
||
| 234 | * @return boolean |
||
| 235 | * @throws QueueException |
||
| 236 | */ |
||
| 237 | 120 | function update_message($message, $key = null) { |
|
| 249 | |||
| 250 | 3 | function clear_consumed_keys() { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Удалить сообщения и сразу же записать это в БД |
||
| 259 | * |
||
| 260 | * @param iMessage[]|object[] $messages |
||
| 261 | * |
||
| 262 | * @return string[]|integer[] |
||
| 263 | */ |
||
| 264 | 1092 | function delete_messages(array $messages) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param Queue $queue |
||
| 275 | * |
||
| 276 | * @return boolean |
||
| 277 | */ |
||
| 278 | 3 | function is_equal_to($queue) { |
|
| 296 | } |
||
| 297 | |||
| 298 | ?> |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.