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 ContentController |
||
|
|||
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 | * Retrieves a {@link DMSDocumentCart} instance |
||
166 | * |
||
167 | * @return DMSDocumentCart |
||
168 | */ |
||
169 | public function getCart() |
||
173 | |||
174 | /** |
||
175 | * Validates a request to add a document to the cart |
||
176 | * |
||
177 | * @param int $quantity |
||
178 | * @param DMSDocument $document |
||
179 | * @return ValidationResult |
||
180 | */ |
||
181 | protected function validateAddRequest($quantity, DMSDocument $document) |
||
201 | |||
202 | /** |
||
203 | * Updates the document quantities just before the request is sent. |
||
204 | * |
||
205 | * @param array $data |
||
206 | * @param Form $form |
||
207 | * @param SS_HTTPRequest $request |
||
208 | */ |
||
209 | public function updateCartItems($data, Form $form, SS_HTTPRequest $request) |
||
236 | |||
237 | /** |
||
238 | * Presents an interface for user to update the cart quantities |
||
239 | * |
||
240 | * @param SS_HTTPRequest $request |
||
241 | * @return ViewableData_Customised |
||
242 | */ |
||
243 | public function view(SS_HTTPRequest $request) |
||
244 | { |
||
245 | $this->getCart()->setViewOnly(true); |
||
246 | $form = $this->DMSCartEditForm(); |
||
247 | return $this |
||
248 | ->customise( |
||
249 | array( |
||
250 | 'Form' => $form, |
||
251 | 'Title' => _t(__CLASS__ . '.UPDATE_TITLE', 'Updating cart items') |
||
252 | ) |
||
253 | ); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Gets and displays an editable list of items within the cart. |
||
258 | * |
||
259 | * To extend use the following from within an Extension subclass: |
||
260 | * |
||
261 | * <code> |
||
262 | * public function updateDMSCartEditForm($form) |
||
263 | * { |
||
264 | * // Do something here |
||
265 | * } |
||
266 | * </code> |
||
267 | * |
||
268 | * @return Form |
||
269 | */ |
||
270 | public function DMSCartEditForm() |
||
288 | |||
289 | /** |
||
290 | * Ensure that links for this controller use the customised route |
||
291 | * |
||
292 | * @param string $action |
||
293 | * @return string |
||
294 | */ |
||
295 | public function Link($action = null) |
||
301 | } |
||
302 |
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.