Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueueRestProxy 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 QueueRestProxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class QueueRestProxy extends ServiceRestProxy implements IQueue |
||
63 | { |
||
64 | use ServiceRestTrait; |
||
65 | |||
66 | /** |
||
67 | * Lists all queues in the storage account. |
||
68 | * |
||
69 | * @param ListQueuesOptions $options The optional list queue options. |
||
70 | * |
||
71 | * @return ListQueuesResult |
||
72 | */ |
||
73 | public function listQueues(ListQueuesOptions $options = null) |
||
77 | |||
78 | /** |
||
79 | * Creates promise to list all queues in the storage account. |
||
80 | * |
||
81 | * @param ListQueuesOptions $options The optional list queue options. |
||
82 | * |
||
83 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
84 | */ |
||
85 | public function listQueuesAsync(ListQueuesOptions $options = null) |
||
132 | |||
133 | /** |
||
134 | * Clears all messages from the queue. |
||
135 | * |
||
136 | * If a queue contains a large number of messages, Clear Messages may time out |
||
137 | * before all messages have been deleted. In this case the Queue service will |
||
138 | * return status code 500 (Internal Server Error), with the additional error |
||
139 | * code OperationTimedOut. If the operation times out, the client should |
||
140 | * continue to retry Clear Messages until it succeeds, to ensure that all |
||
141 | * messages have been deleted. |
||
142 | * |
||
143 | * @param string $queueName The name of the queue. |
||
144 | * @param QueueServiceOptions $options The optional parameters. |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | public function clearMessages($queueName, QueueServiceOptions $options = null) |
||
152 | |||
153 | /** |
||
154 | * Creates promise to clear all messages from the queue. |
||
155 | * |
||
156 | * If a queue contains a large number of messages, Clear Messages may time out |
||
157 | * before all messages have been deleted. In this case the Queue service will |
||
158 | * return status code 500 (Internal Server Error), with the additional error |
||
159 | * code OperationTimedOut. If the operation times out, the client should |
||
160 | * continue to retry Clear Messages until it succeeds, to ensure that all |
||
161 | * messages have been deleted. |
||
162 | * |
||
163 | * @param string $queueName The name of the queue. |
||
164 | * @param QueueServiceOptions $options The optional parameters. |
||
165 | * |
||
166 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
167 | */ |
||
168 | public function clearMessagesAsync( |
||
199 | |||
200 | /** |
||
201 | * Adds a message to the queue and optionally sets a visibility timeout |
||
202 | * for the message. |
||
203 | * |
||
204 | * @param string $queueName The name of the queue. |
||
205 | * @param string $messageText The message contents. |
||
206 | * @param CreateMessageOptions $options The optional parameters. |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | public function createMessage( |
||
217 | |||
218 | /** |
||
219 | * Creates promise to add a message to the queue and optionally sets a |
||
220 | * visibility timeout for the message. |
||
221 | * |
||
222 | * @param string $queueName The name of the queue. |
||
223 | * @param string $messageText The message contents. |
||
224 | * @param CreateMessageOptions $options The optional parameters. |
||
225 | * |
||
226 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
227 | */ |
||
228 | public function createMessageAsync( |
||
285 | |||
286 | /** |
||
287 | * Creates a new queue under the storage account. |
||
288 | * |
||
289 | * @param string $queueName The queue name. |
||
290 | * @param Models\CreateQueueOptions $options The Optional parameters. |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | public function createQueue( |
||
300 | |||
301 | /** |
||
302 | * Creates promise to create a new queue under the storage account. |
||
303 | * |
||
304 | * @param string $queueName The queue name. |
||
305 | * @param Models\CreateQueueOptions $options The Optional parameters. |
||
306 | * |
||
307 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
308 | */ |
||
309 | public function createQueueAsync( |
||
342 | |||
343 | /** |
||
344 | * Deletes a specified message from the queue. |
||
345 | * |
||
346 | * @param string $queueName The name of the queue. |
||
347 | * @param string $messageId The id of the message. |
||
348 | * @param string $popReceipt The valid pop receipt value returned |
||
349 | * from an earlier call to the Get Messages or Update Message operation. |
||
350 | * @param QueueServiceOptions $options The optional parameters. |
||
351 | * |
||
352 | * @return void |
||
353 | */ |
||
354 | public function deleteMessage( |
||
367 | |||
368 | /** |
||
369 | * Creates promise to delete a specified message from the queue. |
||
370 | * |
||
371 | * @param string $queueName The name of the queue. |
||
372 | * @param string $messageId The id of the message. |
||
373 | * @param string $popReceipt The valid pop receipt value returned |
||
374 | * from an earlier call to the Get Messages or Update Message operation. |
||
375 | * @param QueueServiceOptions $options The optional parameters. |
||
376 | * |
||
377 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
378 | */ |
||
379 | public function deleteMessageAsync( |
||
422 | |||
423 | /** |
||
424 | * Deletes a queue. |
||
425 | * |
||
426 | * @param string $queueName The queue name. |
||
427 | * @param QueueServiceOptions $options The optional parameters. |
||
428 | * |
||
429 | * @return void |
||
430 | */ |
||
431 | public function deleteQueue($queueName, QueueServiceOptions $options = null) |
||
435 | |||
436 | /** |
||
437 | * Creates promise to delete a queue. |
||
438 | * |
||
439 | * @param string $queueName The queue name. |
||
440 | * @param QueueServiceOptions $options The optional parameters. |
||
441 | * |
||
442 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
443 | */ |
||
444 | View Code Duplication | public function deleteQueueAsync( |
|
474 | |||
475 | /** |
||
476 | * Returns queue properties, including user-defined metadata. |
||
477 | * |
||
478 | * @param string $queueName The queue name. |
||
479 | * @param QueueServiceOptions $options The optional parameters. |
||
480 | * |
||
481 | * @return Models\GetQueueMetadataResult |
||
482 | */ |
||
483 | public function getQueueMetadata($queueName, QueueServiceOptions $options = null) |
||
487 | |||
488 | /** |
||
489 | * Creates promise to return queue properties, including user-defined metadata. |
||
490 | * |
||
491 | * @param string $queueName The queue name. |
||
492 | * @param QueueServiceOptions $options The optional parameters. |
||
493 | * |
||
494 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
495 | */ |
||
496 | public function getQueueMetadataAsync( |
||
538 | |||
539 | /** |
||
540 | * Lists all messages in the queue. |
||
541 | * |
||
542 | * @param string $queueName The queue name. |
||
543 | * @param ListMessagesOptions $options The optional parameters. |
||
544 | * |
||
545 | * @return Models\ListMessagesResult |
||
546 | */ |
||
547 | public function listMessages($queueName, ListMessagesOptions $options = null) |
||
551 | |||
552 | /** |
||
553 | * Creates promise to list all messages in the queue. |
||
554 | * |
||
555 | * @param string $queueName The queue name. |
||
556 | * @param ListMessagesOptions $options The optional parameters. |
||
557 | * |
||
558 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
559 | */ |
||
560 | public function listMessagesAsync( |
||
609 | |||
610 | /** |
||
611 | * Retrieves a message from the front of the queue, without changing |
||
612 | * the message visibility. |
||
613 | * |
||
614 | * @param string $queueName The queue name. |
||
615 | * @param PeekMessagesOptions $options The optional parameters. |
||
616 | * |
||
617 | * @return Models\PeekMessagesResult |
||
618 | */ |
||
619 | public function peekMessages($queueName, PeekMessagesOptions $options = null) |
||
623 | |||
624 | /** |
||
625 | * Creates promise to retrieve a message from the front of the queue, |
||
626 | * without changing the message visibility. |
||
627 | * |
||
628 | * @param string $queueName The queue name. |
||
629 | * @param PeekMessagesOptions $options The optional parameters. |
||
630 | * |
||
631 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
632 | */ |
||
633 | public function peekMessagesAsync( |
||
675 | |||
676 | /** |
||
677 | * Sets user-defined metadata on the queue. To delete queue metadata, call |
||
678 | * this API without specifying any metadata in $metadata. |
||
679 | * |
||
680 | * @param string $queueName The queue name. |
||
681 | * @param array $metadata The metadata array. |
||
682 | * @param QueueServiceOptions $options The optional parameters. |
||
683 | * |
||
684 | * @return void |
||
685 | */ |
||
686 | public function setQueueMetadata( |
||
693 | |||
694 | /** |
||
695 | * Creates promise to set user-defined metadata on the queue. To delete |
||
696 | * queue metadata, call this API without specifying any metadata in $metadata. |
||
697 | * |
||
698 | * @param string $queueName The queue name. |
||
699 | * @param array $metadata The metadata array. |
||
700 | * @param QueueServiceOptions $options The optional parameters. |
||
701 | * |
||
702 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
703 | */ |
||
704 | public function setQueueMetadataAsync( |
||
742 | |||
743 | /** |
||
744 | * Updates the visibility timeout of a message and/or the message contents. |
||
745 | * |
||
746 | * @param string $queueName The queue name. |
||
747 | * @param string $messageId The id of the message. |
||
748 | * @param string $popReceipt The valid pop receipt |
||
749 | * value returned from an earlier call to the Get Messages or Update Message |
||
750 | * operation. |
||
751 | * @param string $messageText The message contents. |
||
752 | * @param int $visibilityTimeoutInSeconds Specifies the new |
||
753 | * visibility timeout value, in seconds, relative to server time. |
||
754 | * The new value must be larger than or equal to 0, and cannot be larger |
||
755 | * than 7 days. The visibility timeout of a message cannot be set to a value |
||
756 | * later than the expiry time. A message can be updated until it has been |
||
757 | * deleted or has expired. |
||
758 | * @param QueueServiceOptions $options The optional |
||
759 | * parameters. |
||
760 | * |
||
761 | * @return Models\UpdateMessageResult |
||
762 | */ |
||
763 | public function updateMessage( |
||
780 | |||
781 | /** |
||
782 | * Creates promise to update the visibility timeout of a message and/or the |
||
783 | * message contents. |
||
784 | * |
||
785 | * @param string $queueName The queue name. |
||
786 | * @param string $messageId The id of the message. |
||
787 | * @param string $popReceipt The valid pop receipt |
||
788 | * value returned from an earlier call to the Get Messages or Update Message |
||
789 | * operation. |
||
790 | * @param string $messageText The message contents. |
||
791 | * @param int $visibilityTimeoutInSeconds Specifies the new |
||
792 | * visibility timeout value, in seconds, relative to server time. |
||
793 | * The new value must be larger than or equal to 0, and cannot be larger |
||
794 | * than 7 days. The visibility timeout of a message cannot be set to a value |
||
795 | * later than the expiry time. A message can be updated until it has been |
||
796 | * deleted or has expired. |
||
797 | * @param QueueServiceOptions $options The optional |
||
798 | * parameters. |
||
799 | * |
||
800 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
801 | */ |
||
802 | public function updateMessageAsync( |
||
876 | |||
877 | /** |
||
878 | * Gets the access control list (ACL) |
||
879 | * |
||
880 | * @param string $queue The queue name. |
||
881 | * @param Models\QueueServiceOptions $options The optional parameters. |
||
882 | * |
||
883 | * @return Models\QueueACL |
||
884 | * |
||
885 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-queue-acl |
||
886 | */ |
||
887 | public function getQueueAcl( |
||
893 | |||
894 | /** |
||
895 | * Creates the promise to gets the access control list (ACL) |
||
896 | * |
||
897 | * @param string $queue The queue name. |
||
898 | * @param Models\QueueServiceOptions $options The optional parameters. |
||
899 | * |
||
900 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
901 | * |
||
902 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-queue-acl |
||
903 | */ |
||
904 | View Code Duplication | public function getQueueAclAsync( |
|
945 | |||
946 | /** |
||
947 | * Sets the ACL. |
||
948 | * |
||
949 | * @param string $queue name |
||
950 | * @param Models\QueueACL $acl access control list for Queue |
||
951 | * @param Models\QueueServiceOptions $options optional parameters |
||
952 | * |
||
953 | * @return void |
||
954 | * |
||
955 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-queue-acl |
||
956 | */ |
||
957 | public function setQueueAcl( |
||
964 | |||
965 | /** |
||
966 | * Creates promise to set the ACL |
||
967 | * |
||
968 | * @param string $queue name |
||
969 | * @param Models\QueueACL $acl access control list for Queue |
||
970 | * @param Models\QueueServiceOptions $options optional parameters |
||
971 | * |
||
972 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
973 | * |
||
974 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-queue-acl |
||
975 | */ |
||
976 | View Code Duplication | public function setQueueAclAsync( |
|
1014 | } |
||
1015 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.