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) |
||
193 | |||
194 | /** |
||
195 | * Updates the document quantities just before the request is sent. |
||
196 | * |
||
197 | * @param array $data |
||
198 | * @param Form $form |
||
199 | * @param SS_HTTPRequest $request |
||
200 | * |
||
201 | * @return SS_HTTPResponse |
||
202 | */ |
||
203 | public function updateCartItems($data, Form $form, SS_HTTPRequest $request) |
||
204 | { |
||
205 | $errors = array(); |
||
206 | if (!empty($data['ItemQuantity'])) { |
||
207 | foreach ($data['ItemQuantity'] as $itemID => $quantity) { |
||
208 | if (!is_numeric($quantity) || $quantity < 0) { |
||
209 | continue; |
||
210 | } |
||
211 | // Only update if the document is valid and the quantity has changed |
||
212 | $item = $this->getCart()->getItem($itemID); |
||
213 | if (!$item || $item->getQuantity() == $quantity) { |
||
214 | continue; |
||
215 | } |
||
216 | // No validate item |
||
217 | $validate = $this->validateAddRequest($quantity, $item->getDocument()); |
||
218 | if ($validate->valid()) { |
||
219 | // Removes, then adds a item new item. |
||
220 | $this->getCart()->removeItem($item); |
||
221 | $this->getCart()->addItem($item->setQuantity($quantity)); |
||
222 | } else { |
||
223 | $errors[] = $validate->starredList(); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | if (!empty($errors)) { |
||
229 | $form->sessionMessage(implode('<br>', $errors), 'bad', false); |
||
230 | return $this->redirectBack(); |
||
231 | } |
||
232 | |||
233 | return $this->redirect($this->getCart()->getBackUrl()); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Presents an interface for user to update the cart quantities |
||
238 | * |
||
239 | * @param SS_HTTPRequest $request |
||
240 | * @return ViewableData_Customised |
||
241 | */ |
||
242 | public function view(SS_HTTPRequest $request) |
||
254 | |||
255 | /** |
||
256 | * Gets and displays an editable list of items within the cart. |
||
257 | * |
||
258 | * To extend use the following from within an Extension subclass: |
||
259 | * |
||
260 | * <code> |
||
261 | * public function updateDMSCartEditForm($form) |
||
262 | * { |
||
263 | * // Do something here |
||
264 | * } |
||
265 | * </code> |
||
266 | * |
||
267 | * @return Form |
||
268 | */ |
||
269 | public function DMSCartEditForm() |
||
287 | } |
||
288 |
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.