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 |
||
32 | class ReportsTable extends Table { |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany |
||
37 | * @see Cake::Model::$hasMany |
||
38 | */ |
||
39 | public $hasMany = array( |
||
40 | 'Incidents' => array( |
||
41 | 'dependant' => true |
||
42 | ) |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * @var Array |
||
47 | * @link http://book.cakephp.org/2.0/en/models/model-attributes.html#validate |
||
48 | * @link 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 | * @see Model::$findMethods |
||
63 | */ |
||
64 | public $findMethods = array( |
||
65 | 'allDataTable' => true, |
||
66 | 'arrayList' => true, |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * List of valid finder method options, supplied as the first parameter to find(). |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | public $status = array( |
||
75 | 'new' => 'New', |
||
76 | 'fixed' => 'Fixed', |
||
77 | 'wontfix' => "Won't Fix", |
||
78 | 'open' => "Open", |
||
79 | 'pending' => "Pending", |
||
80 | 'resolved' => "Resolved", |
||
81 | 'invalid' => "Invalid", |
||
82 | 'duplicate' => "Duplicate", |
||
83 | 'works-for-me' => "Works for me", |
||
84 | 'out-of-date' => "Out of Date" |
||
85 | ); |
||
86 | |||
87 | 14 | public function initialize(array $config) |
|
93 | /** |
||
94 | * Retrieves the incident records that are related to the current report |
||
95 | * |
||
96 | * @return Array the list of incidents ordered by creation date desc |
||
97 | */ |
||
98 | 3 | public function getIncidents() { |
|
106 | |||
107 | /** |
||
108 | * Retrieves the report records that are related to the current report |
||
109 | * |
||
110 | * @return Array the list of related reports |
||
111 | */ |
||
112 | 1 | public function getRelatedReports() { |
|
113 | 1 | return $this->find("all", array( |
|
114 | 1 | 'conditions' => $this->_relatedReportsConditions(), |
|
115 | )); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Retrieves the incident records that are related to the current report that |
||
120 | * also have a description |
||
121 | * |
||
122 | * @return Array the list of incidents ordered by description lenght desc |
||
123 | */ |
||
124 | 2 | public function getIncidentsWithDescription() { |
|
125 | 2 | return TableRegistry::get('Incidents')->find('all', array( |
|
126 | 'conditions' => array( |
||
127 | 'NOT' => array( |
||
128 | 'Incidents.steps is null' |
||
129 | ), |
||
130 | 2 | $this->_relatedIncidentsConditions(), |
|
131 | 2 | ), |
|
132 | 2 | 'order' => 'Incidents.steps desc' |
|
133 | )); |
||
134 | } |
||
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 | 2 | public function getIncidentsWithDifferentStacktrace() { |
|
143 | 2 | return TableRegistry::get('Incidents')->find('all', array( |
|
144 | 'fields' => array('DISTINCT Incidents.stackhash', 'Incidents.stacktrace', |
||
145 | 2 | 'Incidents.full_report', 'Incidents.exception_type'), |
|
146 | 2 | 'conditions' => $this->_relatedIncidentsConditions(), |
|
147 | 2 | 'group' => "Incidents.stackhash", |
|
148 | )); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Removes a report from a group of related reports |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | public function removeFromRelatedGroup($report) { |
||
157 | $report->related_to = null; |
||
158 | $this->save($report); |
||
159 | |||
160 | $rel_report = $this->findByRelatedTo($report->id)->first(); |
||
|
|||
161 | if ($rel_report) { |
||
162 | $this->updateAll( |
||
163 | array("related_to" => $rel_report->id), |
||
164 | array("related_to" => $report->id) |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | // remove all redundant self-groupings |
||
169 | $this->updateAll( |
||
170 | array("related_to" => null), |
||
171 | array("reports.related_to = reports.id") |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Adds a report to a group of related reports |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public function addToRelatedGroup($report, $related_to) { |
||
181 | $dup_report = $this->get($related_to); |
||
182 | |||
183 | if ($dup_report && $dup_report->related_to) { |
||
184 | $report->related_to = $dup_report->related_to; |
||
185 | } else { |
||
186 | $report->related_to = $related_to; |
||
187 | } |
||
188 | $this->save($report); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Returns the full url to the current report |
||
193 | * |
||
194 | * @return String url |
||
195 | */ |
||
196 | 1 | public function getUrl() { |
|
200 | |||
201 | /** |
||
202 | * groups related incidents by distinct values of a field. It may also provide |
||
203 | * the number of groups, whether to only include incidents that are related |
||
204 | * to the current report and also to only limit the search to incidents |
||
205 | * submited after a certain date |
||
206 | * |
||
207 | * @param String $fieldName the name of the field to group by |
||
208 | * @param Integer $limit the max number of groups to return |
||
209 | * @param Boolean $count whether to return the number of distinct groups |
||
210 | * @param Boolean $related whether to limit the search to only related incidents |
||
211 | * @param Date $timeLimit the date at which to start the search |
||
212 | * @return Array the groups with the count of each group and possibly the number |
||
213 | * of groups. Ex: array('Apache' => 2) or array(array('Apache' => 2), 1) |
||
214 | */ |
||
215 | 2 | public function getRelatedByField($fieldName, $limit = 10, $count = false, |
|
285 | |||
286 | /** |
||
287 | * returns an array of conditions that would return all related incidents to the |
||
288 | * current report |
||
289 | * |
||
290 | * @return Array the related incidents conditions |
||
291 | */ |
||
292 | 6 | protected function _relatedIncidentsConditions() { |
|
301 | |||
302 | /** |
||
303 | * returns an array of conditions that would return all related reports to the |
||
304 | * current report |
||
305 | * |
||
306 | * @return Array the related reports conditions |
||
307 | */ |
||
308 | 1 | protected function _relatedReportsConditions() { |
|
321 | } |
||
322 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: