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 | 'invalid' => 'Invalid', |
||
| 78 | 'resolved' => 'Resolved' |
||
| 79 | ); |
||
| 80 | |||
| 81 | 15 | public function initialize(array $config) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Retrieves the incident records that are related to the current report. |
||
| 90 | * |
||
| 91 | * @return array the list of incidents ordered by creation date desc |
||
| 92 | */ |
||
| 93 | 4 | public function getIncidents() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Retrieves the report records that are related to the current report. |
||
| 106 | * |
||
| 107 | * @return array the list of related reports |
||
| 108 | */ |
||
| 109 | 2 | public function getRelatedReports() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Retrieves the incident records that are related to the current report that |
||
| 118 | * also have a description. |
||
| 119 | * |
||
| 120 | * @return array the list of incidents ordered by description lenght desc |
||
| 121 | */ |
||
| 122 | 3 | public function getIncidentsWithDescription() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Retrieves the incident records that are related to the current report that |
||
| 137 | * that have a different stacktrace. |
||
| 138 | * |
||
| 139 | * @return array the list of incidents |
||
| 140 | */ |
||
| 141 | 3 | public function getIncidentsWithDifferentStacktrace() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Removes a report from a group of related reports. |
||
| 153 | * |
||
| 154 | * @param mixed $report |
||
| 155 | */ |
||
| 156 | 1 | public function removeFromRelatedGroup($report) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Adds a report to a group of related reports. |
||
| 178 | * |
||
| 179 | * @param mixed $report |
||
| 180 | * @param mixed $related_to |
||
| 181 | */ |
||
| 182 | 1 | public function addToRelatedGroup($report, $related_to) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the full url to the current report. |
||
| 198 | * |
||
| 199 | * @return string url |
||
| 200 | */ |
||
| 201 | 1 | public function getUrl() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * groups related incidents by distinct values of a field. It may also provide |
||
| 209 | * the number of groups, whether to only include incidents that are related |
||
| 210 | * to the current report and also to only limit the search to incidents |
||
| 211 | * submited after a certain date. |
||
| 212 | * |
||
| 213 | * @param string $fieldName the name of the field to group by |
||
| 214 | * @param int $limit the max number of groups to return |
||
| 215 | * @param bool $count whether to return the number of distinct groups |
||
| 216 | * @param bool $related whether to limit the search to only related incidents |
||
| 217 | * @param Date $timeLimit the date at which to start the search |
||
| 218 | * |
||
| 219 | * @return array the groups with the count of each group and possibly the number |
||
| 220 | * of groups. Ex: array('Apache' => 2) or array(array('Apache' => 2), 1) |
||
| 221 | */ |
||
| 222 | 3 | public function getRelatedByField($fieldName, $limit = 10, $count = false, |
|
| 291 | |||
| 292 | /** |
||
| 293 | * returns an array of conditions that would return all related incidents to the |
||
| 294 | * current report. |
||
| 295 | * |
||
| 296 | * @return array the related incidents conditions |
||
| 297 | */ |
||
| 298 | 7 | protected function _relatedIncidentsConditions() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * returns an array of conditions that would return all related reports to the |
||
| 313 | * current report. |
||
| 314 | * |
||
| 315 | * @return array the related reports conditions |
||
| 316 | */ |
||
| 317 | 2 | protected function _relatedReportsConditions() |
|
| 330 | } |
||
| 331 |
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: