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 | 6 | function __construct($settings) { |
|
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | static function generate_rnd_postfix() { |
||
74 | 3678 | ||
75 | /** |
||
76 | 3678 | * Формируем сообщение для очереди |
|
77 | 3678 | * |
|
78 | 3678 | * @param mixed $data |
|
79 | 3678 | * @param string|null $name Название сообщения. Если null, то можно дублировать |
|
80 | 3678 | * @param integer $sort |
|
81 | 1221 | * |
|
82 | 1221 | * @return iMessage|object |
|
83 | */ |
||
84 | static function build_message($data, $name = null, $sort = 5) { |
||
95 | 3 | ||
96 | 1 | /** |
|
97 | * @param iMessage|object $object |
||
98 | 3678 | * |
|
99 | 3 | * @return iMessage|object |
|
100 | * @throws QueueException |
||
101 | 3678 | */ |
|
102 | 3 | static function sanify_event_object($object) { |
|
120 | |||
121 | /** |
||
122 | * Cloning sub queues |
||
123 | */ |
||
124 | function __clone() { |
||
131 | 3672 | ||
132 | /** |
||
133 | * implements Transport |
||
134 | 6 | */ |
|
135 | 6 | ||
136 | /** |
||
137 | * @param iMessage|object $message |
||
138 | 3 | * |
|
139 | 3 | * @return string |
|
140 | */ |
||
141 | static function get_real_key_for_message($message) { |
||
149 | 1224 | ||
150 | 1224 | function get_queue_name() { |
|
153 | |||
154 | static function is_support_sorted_events() { |
||
157 | |||
158 | function produce_message($data, $name = null, $sort = 5) { |
||
162 | |||
163 | function save() { |
||
169 | 1122 | ||
170 | function set_exclusive_mode($mode) { |
||
176 | 1254 | ||
177 | 24 | /** |
|
178 | 8 | * @var iMessage[]|object[] |
|
179 | 1254 | */ |
|
180 | protected $_next_messages = []; |
||
181 | 1254 | ||
182 | 1254 | function consume_next_message($wait_time = -1) { |
|
225 | |||
226 | /** |
||
227 | * Обновляем сообщение и сразу же сохраняем всё |
||
228 | * |
||
229 | 40 | * Эта функция не рейзит ошибку, если сообщение не найдено |
|
230 | * |
||
231 | 120 | * @param iMessage|object $message |
|
232 | * @param string|null $key форсированно задаём ключ сообщения |
||
233 | * |
||
234 | 3 | * @return boolean |
|
235 | 3 | * @throws QueueException |
|
236 | 3 | */ |
|
237 | 3 | function update_message($message, $key = null) { |
|
249 | 1092 | ||
250 | 1092 | function clear_consumed_keys() { |
|
256 | |||
257 | /** |
||
258 | * Удалить сообщения и сразу же записать это в БД |
||
259 | * |
||
260 | * @param iMessage[]|object[] $messages |
||
261 | * |
||
262 | 3 | * @return string[]|integer[] |
|
263 | 3 | */ |
|
264 | 3 | function delete_messages(array $messages) { |
|
272 | 3 | ||
273 | 3 | /** |
|
274 | 2 | * @param Queue $queue |
|
275 | * |
||
276 | 1 | * @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.