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 = array( |
||
| 39 | 'Incidents' => array( |
||
| 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 = array( |
||
| 52 | 'error_message' => array( |
||
| 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 = array( |
||
| 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 = array( |
||
| 76 | 'new' => 'New', |
||
| 77 | 'fixed' => 'Fixed', |
||
| 78 | 'wontfix' => "Won't Fix", |
||
| 79 | 'open' => 'Open', |
||
| 80 | 'pending' => 'Pending', |
||
| 81 | 'resolved' => 'Resolved', |
||
| 82 | 'invalid' => 'Invalid', |
||
| 83 | 'duplicate' => 'Duplicate', |
||
| 84 | 'works-for-me' => 'Works for me', |
||
| 85 | 'out-of-date' => 'Out of Date', |
||
| 86 | ); |
||
| 87 | |||
| 88 | 15 | public function initialize(array $config) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Retrieves the incident records that are related to the current report. |
||
| 97 | * |
||
| 98 | * @return array the list of incidents ordered by creation date desc |
||
| 99 | */ |
||
| 100 | 4 | public function getIncidents() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Retrieves the report records that are related to the current report. |
||
| 113 | * |
||
| 114 | * @return array the list of related reports |
||
| 115 | */ |
||
| 116 | 1 | public function getRelatedReports() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Retrieves the incident records that are related to the current report that |
||
| 125 | * also have a description. |
||
| 126 | * |
||
| 127 | * @return array the list of incidents ordered by description lenght desc |
||
| 128 | */ |
||
| 129 | 2 | public function getIncidentsWithDescription() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Retrieves the incident records that are related to the current report that |
||
| 144 | * that have a different stacktrace. |
||
| 145 | * |
||
| 146 | * @return array the list of incidents |
||
| 147 | */ |
||
| 148 | 2 | public function getIncidentsWithDifferentStacktrace() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Removes a report from a group of related reports. |
||
| 160 | * |
||
| 161 | * @param mixed $report |
||
| 162 | */ |
||
| 163 | 1 | public function removeFromRelatedGroup($report) |
|
| 164 | { |
||
| 165 | 1 | $report->related_to = null; |
|
| 166 | 1 | $this->save($report); |
|
| 167 | |||
| 168 | 1 | $rel_report = $this->findByRelatedTo($report->id)->first(); |
|
|
|
|||
| 169 | 1 | if ($rel_report) { |
|
| 170 | $this->updateAll( |
||
| 171 | array('related_to' => $rel_report->id), |
||
| 172 | array('related_to' => $report->id) |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | |||
| 176 | // remove all redundant self-groupings |
||
| 177 | 1 | $this->updateAll( |
|
| 178 | 1 | array('related_to' => null), |
|
| 179 | 1 | array('reports.related_to = reports.id') |
|
| 180 | ); |
||
| 181 | 1 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Adds a report to a group of related reports. |
||
| 185 | * |
||
| 186 | * @param mixed $report |
||
| 187 | * @param mixed $related_to |
||
| 188 | */ |
||
| 189 | 1 | public function addToRelatedGroup($report, $related_to) |
|
| 190 | { |
||
| 191 | 1 | $dup_report = $this->get($related_to); |
|
| 192 | |||
| 193 | 1 | if ($dup_report && $dup_report->related_to) { |
|
| 194 | $report->related_to = $dup_report->related_to; |
||
| 195 | } else { |
||
| 196 | 1 | $report->related_to = $related_to; |
|
| 197 | } |
||
| 198 | 1 | $this->save($report); |
|
| 199 | 1 | } |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the full url to the current report. |
||
| 203 | * |
||
| 204 | * @return string url |
||
| 205 | */ |
||
| 206 | 1 | public function getUrl() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * groups related incidents by distinct values of a field. It may also provide |
||
| 214 | * the number of groups, whether to only include incidents that are related |
||
| 215 | * to the current report and also to only limit the search to incidents |
||
| 216 | * submited after a certain date. |
||
| 217 | * |
||
| 218 | * @param string $fieldName the name of the field to group by |
||
| 219 | * @param int $limit the max number of groups to return |
||
| 220 | * @param bool $count whether to return the number of distinct groups |
||
| 221 | * @param bool $related whether to limit the search to only related incidents |
||
| 222 | * @param Date $timeLimit the date at which to start the search |
||
| 223 | * |
||
| 224 | * @return array the groups with the count of each group and possibly the number |
||
| 225 | * of groups. Ex: array('Apache' => 2) or array(array('Apache' => 2), 1) |
||
| 226 | */ |
||
| 227 | 2 | public function getRelatedByField($fieldName, $limit = 10, $count = false, |
|
| 296 | |||
| 297 | /** |
||
| 298 | * returns an array of conditions that would return all related incidents to the |
||
| 299 | * current report. |
||
| 300 | * |
||
| 301 | * @return array the related incidents conditions |
||
| 302 | */ |
||
| 303 | 7 | protected function _relatedIncidentsConditions() |
|
| 304 | { |
||
| 305 | $conditions = array( |
||
| 306 | 7 | array('Incidents.report_id = ' . $this->id), |
|
| 307 | ); |
||
| 308 | 7 | $report = $this->get($this->id); |
|
| 309 | 7 | if ($report->related_to) { //TODO: fix when fix related reports |
|
| 310 | 1 | $conditions[] = array('Incidents.report_id = ' . $report->related_to); |
|
| 311 | } |
||
| 312 | |||
| 313 | 7 | return array('OR' => $conditions); |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * returns an array of conditions that would return all related reports to the |
||
| 318 | * current report. |
||
| 319 | * |
||
| 320 | * @return array the related reports conditions |
||
| 321 | */ |
||
| 322 | 1 | protected function _relatedReportsConditions() |
|
| 335 | } |
||
| 336 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: