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 |
||
| 17 | class Queue implements QueueInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Channel |
||
| 21 | */ |
||
| 22 | protected $channel; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var AMQPQueue |
||
| 26 | */ |
||
| 27 | protected $resource; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Options\QueueOptions |
||
| 31 | */ |
||
| 32 | protected $options; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var MessageMapper |
||
| 36 | */ |
||
| 37 | protected $messageMapper; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return Options\QueueOptions |
||
| 41 | */ |
||
| 42 | 38 | public function getOptions() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param Options\QueueOptions|\Traversable|array $options |
||
| 49 | * @return $this |
||
| 50 | * @throws BaseException\BadMethodCallException |
||
| 51 | * @throws BaseException\InvalidArgumentException |
||
| 52 | */ |
||
| 53 | 38 | public function setOptions($options) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return $this |
||
| 65 | */ |
||
| 66 | 38 | protected function configureQueue() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return AMQPQueue |
||
| 94 | */ |
||
| 95 | 39 | public function getResource() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param AMQPQueue $resource |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | 39 | public function setResource(AMQPQueue $resource) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Declare a new queue on the broker. |
||
| 112 | * |
||
| 113 | * @return $this |
||
| 114 | * @throws \AMQPChannelException |
||
| 115 | * @throws \AMQPConnectionException |
||
| 116 | */ |
||
| 117 | 7 | public function declareQueue() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Bind the given queue to a routing key on an exchange. |
||
| 126 | * |
||
| 127 | * @param string $exchangeName Name of the exchange to bind to. |
||
| 128 | * @param string $routingKey Pattern or routing key to bind with. |
||
| 129 | * @param bool $noWait No wait for a reply |
||
| 130 | * @param array $arguments Additional binding arguments. |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | * @throws \AMQPChannelException |
||
| 134 | * @throws \AMQPConnectionException |
||
| 135 | */ |
||
| 136 | 6 | public function bind($exchangeName, $routingKey = null, $noWait = false, array $arguments = []) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Remove a routing key binding on an exchange from the given queue. |
||
| 145 | * |
||
| 146 | * @param string $exchangeName The name of the exchange on which the |
||
| 147 | * queue is bound. |
||
| 148 | * @param string $routingKey The binding routing key used by the |
||
| 149 | * queue. |
||
| 150 | * @param array $arguments Additional binding arguments. |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | * @throws \AMQPChannelException |
||
| 154 | * @throws \AMQPConnectionException |
||
| 155 | */ |
||
| 156 | 1 | public function unbind($exchangeName, $routingKey = null, array $arguments = []) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Acknowledge the receipt of a message. |
||
| 165 | * |
||
| 166 | * @param string $deliveryTag The message delivery tag of which to |
||
| 167 | * acknowledge receipt. |
||
| 168 | * @param bool $multiple Acknowledge all previous |
||
| 169 | * unacked messages as well. |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | * @throws \AMQPChannelException |
||
| 173 | * @throws \AMQPConnectionException |
||
| 174 | */ |
||
| 175 | 3 | public function ack($deliveryTag, $multiple = false) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Mark a message as explicitly not acknowledged. |
||
| 188 | * |
||
| 189 | * Mark the message identified by delivery_tag as explicitly not |
||
| 190 | * acknowledged. This method can only be called on messages that have not |
||
| 191 | * yet been acknowledged. When called, the broker will immediately put the |
||
| 192 | * message back onto the queue, instead of waiting until the connection is |
||
| 193 | * closed. This method is only supported by the RabbitMQ broker. The |
||
| 194 | * behavior of calling this method while connected to any other broker is |
||
| 195 | * undefined. |
||
| 196 | * |
||
| 197 | * @param string $deliveryTag Delivery tag of last message to reject. |
||
| 198 | * @param bool $requeue Requeue the message(s). |
||
| 199 | * @param bool $multiple Mark as not acknowledge all previous |
||
| 200 | * unacked messages as well. |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | * @throws \AMQPChannelException |
||
| 204 | * @throws \AMQPConnectionException |
||
| 205 | */ |
||
| 206 | 4 | public function nack($deliveryTag, $requeue = false, $multiple = false) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Mark one message as explicitly not acknowledged. |
||
| 222 | * |
||
| 223 | * Mark the message identified by delivery_tag as explicitly not |
||
| 224 | * acknowledged. This method can only be called on messages that have not |
||
| 225 | * yet been acknowledged. |
||
| 226 | * |
||
| 227 | * @param string $deliveryTag Delivery tag of the message to reject. |
||
| 228 | * @param bool $requeue Requeue the message(s). |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | * @throws \AMQPChannelException |
||
| 232 | * @throws \AMQPConnectionException |
||
| 233 | */ |
||
| 234 | 3 | public function reject($deliveryTag, $requeue = false) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Purge the contents of a queue. |
||
| 247 | * |
||
| 248 | * @return $this |
||
| 249 | * @throws \AMQPChannelException |
||
| 250 | * @throws \AMQPConnectionException |
||
| 251 | */ |
||
| 252 | 1 | public function purge() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Cancel a queue that is already bound to an exchange and routing key. |
||
| 261 | * |
||
| 262 | * @param string $consumerTag The queue name to cancel, if the queue |
||
| 263 | * object is not already representative of |
||
| 264 | * a queue. |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | * @throws \AMQPChannelException |
||
| 268 | * @throws \AMQPConnectionException |
||
| 269 | */ |
||
| 270 | 1 | public function cancel($consumerTag = '') |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Delete a queue from the broker. |
||
| 282 | * |
||
| 283 | * This includes its entire contents of unread or unacknowledged messages. |
||
| 284 | * |
||
| 285 | * @param bool $ifUnused Optionally $ifUnused can be specified |
||
| 286 | * to indicate the queue should not be |
||
| 287 | * deleted until no clients are connected to |
||
| 288 | * it. |
||
| 289 | * @param bool $ifEmpty Optionally $ifUnused can be specified |
||
| 290 | * to indicate the queue should not be |
||
| 291 | * deleted until it's empty |
||
| 292 | * @param bool $noWait No wait for a reply |
||
| 293 | * |
||
| 294 | * @return $this |
||
| 295 | * @throws \AMQPChannelException |
||
| 296 | * @throws \AMQPConnectionException |
||
| 297 | */ |
||
| 298 | 8 | public function delete($ifUnused = false, $ifEmpty = false, $noWait = false) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Retrieve the next message from the queue. |
||
| 317 | * |
||
| 318 | * @param bool $autoAck |
||
| 319 | * @return null|Message |
||
| 320 | * @throws \AMQPChannelException |
||
| 321 | * @throws \AMQPConnectionException |
||
| 322 | */ |
||
| 323 | 5 | public function get($autoAck = false) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return MessageMapper |
||
| 335 | */ |
||
| 336 | 8 | public function getMessageMapper() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param MessageMapper $messageMapper |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | 9 | public function setMessageMapper(MessageMapper $messageMapper) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Consume messages from a queue (blocking function). |
||
| 356 | * |
||
| 357 | * @param callback|ConsumerInterface|null $callback A callback function to which the |
||
| 358 | * consumed message will be passed. |
||
| 359 | * @param bool $noLocal |
||
| 360 | * @param bool $autoAck |
||
| 361 | * @param bool $exclusive |
||
| 362 | * @param string $consumerTag A string describing this consumer. Used |
||
| 363 | * for canceling subscriptions with cancel(). |
||
| 364 | * @return $this |
||
| 365 | * @throws \AMQPChannelException |
||
| 366 | * @throws \AMQPConnectionException |
||
| 367 | */ |
||
| 368 | 9 | public function consume( |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get the Channel object in use |
||
| 399 | * |
||
| 400 | * @return Channel |
||
| 401 | */ |
||
| 402 | 1 | public function getChannel() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param Channel $channel |
||
| 409 | * @return $this |
||
| 410 | */ |
||
| 411 | 8 | public function setChannel(Channel $channel) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get the Connection object in use |
||
| 419 | * |
||
| 420 | * @return Connection |
||
| 421 | */ |
||
| 422 | 1 | public function getConnection() |
|
| 426 | } |
||
| 427 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.