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 | 36 | function __construct($settings) { |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 847 | 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 | 848 | static function build_message($data, $name = null, $sort = 5) { |
|
|
1 ignored issue
–
show
|
|||
| 85 | return (object) [ |
||
| 86 | 848 | 'name' => is_null($name) ? null : (string) $name, |
|
| 87 | 848 | 'data' => $data, |
|
| 88 | 848 | 'time_created' => microtime(true), |
|
| 89 | 848 | 'time_rnd_postfix' => is_null($name) ? static::generate_rnd_postfix() : null, |
|
| 90 | 848 | 'time_last_update' => microtime(true), |
|
| 91 | 848 | 'sort' => min(max($sort, 0), self::DefaultDBFileCount - 1), |
|
| 92 | 212 | 'is_read' => false, |
|
| 93 | 212 | ]; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param iMessage|object $object |
||
| 98 | * |
||
| 99 | * @return iMessage|object |
||
| 100 | * @throws QueueException |
||
| 101 | */ |
||
| 102 | 864 | static function sanify_event_object($object) { |
|
|
1 ignored issue
–
show
|
|||
| 103 | 864 | $ret = clone $object; |
|
| 104 | /** @noinspection PhpParamsInspection */ |
||
| 105 | 864 | if (!array_key_exists('name', $ret)) { |
|
| 106 | 4 | $ret->name = null; |
|
| 107 | 1 | } |
|
| 108 | /** @noinspection PhpParamsInspection */ |
||
| 109 | 864 | if (!array_key_exists('data', $ret)) { |
|
| 110 | 4 | throw new QueueException('Datum does not have field data', 12); |
|
| 111 | } |
||
| 112 | 864 | if (!isset($ret->sort)) { |
|
| 113 | 4 | $ret->sort = 5; |
|
| 114 | 1 | } else { |
|
| 115 | 860 | $ret->sort = min(max($ret->sort, 0), Queue::DefaultDBFileCount - 1); |
|
| 116 | } |
||
| 117 | |||
| 118 | 864 | return $ret; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Cloning sub queues |
||
| 123 | */ |
||
| 124 | 376 | function __clone() { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * implements Transport |
||
| 134 | */ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param iMessage|object $message |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 1088 | static function get_real_key_for_message($message) { |
|
|
1 ignored issue
–
show
|
|||
| 142 | 1088 | return !is_null($message->name) |
|
| 143 | 953 | ? $message->name |
|
| 144 | 1054 | : sprintf('_%s%s', |
|
| 145 | 1043 | number_format($message->time_created, 7, '.', ''), |
|
| 146 | 1077 | isset($message->time_rnd_postfix) ? '_'.$message->time_rnd_postfix : '' |
|
| 147 | 272 | ); |
|
| 148 | } |
||
| 149 | |||
| 150 | 8 | function get_queue_name() { |
|
| 153 | |||
| 154 | 4 | static function is_support_sorted_events() { |
|
| 157 | |||
| 158 | 20 | function produce_message($data, $name = null, $sort = 5) { |
|
|
1 ignored issue
–
show
|
|||
| 159 | 20 | $this->set_same_time_flag(1); |
|
| 160 | 20 | $this->_general_queue->produce_message($data, $name, $sort); |
|
| 162 | |||
| 163 | 300 | function save() { |
|
| 169 | |||
| 170 | function set_exclusive_mode($mode) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var iMessage[]|object[] |
||
| 179 | */ |
||
| 180 | protected $_next_messages = []; |
||
| 181 | |||
| 182 | 352 | 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 | */ |
||
| 236 | 160 | function update_message($message, $key = null) { |
|
| 248 | |||
| 249 | 4 | function clear_consumed_keys() { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Удалить сообщения и сразу же записать это в БД |
||
| 258 | * |
||
| 259 | * @param iMessage[]|object[] $messages |
||
| 260 | * |
||
| 261 | * @return string[]|integer[] |
||
| 262 | */ |
||
| 263 | 136 | function delete_messages(array $messages) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param Queue $queue |
||
| 274 | * |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | 4 | function is_equal_to($queue) { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param mixed $data |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | static function serialize($data) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $string |
||
| 307 | * @param boolean $is_valid |
||
| 308 | * |
||
| 309 | * @return mixed |
||
| 310 | */ |
||
| 311 | static function unserialize($string, &$is_valid) { |
||
| 314 | } |
||
| 315 | |||
| 316 | ?> |
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.