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 |
||
31 | class ReportsController extends AppController |
||
32 | { |
||
33 | public $components = [ |
||
34 | 'RequestHandler', |
||
35 | 'OrderSearch', |
||
36 | ]; |
||
37 | |||
38 | public $helpers = [ |
||
39 | 'Html', |
||
40 | 'Form', |
||
41 | 'Reports', |
||
42 | 'Incidents', |
||
43 | ]; |
||
44 | public $uses = [ |
||
45 | 'Incidents', |
||
46 | 'Reports', |
||
47 | 'Notifications', |
||
48 | 'Developers', |
||
49 | ]; |
||
50 | |||
51 | 1 | public function index() |
|
83 | |||
84 | 1 | public function view($reportId) |
|
124 | |||
125 | 1 | public function data_tables() |
|
126 | { |
||
127 | $subquery_params = [ |
||
128 | 'fields' => [ |
||
129 | 1 | 'report_id' => 'report_id', |
|
130 | 'inci_count' => 'COUNT(id)', |
||
131 | ], |
||
132 | 'group' => 'report_id', |
||
133 | ]; |
||
134 | 1 | $subquery = TableRegistry::get('incidents')->find('all', $subquery_params); |
|
135 | |||
136 | // override automatic aliasing, for proper usage in joins |
||
137 | $aColumns = [ |
||
138 | 1 | 'id' => 'id', |
|
139 | 'error_name' => 'error_name', |
||
140 | 'error_message' => 'error_message', |
||
141 | 'location' => 'location', |
||
142 | 'pma_version' => 'pma_version', |
||
143 | 'status' => 'status', |
||
144 | 'exception_type' => 'exception_type', |
||
145 | 'inci_count' => 'inci_count', |
||
146 | ]; |
||
147 | |||
148 | 1 | $searchConditions = $this->OrderSearch->getSearchConditions($aColumns); |
|
149 | 1 | $orderConditions = $this->OrderSearch->getOrder($aColumns); |
|
150 | |||
151 | $params = [ |
||
152 | 1 | 'fields' => $aColumns, |
|
153 | 'conditions' => [ |
||
154 | 1 | $searchConditions, |
|
155 | 1 | 'related_to is NULL', |
|
156 | ], |
||
157 | 1 | 'order' => $orderConditions, |
|
158 | ]; |
||
159 | |||
160 | 1 | $pagedParams = $params; |
|
161 | 1 | $pagedParams['limit'] = intval($this->request->query('iDisplayLength')); |
|
162 | 1 | $pagedParams['offset'] = intval($this->request->query('iDisplayStart')); |
|
163 | |||
164 | 1 | $rows = $this->_findAllDataTable( |
|
165 | 1 | $this->Reports->find('all', $pagedParams)->innerJoin( |
|
166 | 1 | ['incidents' => $subquery], |
|
167 | 1 | ['incidents.report_id = Reports.id'] |
|
168 | ) |
||
169 | ); |
||
170 | //$rows = Sanitize::clean($rows); |
||
171 | 1 | $totalFiltered = $this->Reports->find('all', $params)->count(); |
|
172 | |||
173 | // change exception_type from boolean values to strings |
||
174 | // add incident count for related reports |
||
175 | 1 | $dispRows = []; |
|
176 | 1 | foreach ($rows as $row) { |
|
177 | 1 | $row[5] = $this->Reports->status[$row[5]]; |
|
178 | 1 | $row[6] = (intval($row[6])) ? ('php') : ('js'); |
|
179 | $input_elem = "<input type='checkbox' name='reports[]' value='" |
||
180 | 1 | . $row[0] |
|
181 | 1 | . "'/>"; |
|
182 | |||
183 | $subquery_params_count = [ |
||
184 | 'fields' => [ |
||
185 | 1 | 'report_id' => 'report_id', |
|
186 | ], |
||
187 | ]; |
||
188 | 1 | $subquery_count = TableRegistry::get('incidents')->find( |
|
189 | 1 | 'all', |
|
190 | $subquery_params_count |
||
191 | ); |
||
192 | |||
193 | $params_count = [ |
||
194 | 1 | 'fields' => ['inci_count' => 'inci_count'], |
|
195 | 'conditions' => [ |
||
196 | 1 | 'related_to = ' . $row[0], |
|
197 | ], |
||
198 | ]; |
||
199 | |||
200 | 1 | $inci_count_related = $this->Reports->find('all', $params_count)->innerJoin( |
|
201 | 1 | ['incidents' => $subquery_count], |
|
202 | 1 | ['incidents.report_id = Reports.related_to'] |
|
203 | 1 | )->count(); |
|
204 | |||
205 | 1 | $row[7] += $inci_count_related; |
|
206 | |||
207 | 1 | array_unshift($row, $input_elem); |
|
208 | 1 | array_push($dispRows, $row); |
|
209 | } |
||
210 | |||
211 | $response = [ |
||
212 | 1 | 'iTotalRecords' => $this->Reports->find('all')->count(), |
|
213 | 1 | 'iTotalDisplayRecords' => $totalFiltered, |
|
214 | 1 | 'sEcho' => intval($this->request->query('sEcho')), |
|
215 | 1 | 'aaData' => $dispRows |
|
216 | ]; |
||
217 | 1 | $this->autoRender = false; |
|
218 | 1 | $this->response->body(json_encode($response)); |
|
219 | |||
220 | 1 | return $this->response; |
|
221 | } |
||
222 | |||
223 | 1 | public function mark_related_to($reportId) |
|
251 | |||
252 | 1 | public function unmark_related_to($reportId) |
|
275 | |||
276 | 1 | public function change_state($reportId) |
|
306 | |||
307 | /** |
||
308 | * To carry out mass actions on Reports |
||
309 | * Currently only to change their statuses. |
||
310 | * Can be Extended for other mass operations as well. |
||
311 | * Expects an array of Report Ids as a POST parameter. |
||
312 | * @return void |
||
313 | */ |
||
314 | 1 | public function mass_action() |
|
360 | |||
361 | 1 | protected function _setSimilarFields($id) |
|
377 | |||
378 | protected function _findArrayList($results, $key) |
||
387 | |||
388 | protected function _findAllDataTable($results) |
||
402 | } |
||
403 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.