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 |
||
7 | class Queue extends AbstractQueueTransport { |
||
8 | const ProducerThreadCount = 5; |
||
9 | const DefaultDBFileCount = 10; |
||
10 | const DefaultMessageFolderSubfolderCount = 10; |
||
11 | |||
12 | const StorageTemporary = 0; |
||
13 | const StoragePersistent = 1; |
||
14 | |||
15 | /** |
||
16 | * @var QueueTransport |
||
17 | */ |
||
18 | protected $_general_queue = null; |
||
19 | |||
20 | /** |
||
21 | * @var QueueTransport[] |
||
22 | */ |
||
23 | protected $_additional_queue = []; |
||
24 | |||
25 | /** |
||
26 | * @var GeneralQueueConstructionSettings|object $_settings |
||
27 | */ |
||
28 | protected $_settings = null; |
||
29 | |||
30 | /** |
||
31 | * SmartQueue constructor. |
||
32 | * |
||
33 | * @param GeneralQueueConstructionSettings|object $settings |
||
34 | * |
||
35 | * @throws QueueException |
||
36 | */ |
||
37 | function __construct($settings) { |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | static function generate_rnd_postfix() { |
||
76 | |||
77 | /** |
||
78 | * Формируем сообщение для очереди |
||
79 | * |
||
80 | * @param mixed $data |
||
81 | * @param string|null $name Название сообщения. Если null, то можно дублировать |
||
82 | * @param integer $sort |
||
83 | * |
||
84 | * @return iMessage|object |
||
85 | */ |
||
86 | static function build_message($data, $name = null, $sort = 5) { |
||
97 | |||
98 | /** |
||
99 | * @param iMessage|object $object |
||
100 | * |
||
101 | * @return iMessage|object |
||
102 | * @throws QueueException |
||
103 | */ |
||
104 | static function sanify_event_object($object) { |
||
122 | |||
123 | /** |
||
124 | * Cloning sub queues |
||
125 | */ |
||
126 | function __clone() { |
||
133 | |||
134 | /** |
||
135 | * implements Transport |
||
136 | */ |
||
137 | |||
138 | /** |
||
139 | * @param iMessage|object $message |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | static function get_real_key_for_message($message) { |
||
151 | |||
152 | function get_queue_name() { |
||
155 | |||
156 | static function is_support_sorted_events() { |
||
159 | |||
160 | function produce_message($data, $name = null, $sort = 5) { |
||
164 | |||
165 | function save() { |
||
171 | |||
172 | function set_exclusive_mode($mode) { |
||
178 | |||
179 | /** |
||
180 | * @var iMessage[]|object[] |
||
181 | */ |
||
182 | protected $_next_messages = []; |
||
183 | |||
184 | function consume_next_message($wait_time = -1) { |
||
227 | |||
228 | /** |
||
229 | * Обновляем сообщение и сразу же сохраняем всё |
||
230 | * |
||
231 | * Эта функция не рейзит ошибку, если сообщение не найдено |
||
232 | * |
||
233 | * @param iMessage|object $message |
||
234 | * @param string|null $key форсированно задаём ключ сообщения |
||
235 | * |
||
236 | * @return boolean |
||
237 | */ |
||
238 | function update_message($message, $key = null) { |
||
250 | |||
251 | function clear_consumed_keys() { |
||
257 | |||
258 | /** |
||
259 | * Удалить сообщения и сразу же записать это в БД |
||
260 | * |
||
261 | * @param iMessage[]|object[] $messages |
||
262 | * |
||
263 | * @return string[]|integer[] |
||
264 | */ |
||
265 | function delete_messages(array $messages) { |
||
273 | |||
274 | /** |
||
275 | * @param Queue $queue |
||
276 | * |
||
277 | * @return boolean |
||
278 | */ |
||
279 | function is_equal_to($queue) { |
||
297 | |||
298 | /** |
||
299 | * @param mixed $data |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | static function serialize($data) { |
||
306 | |||
307 | /** |
||
308 | * @param string $string |
||
309 | * @param boolean $is_valid |
||
310 | * |
||
311 | * @return mixed |
||
312 | */ |
||
313 | static function unserialize($string, &$is_valid) { |
||
316 | } |
||
317 | |||
318 | ?> |