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 |
||
| 6 | class DMSDocumentCart extends ViewableData |
||
|
|
|||
| 7 | { |
||
| 8 | /** |
||
| 9 | * A handle to the classes' {@link DMSCartBackendInterface} |
||
| 10 | * |
||
| 11 | * @var DMSCartBackendInterface |
||
| 12 | */ |
||
| 13 | protected $backend; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Variable to control whether a cart is being updated or not |
||
| 17 | * |
||
| 18 | * @var bool |
||
| 19 | */ |
||
| 20 | private $viewOnly = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Instantiate a cart backend either by that provided, or a session default |
||
| 24 | * |
||
| 25 | * @param DMSCartBackendInterface $backend |
||
| 26 | * @throws DMSDocumentCartException If a backend was provided but doesn't implement the backend interface |
||
| 27 | */ |
||
| 28 | public function __construct($backend = null) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Returns all the cart items as an array |
||
| 39 | * |
||
| 40 | * @return ArrayList |
||
| 41 | */ |
||
| 42 | public function getItems() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Add an {@link DMSRequestItem} object into the cart. |
||
| 49 | * |
||
| 50 | * @param DMSRequestItem $item |
||
| 51 | * |
||
| 52 | * @return DMSDocumentCart |
||
| 53 | */ |
||
| 54 | public function addItem(DMSRequestItem $item) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get a {@link DMSRequestItem} object from the cart. |
||
| 63 | * |
||
| 64 | * @param int $itemID The ID of the item |
||
| 65 | * |
||
| 66 | * @return DMSRequestItem|boolean |
||
| 67 | */ |
||
| 68 | public function getItem($itemID) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Removes a {@link DMSRequestItem} from the cart by it's id |
||
| 75 | * |
||
| 76 | * @param DMSRequestItem $item |
||
| 77 | * |
||
| 78 | * @return DMSDocumentCart |
||
| 79 | */ |
||
| 80 | public function removeItem(DMSRequestItem $item) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Removes a {@link DMSRequestItem} from the cart by it's id |
||
| 89 | * |
||
| 90 | * @param int $itemID |
||
| 91 | * |
||
| 92 | * @return DMSDocumentCart |
||
| 93 | */ |
||
| 94 | public function removeItemByID($itemID) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Adjusts (increments, decrements or amends) the quantity of an {@link DMSRequestItem}.' |
||
| 103 | * A positive $quantity increments the total, whereas a negative value decrements the total. A cart item |
||
| 104 | * is removed completely if it's value reaches <= 0. |
||
| 105 | * |
||
| 106 | * @param int $itemID |
||
| 107 | * @param int $quantity |
||
| 108 | * |
||
| 109 | * @return DMSDocumentCart |
||
| 110 | */ |
||
| 111 | public function updateItemQuantity($itemID, $quantity) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Completely empties a cart |
||
| 129 | * |
||
| 130 | * @return DMSDocumentCart |
||
| 131 | */ |
||
| 132 | public function emptyCart() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Checks if a cart is empty. |
||
| 141 | * Returns true if cart is empty, false otherwise. |
||
| 142 | * |
||
| 143 | * @return boolean |
||
| 144 | */ |
||
| 145 | public function isCartEmpty() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the backURL to be a Session variable for the current Document Cart |
||
| 154 | * |
||
| 155 | * @param string $backURL |
||
| 156 | * |
||
| 157 | * @return DMSDocumentCart |
||
| 158 | */ |
||
| 159 | public function setBackUrl($backURL) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns the backURL for the current Document Cart |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getBackUrl() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Sets the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
||
| 178 | * |
||
| 179 | * @param array $receiverInfo |
||
| 180 | * |
||
| 181 | * @return DMSDocumentCart |
||
| 182 | */ |
||
| 183 | public function setReceiverInfo($receiverInfo) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Retrieves the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap')) |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function getReceiverInfo() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the recipients in a Viewable format |
||
| 202 | * |
||
| 203 | * @return ArrayData|bool |
||
| 204 | */ |
||
| 205 | public function getReceiverInfoNice() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets the backend handler |
||
| 212 | * |
||
| 213 | * @return DMSSessionBackend |
||
| 214 | */ |
||
| 215 | public function getBackend() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Checks if an item exists within a cart. Returns true (if exists) or false. |
||
| 222 | * |
||
| 223 | * @param int $itemID |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function isInCart($itemID) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Persists a cart submission to the database |
||
| 234 | * |
||
| 235 | * @param Form $form |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | public function saveSubmission(Form $form) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns true if the cart is being updated. False otherwise |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function isViewOnly() |
||
| 263 | { |
||
| 264 | return $this->viewOnly; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sets the updating flag |
||
| 269 | * |
||
| 270 | * @param bool $updating |
||
| 271 | * @return DMSDocumentCart |
||
| 272 | */ |
||
| 273 | public function setViewOnly($updating) |
||
| 274 | { |
||
| 275 | $this->viewOnly = (bool) $updating; |
||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Displays a view-only table of the cart items. |
||
| 281 | * |
||
| 282 | * @return HTMLText |
||
| 283 | */ |
||
| 284 | public function getSummary() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Utility method to link to the current controllers action |
||
| 291 | * |
||
| 292 | * @param string $action |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getLink($action) |
||
| 305 | } |
||
| 306 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.