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( |
|
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 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: