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 = array('RequestHandler', 'OrderSearch'); |
||
34 | |||
35 | public $helpers = array('Html', 'Form', 'Reports', 'Incidents'); |
||
36 | public $uses = array('Incidents', 'Reports', 'Notifications', 'Developers'); |
||
37 | |||
38 | 1 | public function index() |
|
39 | { |
||
40 | 1 | $this->Reports->recursive = -1; |
|
41 | 1 | $this->set( |
|
42 | 1 | 'distinct_statuses', |
|
43 | 1 | $this->_findArrayList( |
|
44 | $this->Reports->find()->select(array('status'))->distinct(array('status')), |
||
45 | 1 | 'status' |
|
46 | 1 | ) |
|
47 | 1 | ); |
|
48 | 1 | $this->set( |
|
49 | 1 | 'distinct_locations', |
|
50 | 1 | $this->_findArrayList( |
|
51 | $this->Reports->find()->select(array('location')) |
||
52 | ->distinct(array('location')), |
||
53 | 1 | 'location' |
|
54 | 1 | ) |
|
55 | ); |
||
56 | 1 | $this->set( |
|
57 | 1 | 'distinct_versions', |
|
58 | 1 | $this->_findArrayList($this->Reports->find()->select(array('pma_version'))->distinct(array('pma_version')), 'pma_version') |
|
59 | ); |
||
60 | 1 | $this->set( |
|
61 | 'distinct_error_names', |
||
62 | 1 | $this->_findArrayList($this->Reports->find('all', array( |
|
63 | 1 | 'fields' => array('error_name'), |
|
64 | 1 | 'conditions' => array('error_name !=' => ''), |
|
65 | ))->distinct(array('error_name')), 'error_name') |
||
66 | 1 | ); |
|
67 | $this->set('statuses', $this->Reports->status); |
||
68 | 1 | $this->autoRender = true; |
|
69 | } |
||
70 | |||
71 | 1 | public function view($reportId) |
|
72 | 1 | { |
|
73 | 1 | if (!isset($reportId) || !$reportId) { |
|
74 | throw new NotFoundException(__('Invalid Report')); |
||
75 | } |
||
76 | 1 | $report = $this->Reports->findById($reportId)->toArray(); |
|
77 | 1 | if (!$report) { |
|
78 | 1 | throw new NotFoundException(__('Invalid Report')); |
|
79 | 1 | } |
|
80 | 1 | ||
81 | 1 | $this->set('report', $report); |
|
82 | 1 | $this->set('project_name', Configure::read('GithubRepoPath')); |
|
83 | 1 | $this->Reports->id = $reportId; |
|
84 | 1 | $this->set('incidents', $this->Reports->getIncidents()->toArray()); |
|
85 | 1 | $this->set( |
|
86 | 1 | 'incidents_with_description', |
|
87 | $this->Reports->getIncidentsWithDescription() |
||
88 | ); |
||
89 | 1 | $this->set( |
|
90 | 1 | 'incidents_with_stacktrace', |
|
91 | $this->Reports->getIncidentsWithDifferentStacktrace() |
||
92 | 1 | ); |
|
93 | 1 | $this->set('related_reports', $this->Reports->getRelatedReports()); |
|
94 | 1 | $this->set('status', $this->Reports->status); |
|
95 | 1 | $this->_setSimilarFields($reportId); |
|
96 | |||
97 | 1 | // if there is an unread notification for this report, then mark it as read |
|
98 | $current_developer = TableRegistry::get('Developers')-> |
||
|
|||
99 | findById($this->request->session()->read('Developer.id'))->all()->first(); |
||
100 | 1 | ||
101 | if ($current_developer) { |
||
102 | 1 | TableRegistry::get('Notifications')->deleteAll( |
|
103 | array('developer_id' => $current_developer['Developer']['id'], |
||
104 | 'report_id' => $reportId, |
||
105 | 1 | ), |
|
106 | false |
||
107 | ); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | 1 | public function data_tables() |
|
112 | { |
||
113 | $subquery_params = array( |
||
114 | 'fields' => array( |
||
115 | 1 | 'report_id' => 'report_id', |
|
116 | 'inci_count' => 'COUNT(id)', |
||
117 | ), |
||
118 | 'group' => 'report_id', |
||
119 | ); |
||
120 | $subquery = TableRegistry::get('incidents')->find('all', $subquery_params); |
||
121 | |||
122 | // override automatic aliasing, for proper usage in joins |
||
123 | $aColumns = array( |
||
124 | 'id' => 'id', |
||
125 | 1 | 'error_name' => 'error_name', |
|
126 | 1 | 'error_message' => 'error_message', |
|
127 | 'location' => 'location', |
||
128 | 'pma_version' => 'pma_version', |
||
129 | 1 | 'status' => 'status', |
|
130 | 'exception_type' => 'exception_type', |
||
131 | 1 | 'inci_count' => 'inci_count', |
|
132 | 1 | ); |
|
133 | |||
134 | 1 | $searchConditions = $this->OrderSearch->getSearchConditions($aColumns); |
|
135 | $orderConditions = $this->OrderSearch->getOrder($aColumns); |
||
136 | |||
137 | 1 | $params = array( |
|
138 | 1 | 'fields' => $aColumns, |
|
139 | 1 | 'conditions' => array( |
|
140 | $searchConditions, |
||
141 | 1 | 'related_to is NULL', |
|
142 | 1 | ), |
|
143 | 1 | 'order' => $orderConditions, |
|
144 | ); |
||
145 | |||
146 | $pagedParams = $params; |
||
147 | 1 | $pagedParams['limit'] = intval($this->request->query('iDisplayLength')); |
|
148 | $pagedParams['offset'] = intval($this->request->query('iDisplayStart')); |
||
149 | |||
150 | $rows = $this->_findAllDataTable( |
||
151 | 1 | $this->Reports->find('all', $pagedParams)->innerJoin( |
|
152 | 1 | array('incidents' => $subquery), |
|
153 | 1 | array('incidents.report_id = Reports.id') |
|
154 | 1 | ) |
|
155 | ); |
||
156 | 1 | //$rows = Sanitize::clean($rows); |
|
157 | 1 | $totalFiltered = $this->Reports->find('all', $params)->count(); |
|
158 | |||
159 | // change exception_type from boolean values to strings |
||
160 | 1 | // add incident count for related reports |
|
161 | $dispRows = array(); |
||
162 | foreach ($rows as $row) { |
||
163 | $row[5] = $this->Reports->status[$row[5]]; |
||
164 | 1 | $row[6] = (intval($row[6])) ? ('php') : ('js'); |
|
165 | 1 | $input_elem = "<input type='checkbox' name='reports[]' value='" |
|
166 | . $row[0] |
||
167 | . "'/>"; |
||
168 | |||
169 | 1 | $subquery_params_count = array( |
|
170 | 'fields' => array( |
||
171 | 1 | 'report_id' => 'report_id', |
|
172 | ), |
||
173 | ); |
||
174 | $subquery_count = TableRegistry::get('incidents')->find( |
||
175 | 1 | 'all', |
|
176 | 1 | $subquery_params_count |
|
177 | 1 | ); |
|
178 | 1 | ||
179 | $params_count = array( |
||
180 | 1 | 'fields' => array('inci_count' => 'inci_count'), |
|
181 | 'conditions' => array( |
||
182 | 1 | 'related_to = ' . $row[0], |
|
183 | 1 | ), |
|
184 | ); |
||
185 | |||
186 | $inci_count_related = $this->Reports->find('all', $params_count)->innerJoin( |
||
187 | 1 | array('incidents' => $subquery_count), |
|
188 | 1 | array('incidents.report_id = Reports.related_to') |
|
189 | 1 | )->count(); |
|
190 | 1 | ||
191 | $row[7] += $inci_count_related; |
||
192 | 1 | ||
193 | 1 | array_unshift($row, $input_elem); |
|
194 | array_push($dispRows, $row); |
||
195 | 1 | } |
|
196 | |||
197 | $response = array( |
||
198 | 1 | 'iTotalRecords' => $this->Reports->find('all')->count(), |
|
199 | 'iTotalDisplayRecords' => $totalFiltered, |
||
200 | 'sEcho' => intval($this->request->query('sEcho')), |
||
201 | 1 | 'aaData' => $dispRows |
|
202 | ); |
||
203 | 1 | $this->autoRender = false; |
|
204 | 1 | $this->response->body(json_encode($response)); |
|
205 | 1 | ||
206 | 1 | return $this->response; |
|
207 | } |
||
208 | |||
209 | public function mark_related_to($reportId) |
||
210 | { |
||
211 | 1 | // Only allow POST requests |
|
212 | 1 | $this->request->allowMethod(['post']); |
|
213 | |||
214 | $relatedTo = $this->request->getData('related_to'); |
||
215 | if (!$reportId |
||
216 | 1 | || !$relatedTo |
|
217 | || $reportId == $relatedTo |
||
218 | 1 | ) { |
|
219 | 1 | throw new NotFoundException(__('Invalid Report')); |
|
220 | 1 | } |
|
221 | 1 | ||
222 | 1 | $report = $this->Reports->get($reportId); |
|
223 | 1 | if (!$report) { |
|
224 | throw new NotFoundException(__('Invalid Report')); |
||
225 | 1 | } |
|
226 | |||
227 | $this->Reports->addToRelatedGroup($report, $relatedTo); |
||
228 | 1 | ||
229 | $flash_class = 'alert alert-success'; |
||
230 | 1 | $this->Flash->default( |
|
231 | 'This report has been marked the same as #' |
||
232 | . $relatedTo, |
||
233 | array('params' => array('class' => $flash_class)) |
||
234 | 1 | ); |
|
235 | 1 | $this->redirect("/reports/view/$reportId"); |
|
236 | } |
||
237 | |||
238 | public function unmark_related_to($reportId) |
||
261 | 1 | ||
262 | 1 | public function change_state($reportId) |
|
263 | { |
||
264 | 1 | if (!$reportId) { |
|
265 | 1 | throw new NotFoundException(__('Invalid Report')); |
|
292 | 1 | ||
293 | 1 | /** |
|
294 | 1 | * To carry out mass actions on Reports |
|
295 | 1 | * Currently only to change their statuses. |
|
296 | * Can be Extended for other mass operations as well. |
||
297 | 1 | * Expects an array of Report Ids as a POST parameter. |
|
298 | 1 | */ |
|
299 | 1 | public function mass_action() |
|
345 | |||
346 | //# HELPERS |
||
347 | |||
348 | 1 | protected function _setSimilarFields($id) |
|
364 | |||
365 | 1 | protected function _findArrayList($results, $key) |
|
374 | |||
375 | protected function _findAllDataTable($results) |
||
389 | } |
||
390 |
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.