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 |
||
30 | class ReportsTable extends Table |
||
31 | { |
||
32 | /** |
||
33 | * @var array |
||
34 | * |
||
35 | * @see http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany |
||
36 | * @see Cake::Model::$hasMany |
||
37 | */ |
||
38 | public $hasMany = [ |
||
39 | 'Incidents' => [ |
||
40 | 'dependant' => true, |
||
41 | ], |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | * |
||
47 | * @see http://book.cakephp.org/2.0/en/models/model-attributes.html#validate |
||
48 | * @see http://book.cakephp.org/2.0/en/models/data-validation.html |
||
49 | * @see Model::$validate |
||
50 | */ |
||
51 | public $validate = [ |
||
52 | 'error_message' => [ |
||
53 | 'rule' => 'notEmpty', |
||
54 | 'required' => true, |
||
55 | ], |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * List of valid finder method options, supplied as the first parameter to find(). |
||
60 | * |
||
61 | * @var array |
||
62 | * |
||
63 | * @see Model::$findMethods |
||
64 | */ |
||
65 | public $findMethods = [ |
||
66 | 'allDataTable' => true, |
||
67 | 'arrayList' => true, |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * List of valid finder method options, supplied as the first parameter to find(). |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | public $status = [ |
||
76 | 'new' => 'New', |
||
77 | 'invalid' => 'Invalid', |
||
78 | 'resolved' => 'Resolved', |
||
79 | 'forwarded' => 'Forwarded', |
||
80 | ]; |
||
81 | |||
82 | 25 | public function initialize(array $config) |
|
88 | |||
89 | /** |
||
90 | * Retrieves the incident records that are related to the current report. |
||
91 | * |
||
92 | * @return array the list of incidents ordered by creation date desc |
||
93 | */ |
||
94 | 4 | public function getIncidents() |
|
104 | |||
105 | /** |
||
106 | * Retrieves the report records that are related to the current report. |
||
107 | * |
||
108 | * @return array the list of related reports |
||
109 | */ |
||
110 | 2 | public function getRelatedReports() |
|
116 | |||
117 | /** |
||
118 | * Retrieves the incident records that are related to the current report that |
||
119 | * also have a description. |
||
120 | * |
||
121 | * @return array the list of incidents ordered by description lenght desc |
||
122 | */ |
||
123 | 3 | public function getIncidentsWithDescription() |
|
135 | |||
136 | /** |
||
137 | * Retrieves the incident records that are related to the current report that |
||
138 | * that have a different stacktrace. |
||
139 | * |
||
140 | * @return array the list of incidents |
||
141 | */ |
||
142 | 3 | public function getIncidentsWithDifferentStacktrace() |
|
155 | |||
156 | /** |
||
157 | * Removes a report from a group of related reports. |
||
158 | * |
||
159 | * @param \Cake\Datasource\EntityInterface $report The report instance |
||
160 | * @return void |
||
161 | */ |
||
162 | 1 | public function removeFromRelatedGroup($report) |
|
181 | |||
182 | /** |
||
183 | * Adds a report to a group of related reports. |
||
184 | * |
||
185 | * @param \Cake\Datasource\EntityInterface $report The report instance |
||
186 | * @param int $related_to The report Id |
||
187 | * @return void |
||
188 | */ |
||
189 | 1 | public function addToRelatedGroup($report, $related_to) |
|
202 | |||
203 | /** |
||
204 | * Returns the full url to the current report. |
||
205 | * |
||
206 | * @return string url |
||
207 | */ |
||
208 | 1 | public function getUrl() |
|
214 | |||
215 | /** |
||
216 | * groups related incidents by distinct values of a field. It may also provide |
||
217 | * the number of groups, whether to only include incidents that are related |
||
218 | * to the current report and also to only limit the search to incidents |
||
219 | * submited after a certain date. |
||
220 | * |
||
221 | * @param string $fieldName the name of the field to group by |
||
222 | * @param int $limit the max number of groups to return |
||
223 | * @param bool $count whether to return the number of distinct groups |
||
224 | * @param bool $related whether to limit the search to only related incidents |
||
225 | * @param string $timeLimit the date at which to start the search |
||
226 | * |
||
227 | * @return array the groups with the count of each group and possibly the number |
||
228 | * of groups. Ex: array('Apache' => 2) or array(array('Apache' => 2), 1) |
||
229 | */ |
||
230 | 4 | public function getRelatedByField( |
|
231 | $fieldName, |
||
232 | $limit = 10, |
||
233 | $count = false, |
||
234 | $related = true, |
||
235 | $timeLimit = null |
||
236 | ) { |
||
237 | 4 | $fieldAlias = "Incidents__$fieldName"; |
|
238 | $queryDetails = [ |
||
239 | 'conditions' => [ |
||
240 | 'NOT' => [ |
||
241 | 4 | "Incidents.$fieldName is null", |
|
242 | ], |
||
243 | ], |
||
244 | 4 | 'limit' => $limit, |
|
245 | ]; |
||
246 | |||
247 | 4 | if ($related) { |
|
248 | 3 | $queryDetails['conditions'][] = $this->_relatedIncidentsConditions(); |
|
249 | } |
||
250 | |||
251 | 4 | if ($timeLimit) { |
|
252 | 2 | $queryDetails['conditions'][] = [ |
|
253 | 2 | 'Incidents.created >=' => $timeLimit, |
|
254 | ]; |
||
255 | } |
||
256 | |||
257 | 4 | $groupedCount = TableRegistry::get('Incidents')->find('all', $queryDetails); |
|
258 | |||
259 | /* Ommit version number in case of browser and server_software fields. |
||
260 | * In case of browser field, version number is seperated by space, |
||
261 | * for example,'FIREFOX 47', hence split the value using space. |
||
262 | * In case of server_software field, version number is seperated by / |
||
263 | * for example, 'nginx/1.7', hence split the value using /. |
||
264 | * See http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functionsp://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions |
||
265 | * for how to use Sql functions with cake |
||
266 | */ |
||
267 | 4 | switch ($fieldName) { |
|
268 | 4 | View Code Duplication | case 'browser': |
269 | // SUBSTRING(browser, 1, LOCATE(' ', Incidents.browser)-1)) |
||
270 | 3 | $field = $groupedCount->func()->substring([ |
|
271 | 3 | $fieldName => 'literal', |
|
272 | 3 | '1' => 'literal', |
|
273 | 3 | "Locate(' ', Incidents.browser)-1" => 'literal', |
|
274 | ]); |
||
275 | 3 | break; |
|
276 | 4 | View Code Duplication | case 'server_software': |
277 | // SUBSTRING(server_software, 1, LOCATE('/', Incidents.server_software)-1)) |
||
278 | 3 | $field = $groupedCount->func()->substring([ |
|
279 | 3 | $fieldName => 'literal', |
|
280 | 3 | '1' => 'literal', |
|
281 | 3 | "Locate('/', Incidents.server_software)-1" => 'literal', |
|
282 | ]); |
||
283 | 3 | break; |
|
284 | default: |
||
285 | 4 | $field = $fieldName; |
|
286 | } |
||
287 | 4 | $groupedCount->select([ |
|
288 | 4 | 'count' => $groupedCount->func()->count('*'), |
|
289 | 4 | $fieldAlias => $field, |
|
290 | 4 | ])->group($fieldAlias)->distinct(["$fieldAlias"]) |
|
291 | 4 | ->order('count')->toArray(); |
|
292 | |||
293 | 4 | if ($count) { |
|
294 | 3 | $queryDetails['fields'] = ["$fieldName"]; |
|
295 | 3 | $queryDetails['limit'] = null; |
|
296 | 3 | $queryDetails['group'] = "Incidents.$fieldName"; |
|
297 | 3 | $totalCount = TableRegistry::get('Incidents')->find('all', $queryDetails)->count(); |
|
298 | |||
299 | return [ |
||
300 | 3 | $groupedCount, |
|
301 | 3 | $totalCount, |
|
302 | ]; |
||
303 | } |
||
304 | |||
305 | 2 | return $groupedCount; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * Updates the linked reports to a Github issue to newly received status |
||
310 | * |
||
311 | * @param string $issueNumber Github Issue number |
||
312 | * @param string $status New status to be set |
||
313 | * |
||
314 | * @return int Number of Linked reports updated |
||
315 | */ |
||
316 | 1 | public function setLinkedReportStatus($issueNumber, $status) |
|
327 | |||
328 | /** |
||
329 | * returns an array of conditions that would return all related incidents to the |
||
330 | * current report. |
||
331 | * |
||
332 | * @return array the related incidents conditions |
||
333 | */ |
||
334 | 7 | protected function _relatedIncidentsConditions() |
|
346 | |||
347 | /** |
||
348 | * returns an array of conditions that would return all related reports to the |
||
349 | * current report. |
||
350 | * |
||
351 | * @return array the related reports conditions |
||
352 | */ |
||
353 | 2 | protected function _relatedReportsConditions() |
|
366 | } |
||
367 |
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.