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() |
|
65 | |||
66 | 1 | public function view($reportId) |
|
67 | { |
||
68 | 1 | $this->autoRender = false; |
|
69 | |||
70 | 1 | if (!isset($reportId) || !$reportId) { |
|
71 | throw new NotFoundException(__('Invalid Report')); |
||
72 | } |
||
73 | 1 | $report = $this->Reports->findById($reportId)->toArray(); |
|
74 | 1 | if (!$report) { |
|
75 | 1 | throw new NotFoundException(__('Invalid Report')); |
|
76 | } |
||
77 | |||
78 | 1 | $this->set('report', $report); |
|
79 | 1 | $this->set('project_name', Configure::read('GithubRepoPath')); |
|
80 | 1 | $this->Reports->id = $reportId; |
|
81 | 1 | $this->set('incidents', $this->Reports->getIncidents()->toArray()); |
|
82 | 1 | $this->set('incidents_with_description', |
|
83 | 1 | $this->Reports->getIncidentsWithDescription()); |
|
84 | 1 | $this->set('incidents_with_stacktrace', |
|
85 | 1 | $this->Reports->getIncidentsWithDifferentStacktrace()); |
|
86 | 1 | $this->set('related_reports', $this->Reports->getRelatedReports()); |
|
87 | 1 | $this->set('status', $this->Reports->status); |
|
88 | 1 | $this->_setSimilarFields($reportId); |
|
89 | |||
90 | // if there is an unread notification for this report, then mark it as read |
||
91 | 1 | $current_developer = TableRegistry::get('Developers')-> |
|
92 | 1 | findById($this->request->session()->read('Developer.id'))->all()->first(); |
|
93 | //$current_developer = Sanitize::clean($current_developer); |
||
94 | 1 | if ($current_developer) { |
|
95 | 1 | TableRegistry::get('Notifications')->deleteAll( |
|
96 | 1 | array('developer_id' => $current_developer['Developer']['id'], |
|
97 | 1 | 'report_id' => $reportId, |
|
98 | ), |
||
99 | 1 | false |
|
|
|||
100 | ); |
||
101 | } |
||
102 | 1 | } |
|
103 | |||
104 | 1 | public function data_tables() |
|
105 | { |
||
106 | $subquery_params = array( |
||
107 | 1 | 'fields' => array( |
|
108 | 'report_id' => 'report_id', |
||
109 | 'inci_count' => 'COUNT(id)', |
||
110 | ), |
||
111 | 'group' => 'report_id', |
||
112 | ); |
||
113 | 1 | $subquery = TableRegistry::get('incidents')->find('all', $subquery_params); |
|
114 | |||
115 | // override automatic aliasing, for proper usage in joins |
||
116 | $aColumns = array( |
||
117 | 1 | 'id' => 'id', |
|
118 | 'error_name' => 'error_name', |
||
119 | 'error_message' => 'error_message', |
||
120 | 'location' => 'location', |
||
121 | 'pma_version' => 'pma_version', |
||
122 | 'status' => 'status', |
||
123 | 'exception_type' => 'exception_type', |
||
124 | 'inci_count' => 'inci_count', |
||
125 | ); |
||
126 | |||
127 | 1 | $searchConditions = $this->OrderSearch->getSearchConditions($aColumns); |
|
128 | 1 | $orderConditions = $this->OrderSearch->getOrder($aColumns); |
|
129 | |||
130 | $params = array( |
||
131 | 1 | 'fields' => $aColumns, |
|
132 | 'conditions' => array( |
||
133 | 1 | $searchConditions, |
|
134 | 1 | 'related_to is NULL', |
|
135 | ), |
||
136 | 1 | 'order' => $orderConditions, |
|
137 | ); |
||
138 | |||
139 | 1 | $pagedParams = $params; |
|
140 | 1 | $pagedParams['limit'] = intval($this->request->query('iDisplayLength')); |
|
141 | 1 | $pagedParams['offset'] = intval($this->request->query('iDisplayStart')); |
|
142 | |||
143 | 1 | $rows = $this->_findAllDataTable( |
|
144 | 1 | $this->Reports->find('all', $pagedParams)->innerJoin( |
|
145 | 1 | array('incidents' => $subquery), array('incidents.report_id = Reports.id') |
|
146 | ) |
||
147 | ); |
||
148 | //$rows = Sanitize::clean($rows); |
||
149 | 1 | $totalFiltered = $this->Reports->find('all', $params)->count(); |
|
150 | |||
151 | // change exception_type from boolean values to strings |
||
152 | // add incident count for related reports |
||
153 | 1 | $dispRows = array(); |
|
154 | 1 | foreach ($rows as $row) { |
|
155 | 1 | $row[5] = $this->Reports->status[$row[5]]; |
|
156 | 1 | $row[6] = (intval($row[6])) ? ('php') : ('js'); |
|
157 | $input_elem = "<input type='checkbox' name='reports[]' value='" |
||
158 | 1 | . $row[0] |
|
159 | 1 | . "'/>"; |
|
160 | |||
161 | $subquery_params_count = array( |
||
162 | 1 | 'fields' => array( |
|
163 | 'report_id' => 'report_id', |
||
164 | ), |
||
165 | ); |
||
166 | 1 | $subquery_count = TableRegistry::get('incidents')->find( |
|
167 | 1 | 'all', $subquery_params_count |
|
168 | ); |
||
169 | |||
170 | $params_count = array( |
||
171 | 1 | 'fields' => array('inci_count' => 'inci_count'), |
|
172 | 'conditions' => array( |
||
173 | 1 | 'related_to = ' . $row[0], |
|
174 | ), |
||
175 | ); |
||
176 | |||
177 | 1 | $inci_count_related = $this->Reports->find('all', $params_count)->innerJoin( |
|
178 | 1 | array('incidents' => $subquery_count), |
|
179 | 1 | array('incidents.report_id = Reports.related_to') |
|
180 | 1 | )->count(); |
|
181 | |||
182 | 1 | $row[7] += $inci_count_related; |
|
183 | |||
184 | 1 | array_unshift($row, $input_elem); |
|
185 | 1 | array_push($dispRows, $row); |
|
186 | } |
||
187 | |||
188 | $response = array( |
||
189 | 1 | 'iTotalRecords' => $this->Reports->find('all')->count(), |
|
190 | 1 | 'iTotalDisplayRecords' => $totalFiltered, |
|
191 | 1 | 'sEcho' => intval($this->request->query('sEcho')), |
|
192 | 1 | 'aaData' => $dispRows |
|
193 | ); |
||
194 | 1 | $this->autoRender = false; |
|
195 | 1 | $this->response->body(json_encode($response)); |
|
196 | |||
197 | 1 | return $this->response; |
|
198 | } |
||
199 | |||
200 | 1 | public function mark_related_to($reportId) |
|
226 | |||
227 | 1 | public function unmark_related_to($reportId) |
|
248 | |||
249 | public function change_state($reportId) |
||
273 | |||
274 | /** |
||
275 | * To carry out mass actions on Reports |
||
276 | * Currently only to change their statuses. |
||
277 | * Can be Extended for other mass operations as well. |
||
278 | * Expects an array of Report Ids as a POST parameter. |
||
279 | */ |
||
280 | public function mass_action() |
||
320 | |||
321 | //# HELPERS |
||
322 | |||
323 | 1 | protected function _setSimilarFields($id) |
|
324 | { |
||
325 | 1 | $this->Reports->id = $id; |
|
326 | |||
327 | 1 | $this->set('columns', TableRegistry::get('Incidents')->summarizableFields); |
|
328 | 1 | $relatedEntries = array(); |
|
329 | |||
330 | 1 | View Code Duplication | foreach (TableRegistry::get('Incidents')->summarizableFields as $field) { |
331 | list($entriesWithCount, $totalEntries) = |
||
332 | 1 | $this->Reports->getRelatedByField($field, 25, true); |
|
333 | 1 | $relatedEntries[$field] = $entriesWithCount; |
|
334 | 1 | $this->set("${field}_distinct_count", $totalEntries); |
|
335 | } |
||
336 | //error_log(json_encode($relatedEntries)); |
||
337 | $this->set('related_entries', $relatedEntries); |
||
338 | } |
||
339 | |||
340 | protected function _findArrayList($results, $key) |
||
349 | |||
350 | protected function _findAllDataTable($results) |
||
364 | } |
||
365 |
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.