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:
| 1 | <?php |
||
| 21 | class Queue extends AbstractArray implements QueueInterface |
||
| 22 | { |
||
| 23 | use TypeTrait; |
||
| 24 | use ValueToStringTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $queueType; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | private $index = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Queue constructor. |
||
| 38 | */ |
||
| 39 | public function __construct($type, array $data = []) |
||
| 44 | |||
| 45 | View Code Duplication | public function offsetSet($offset, $value) |
|
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Ensures that this queue contains the specified element (optional |
||
| 60 | * operation). Returns true if this queue changed as a result of the |
||
| 61 | * call. (Returns false if this queue does not permit duplicates and |
||
| 62 | * already contains the specified element.) |
||
| 63 | * |
||
| 64 | * Queues that support this operation may place limitations on what |
||
| 65 | * elements may be added to this queue. In particular, some |
||
| 66 | * queues will refuse to add null elements, and others will impose |
||
| 67 | * restrictions on the type of elements that may be added. Queue |
||
| 68 | * classes should clearly specify in their documentation any restrictions |
||
| 69 | * on what elements may be added. |
||
| 70 | * |
||
| 71 | * If a queue refuses to add a particular element for any reason other |
||
| 72 | * than that it already contains the element, it must throw an exception |
||
| 73 | * (rather than returning false). This preserves the invariant that a |
||
| 74 | * queue always contains the specified element after this call returns. |
||
| 75 | * |
||
| 76 | * @param mixed $element |
||
| 77 | * @return bool true if this queue changed as a result of the call |
||
| 78 | */ |
||
| 79 | public function add($element) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Retrieves, but does not remove, the head of this queue. This method |
||
| 88 | * differs from peek only in that it throws an exception if this queue is empty. |
||
| 89 | * |
||
| 90 | * @return mixed the head of this queue |
||
| 91 | * @throws NoSuchElementException |
||
| 92 | */ |
||
| 93 | public function element() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Inserts the specified element into this queue if it is possible to do so |
||
| 106 | * immediately without violating capacity restrictions. When using a |
||
| 107 | * capacity-restricted queue, this method is generally preferable to add(E), |
||
| 108 | * which can fail to insert an element only by throwing an exception. |
||
| 109 | * |
||
| 110 | * @param $element |
||
| 111 | * @return mixed true if the element was added to this queue, else false |
||
| 112 | */ |
||
| 113 | public function offer($element) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Retrieves, but does not remove, the head of this queue, or returns null |
||
| 122 | * if this queue is empty. |
||
| 123 | * |
||
| 124 | * @return mixed the head of this queue, or null if this queue is empty |
||
| 125 | */ |
||
| 126 | public function peek() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Retrieves and removes the head of this queue, or returns null |
||
| 137 | * if this queue is empty. |
||
| 138 | * |
||
| 139 | * @return mixed the head of this queue, or null if this queue is empty |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function poll() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Retrieves and removes the head of this queue. This method differs |
||
| 157 | * from poll only in that it throws an |
||
| 158 | * exception if this queue is empty. |
||
| 159 | * |
||
| 160 | * @return mixed the head of this queue |
||
| 161 | * @throws NoSuchElementException |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function remove() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the type associated with this collection |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getType() |
||
| 185 | } |
||
| 186 |
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.