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 |
||
| 3 | class DMSDocumentCartController extends DMSCartAbstractController |
||
|
|
|||
| 4 | { |
||
| 5 | private static $url_handlers = array( |
||
| 6 | '$Action//$ID' => 'handleAction', |
||
| 7 | ); |
||
| 8 | |||
| 9 | private static $allowed_actions = array( |
||
| 10 | 'DMSCartEditForm', |
||
| 11 | 'add', |
||
| 12 | 'deduct', |
||
| 13 | 'remove', |
||
| 14 | 'view' |
||
| 15 | ); |
||
| 16 | |||
| 17 | public function init() |
||
| 22 | /** |
||
| 23 | * See {@link DMSDocumentCart::getItems()} |
||
| 24 | * |
||
| 25 | * @return ArrayList |
||
| 26 | */ |
||
| 27 | public function items() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Prepares receiver info for the template. |
||
| 34 | * Additionally it uses Zend_Locale to retrieve the localised spelling of the Country |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function getReceiverInfo() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * See DMSDocumentCart::isCartEmpty() |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function getIsCartEmpty() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add quantity to an item that exists in {@link DMSDocumentCart}. |
||
| 68 | * If the item does nt exist, try to add a new item of the particular |
||
| 69 | * class given the URL parameters available. |
||
| 70 | * |
||
| 71 | * @param SS_HTTPRequest $request |
||
| 72 | * |
||
| 73 | * @return SS_HTTPResponse|string |
||
| 74 | */ |
||
| 75 | public function add(SS_HTTPRequest $request) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Deduct quantity from an item that exists in {@link DMSDocumentCart} |
||
| 121 | * |
||
| 122 | * @param SS_HTTPRequest $request |
||
| 123 | * |
||
| 124 | * @return SS_HTTPResponse|string |
||
| 125 | */ |
||
| 126 | public function deduct(SS_HTTPRequest $request) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Completely remove an item that exists in {@link DMSDocumentCart} |
||
| 146 | * |
||
| 147 | * @param SS_HTTPRequest $request |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function remove(SS_HTTPRequest $request) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Validates a request to add a document to the cart |
||
| 166 | * |
||
| 167 | * @param int $quantity |
||
| 168 | * @param DMSDocument $document |
||
| 169 | * @return ValidationResult |
||
| 170 | */ |
||
| 171 | protected function validateAddRequest($quantity, DMSDocument $document) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Updates the document quantities just before the request is sent. |
||
| 194 | * |
||
| 195 | * @param array $data |
||
| 196 | * @param Form $form |
||
| 197 | * @param SS_HTTPRequest $request |
||
| 198 | * |
||
| 199 | * @return SS_HTTPResponse |
||
| 200 | */ |
||
| 201 | public function updateCartItems($data, Form $form, SS_HTTPRequest $request) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Presents an interface for user to update the cart quantities |
||
| 231 | * |
||
| 232 | * @param SS_HTTPRequest $request |
||
| 233 | * @return ViewableData_Customised |
||
| 234 | */ |
||
| 235 | public function view(SS_HTTPRequest $request) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Gets and displays an editable list of items within the cart. |
||
| 250 | * |
||
| 251 | * To extend use the following from within an Extension subclass: |
||
| 252 | * |
||
| 253 | * <code> |
||
| 254 | * public function updateDMSCartEditForm($form) |
||
| 255 | * { |
||
| 256 | * // Do something here |
||
| 257 | * } |
||
| 258 | * </code> |
||
| 259 | * |
||
| 260 | * @return Form |
||
| 261 | */ |
||
| 262 | public function DMSCartEditForm() |
||
| 280 | |||
| 281 | public function getContinueBrowsingLink() |
||
| 285 | } |
||
| 286 |
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.