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:
Complex classes like ControllerModuleProducts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ControllerModuleProducts, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ControllerModuleProducts extends Controller |
||
13 | { |
||
14 | private $error = array(); |
||
15 | |||
16 | public function index() |
||
17 | { |
||
18 | $this->load->language('module/products'); |
||
19 | |||
20 | $this->document->setTitle($this->language->get('heading_title')); |
||
21 | |||
22 | $this->load->model('extension/module'); |
||
23 | |||
24 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { |
||
25 | if (!isset($this->request->get['module_id'])) { |
||
26 | $this->model_extension_module->addModule('products', $this->request->post); |
||
27 | } else { |
||
28 | $this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post); |
||
29 | } |
||
30 | |||
31 | $this->session->data['success'] = $this->language->get('text_success'); |
||
32 | |||
33 | if (isset($this->request->post['button']) && $this->request->post['button'] == 'save') { |
||
34 | $module_id = ''; |
||
35 | |||
36 | if (isset($this->request->get['module_id'])) { |
||
37 | $module_id = '&module_id=' . $this->request->get['module_id']; |
||
38 | } elseif ($this->db->getLastId()) { |
||
39 | $module_id = '&module_id=' . $this->db->getLastId(); |
||
40 | } |
||
41 | |||
42 | $this->response->redirect($this->url->link('module/products', 'token=' . $this->session->data['token'] . $module_id, 'SSL')); |
||
43 | } |
||
44 | |||
45 | if (isset($this->request->post['button']) && $this->request->post['button'] == 'new') { |
||
46 | $this->response->redirect($this->url->link('module/products', 'token=' . $this->session->data['token'], 'SSL')); |
||
47 | } |
||
48 | |||
49 | $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')); |
||
50 | } |
||
51 | |||
52 | #Get All Language Text |
||
53 | $data = $this->language->all(); |
||
54 | |||
55 | if (isset($this->error['warning'])) { |
||
56 | $data['error_warning'] = $this->error['warning']; |
||
57 | } else { |
||
58 | $data['error_warning'] = ''; |
||
59 | } |
||
60 | |||
61 | if (isset($this->error['name'])) { |
||
62 | $data['error_name'] = $this->error['name']; |
||
63 | } else { |
||
64 | $data['error_name'] = ''; |
||
65 | } |
||
66 | |||
67 | if (!isset($this->request->get['module_id'])) { |
||
68 | $data['action'] = $this->url->link('module/products', 'token=' . $this->session->data['token'], 'SSL'); |
||
69 | } else { |
||
70 | $data['action'] = $this->url->link('module/products', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL'); |
||
71 | } |
||
72 | |||
73 | $data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'); |
||
74 | |||
75 | View Code Duplication | if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { |
|
76 | $module_info = $this->model_extension_module->getModule($this->request->get['module_id']); |
||
77 | } |
||
78 | |||
79 | $data['token'] = $this->session->data['token']; |
||
80 | |||
81 | if (isset($this->request->post['name'])) { |
||
82 | $data['name'] = $this->request->post['name']; |
||
83 | } elseif (!empty($module_info)) { |
||
84 | $data['name'] = $module_info['name']; |
||
85 | } else { |
||
86 | $data['name'] = ''; |
||
87 | } |
||
88 | |||
89 | if (isset($this->request->post['title'])) { |
||
90 | $data['title'] = $this->request->post['title']; |
||
91 | } elseif (!empty($module_info)) { |
||
92 | $data['title'] = $module_info['title']; |
||
93 | } else { |
||
94 | $data['title'] = ''; |
||
95 | } |
||
96 | |||
97 | if (isset($this->request->post['show_title'])) { |
||
98 | $data['show_title'] = $this->request->post['show_title']; |
||
99 | } elseif (!empty($module_info)) { |
||
100 | $data['show_title'] = $module_info['show_title']; |
||
101 | } else { |
||
102 | $data['show_title'] = '0'; |
||
103 | } |
||
104 | |||
105 | if (isset($this->request->post['module_class'])) { |
||
106 | $data['module_class'] = $this->request->post['module_class']; |
||
107 | } elseif (!empty($module_info)) { |
||
108 | $data['module_class'] = $module_info['module_class']; |
||
109 | } else { |
||
110 | $data['module_class'] = ''; |
||
111 | } |
||
112 | |||
113 | if (isset($this->request->post['module_column'])) { |
||
114 | $data['module_column'] = $this->request->post['module_column']; |
||
115 | } elseif (!empty($module_info)) { |
||
116 | $data['module_column'] = $module_info['module_column']; |
||
117 | } else { |
||
118 | $data['module_column'] = '4'; |
||
119 | } |
||
120 | |||
121 | if (isset($this->request->post['type'])) { |
||
122 | $data['type'] = $this->request->post['type']; |
||
123 | } elseif (!empty($module_info)) { |
||
124 | $data['type'] = $module_info['type']; |
||
125 | } else { |
||
126 | $data['type'] = ''; |
||
127 | } |
||
128 | |||
129 | // Categories |
||
130 | $this->load->model('catalog/category'); |
||
131 | |||
132 | View Code Duplication | if (isset($this->request->post['category'])) { |
|
133 | $categories = $this->request->post['category']; |
||
134 | } elseif (!empty($module_info['category'])) { |
||
135 | $categories = $module_info['category']; |
||
136 | } else { |
||
137 | $categories = array(); |
||
138 | } |
||
139 | |||
140 | $data['categories'] = array(); |
||
141 | |||
142 | if (!empty($categories)) { |
||
143 | View Code Duplication | foreach ($categories as $category_id) { |
|
144 | $category_info = $this->model_catalog_category->getCategory($category_id); |
||
145 | |||
146 | if ($category_info) { |
||
147 | $data['categories'][] = array( |
||
148 | 'category_id' => $category_info['category_id'], |
||
149 | 'name' => ($category_info['path']) ? $category_info['path'] . ' > ' . $category_info['name'] : $category_info['name'] |
||
150 | ); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | // Products |
||
156 | $this->load->model('catalog/product'); |
||
157 | |||
158 | $data['products'] = array(); |
||
159 | |||
160 | View Code Duplication | if (!empty($this->request->post['product'])) { |
|
161 | $products = $this->request->post['product']; |
||
162 | } elseif (!empty($module_info['product'])) { |
||
163 | $products = $module_info['product']; |
||
164 | } else { |
||
165 | $products = array(); |
||
166 | } |
||
167 | |||
168 | View Code Duplication | if (!empty($products)) { |
|
169 | foreach ($products as $product_id) { |
||
170 | $product_info = $this->model_catalog_product->getProduct($product_id); |
||
171 | |||
172 | if ($product_info) { |
||
173 | $data['products'][] = array( |
||
174 | 'product_id' => $product_info['product_id'], |
||
175 | 'name' => $product_info['name'] |
||
176 | ); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | if (isset($this->request->post['random_product'])) { |
||
182 | $data['random_product'] = $this->request->post['random_product']; |
||
183 | } elseif (!empty($module_info)) { |
||
184 | $data['random_product'] = $module_info['random_product']; |
||
185 | } else { |
||
186 | $data['random_product'] = '0'; |
||
187 | } |
||
188 | |||
189 | if (isset($this->request->post['limit'])) { |
||
190 | $data['limit'] = $this->request->post['limit']; |
||
191 | } elseif (!empty($module_info)) { |
||
192 | $data['limit'] = $module_info['limit']; |
||
193 | } else { |
||
194 | $data['limit'] = '4'; |
||
195 | } |
||
196 | |||
197 | if (isset($this->request->post['width'])) { |
||
198 | $data['width'] = $this->request->post['width']; |
||
199 | } elseif (!empty($module_info)) { |
||
200 | $data['width'] = $module_info['width']; |
||
201 | } else { |
||
202 | $data['width'] = '200'; |
||
203 | } |
||
204 | |||
205 | if (isset($this->request->post['height'])) { |
||
206 | $data['height'] = $this->request->post['height']; |
||
207 | } elseif (!empty($module_info)) { |
||
208 | $data['height'] = $module_info['height']; |
||
209 | } else { |
||
210 | $data['height'] = '200'; |
||
211 | } |
||
212 | |||
213 | if (isset($this->request->post['status'])) { |
||
214 | $data['status'] = $this->request->post['status']; |
||
215 | } elseif (!empty($module_info)) { |
||
216 | $data['status'] = $module_info['status']; |
||
217 | } else { |
||
218 | $data['status'] = 1; |
||
219 | } |
||
220 | |||
221 | if (isset($this->request->post['product_image'])) { |
||
222 | $data['product_image'] = $this->request->post['product_image']; |
||
223 | } elseif (!empty($module_info)) { |
||
224 | $data['product_image'] = $module_info['product_image']; |
||
225 | } else { |
||
226 | $data['product_image'] = '1'; |
||
227 | } |
||
228 | |||
229 | if (isset($this->request->post['product_name'])) { |
||
230 | $data['product_name'] = $this->request->post['product_name']; |
||
231 | } elseif (!empty($module_info)) { |
||
232 | $data['product_name'] = $module_info['product_name']; |
||
233 | } else { |
||
234 | $data['product_name'] = '1'; |
||
235 | } |
||
236 | |||
237 | if (isset($this->request->post['product_description'])) { |
||
238 | $data['product_description'] = $this->request->post['product_description']; |
||
239 | } elseif (!empty($module_info)) { |
||
240 | $data['product_description'] = $module_info['product_description']; |
||
241 | } else { |
||
242 | $data['product_description'] = '1'; |
||
243 | } |
||
244 | |||
245 | if (isset($this->request->post['product_rating'])) { |
||
246 | $data['product_rating'] = $this->request->post['product_rating']; |
||
247 | } elseif (!empty($module_info)) { |
||
248 | $data['product_rating'] = $module_info['product_rating']; |
||
249 | } else { |
||
250 | $data['product_rating'] = '1'; |
||
251 | } |
||
252 | |||
253 | if (isset($this->request->post['product_price'])) { |
||
254 | $data['product_price'] = $this->request->post['product_price']; |
||
255 | } elseif (!empty($module_info)) { |
||
256 | $data['product_price'] = $module_info['product_price']; |
||
257 | } else { |
||
258 | $data['product_price'] = '1'; |
||
259 | } |
||
260 | |||
261 | if (isset($this->request->post['add_to_cart'])) { |
||
262 | $data['add_to_cart'] = $this->request->post['add_to_cart']; |
||
263 | } elseif (!empty($module_info)) { |
||
264 | $data['add_to_cart'] = $module_info['add_to_cart']; |
||
265 | } else { |
||
266 | $data['add_to_cart'] = '1'; |
||
267 | } |
||
268 | |||
269 | if (isset($this->request->post['product_image'])) { |
||
270 | $data['wish_list'] = $this->request->post['wish_list']; |
||
271 | } elseif (!empty($module_info)) { |
||
272 | $data['wish_list'] = $module_info['wish_list']; |
||
273 | } else { |
||
274 | $data['wish_list'] = '1'; |
||
275 | } |
||
276 | |||
277 | if (isset($this->request->post['product_image'])) { |
||
278 | $data['compare'] = $this->request->post['compare']; |
||
279 | } elseif (!empty($module_info)) { |
||
280 | $data['compare'] = $module_info['compare']; |
||
281 | } else { |
||
282 | $data['compare'] = '1'; |
||
283 | } |
||
284 | |||
285 | $data['form_fields'] = $this->getFormFields($data); |
||
286 | |||
287 | $data['header'] = $this->load->controller('common/header'); |
||
288 | $data['column_left'] = $this->load->controller('common/column_left'); |
||
289 | $data['footer'] = $this->load->controller('common/footer'); |
||
290 | |||
291 | $this->response->setOutput($this->load->view('module/products.tpl', $data)); |
||
292 | } |
||
293 | |||
294 | protected function getFormFields($data) |
||
399 | |||
400 | View Code Duplication | protected function validate() |
|
412 | } |
||
413 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.