Complex classes like IncidentsTable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IncidentsTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class IncidentsTable extends Table |
||
31 | { |
||
32 | /** |
||
33 | * @var array |
||
34 | * |
||
35 | * @see http://book.cakephp.org/2.0/en/models/behaviors.html#using-behaviors |
||
36 | * @see Model::$actsAs |
||
37 | */ |
||
38 | public $actsAs = ['Summarizable']; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | * |
||
43 | * @see http://book.cakephp.org/2.0/en/models/model-attributes.html#validate |
||
44 | * @see http://book.cakephp.org/2.0/en/models/data-validation.html |
||
45 | * @see Model::$validate |
||
46 | */ |
||
47 | public $validate = [ |
||
48 | 'pma_version' => [ |
||
49 | 'rule' => 'notEmpty', |
||
50 | 'required' => true, |
||
51 | ], |
||
52 | 'php_version' => [ |
||
53 | 'rule' => 'notEmpty', |
||
54 | 'required' => true, |
||
55 | ], |
||
56 | 'full_report' => [ |
||
57 | 'rule' => 'notEmpty', |
||
58 | 'required' => true, |
||
59 | ], |
||
60 | 'stacktrace' => [ |
||
61 | 'rule' => 'notEmpty', |
||
62 | 'required' => true, |
||
63 | ], |
||
64 | 'browser' => [ |
||
65 | 'rule' => 'notEmpty', |
||
66 | 'required' => true, |
||
67 | ], |
||
68 | 'stackhash' => [ |
||
69 | 'rule' => 'notEmpty', |
||
70 | 'required' => true, |
||
71 | ], |
||
72 | 'user_os' => [ |
||
73 | 'rule' => 'notEmpty', |
||
74 | 'required' => true, |
||
75 | ], |
||
76 | 'locale' => [ |
||
77 | 'rule' => 'notEmpty', |
||
78 | 'required' => true, |
||
79 | ], |
||
80 | 'script_name' => [ |
||
81 | 'rule' => 'notEmpty', |
||
82 | 'required' => true, |
||
83 | ], |
||
84 | 'server_software' => [ |
||
85 | 'rule' => 'notEmpty', |
||
86 | 'required' => true, |
||
87 | ], |
||
88 | 'configuration_storage' => [ |
||
89 | 'rule' => 'notEmpty', |
||
90 | 'required' => true, |
||
91 | ], |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | * |
||
97 | * @see http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto |
||
98 | * @see Model::$belongsTo |
||
99 | */ |
||
100 | |||
101 | /** |
||
102 | * The fields which are summarized in the report page with charts and are also |
||
103 | * used in the overall stats and charts for the website. |
||
104 | * |
||
105 | * @var array |
||
106 | */ |
||
107 | public $summarizableFields = [ |
||
108 | 'browser', |
||
109 | 'pma_version', |
||
110 | 'php_version', |
||
111 | 'locale', |
||
112 | 'server_software', |
||
113 | 'user_os', |
||
114 | 'script_name', |
||
115 | 'configuration_storage', |
||
116 | ]; |
||
117 | |||
118 | 28 | public function __construct($id = false) |
|
150 | |||
151 | /** |
||
152 | * creates an incident/report record given a raw bug report object. |
||
153 | * |
||
154 | * This gets a decoded bug report from the submitted json body. This has not |
||
155 | * yet been santized. It either adds it as an incident to another report or |
||
156 | * creates a new report if nothing matches. |
||
157 | * |
||
158 | * @param array $bugReport the bug report being submitted |
||
159 | * |
||
160 | * @return array of: |
||
161 | * 1. array of inserted incident ids. If the report/incident was not |
||
162 | * correctly saved, false is put in it place. |
||
163 | * 2. array of newly created report ids. If no new report was created, |
||
164 | * an empty array is returned |
||
165 | */ |
||
166 | 2 | public function createIncidentFromBugReport($bugReport) |
|
167 | { |
||
168 | 2 | if ($bugReport == null) { |
|
169 | return [ |
||
170 | 1 | 'incidents' => [false], |
|
171 | 'reports' => [] |
||
172 | ]; |
||
173 | } |
||
174 | 2 | $incident_ids = []; // array to hold ids of all the inserted incidents |
|
175 | 2 | $new_report_ids = []; // array to hold ids of all newly created reports |
|
176 | |||
177 | // Avoid storing too many errors from single report |
||
178 | 2 | if (isset($bugReport['errors']) && count($bugReport['errors']) > 40) { |
|
179 | 1 | $bugReport['errors'] = array_slice($bugReport['errors'], 0, 40); |
|
180 | } |
||
181 | 2 | if (isset($bugReport['exception']['stack']) && count($bugReport['exception']['stack']) > 40) { |
|
182 | 1 | $bugReport['exception']['stack'] = array_slice($bugReport['exception']['stack'], 0, 40); |
|
183 | } |
||
184 | |||
185 | // Also sanitizes the bug report |
||
186 | 2 | $schematizedIncidents = $this->_getSchematizedIncidents($bugReport); |
|
187 | 2 | $incidentsTable = TableRegistry::get('Incidents'); |
|
188 | 2 | $reportsTable = TableRegistry::get('Reports'); |
|
189 | 2 | foreach ($schematizedIncidents as $index => $si) { |
|
190 | // find closest report. If not found, create a new report. |
||
191 | 2 | $closestReport = $this->_getClosestReport($bugReport, $index); |
|
192 | 2 | if ($closestReport) { |
|
193 | 2 | $si['report_id'] = $closestReport['id']; |
|
194 | 2 | $si = $incidentsTable->newEntity($si); |
|
195 | 2 | $si->created = date('Y-m-d H:i:s', time()); |
|
196 | 2 | $si->modified = date('Y-m-d H:i:s', time()); |
|
197 | |||
198 | 2 | $this->_logLongIncidentSubmissions($si, $incident_ids); |
|
199 | 2 | if (in_array(false, $incident_ids)) { |
|
200 | 2 | break; |
|
201 | } |
||
202 | } else { |
||
203 | // no close report. Create a new report. |
||
204 | 2 | $report = $this->_getReportDetails($bugReport, $index); |
|
205 | |||
206 | 2 | $this->_logLongIncidentSubmissions($si, $incident_ids); |
|
207 | 2 | if (in_array(false, $incident_ids)) { |
|
208 | break; |
||
209 | } |
||
210 | |||
211 | 2 | $report = $reportsTable->newEntity($report); |
|
212 | 2 | $report->created = date('Y-m-d H:i:s', time()); |
|
213 | 2 | $report->modified = date('Y-m-d H:i:s', time()); |
|
214 | 2 | $reportsTable->save($report); |
|
215 | |||
216 | 2 | $si['report_id'] = $report->id; |
|
217 | 2 | $new_report_ids[] = $report->id; |
|
218 | 2 | $si = $incidentsTable->newEntity($si); |
|
219 | 2 | $si->created = date('Y-m-d H:i:s', time()); |
|
220 | 2 | $si->modified = date('Y-m-d H:i:s', time()); |
|
221 | } |
||
222 | |||
223 | 2 | $isSaved = $incidentsTable->save($si); |
|
224 | 2 | if ($isSaved) { |
|
225 | 2 | array_push($incident_ids, $si->id); |
|
226 | 2 | if (! $closestReport) { |
|
227 | // add notifications entry |
||
228 | 2 | $tmpIncident = $incidentsTable->findById($si->id)->all()->first(); |
|
229 | 2 | if (! TableRegistry::get('Notifications')->addNotifications(intval($tmpIncident['report_id']))) { |
|
230 | Log::write( |
||
231 | 'error', |
||
232 | 'ERRORED: Notification::addNotifications() failed on Report#' |
||
233 | . $tmpIncident['report_id'], |
||
234 | 2 | 'alert' |
|
235 | ); |
||
236 | } |
||
237 | } |
||
238 | } else { |
||
239 | array_push($incident_ids, false); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | return [ |
||
244 | 2 | 'incidents' => $incident_ids, |
|
245 | 2 | 'reports' => $new_report_ids |
|
246 | ]; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * retrieves the closest report to a given bug report. |
||
251 | * |
||
252 | * it checks for another report with the same line number, filename and |
||
253 | * pma_version |
||
254 | * |
||
255 | * @param array $bugReport the bug report being checked |
||
256 | * Integer $index: for php exception type |
||
257 | * @param int $index The report index |
||
258 | * |
||
259 | * @return array the first similar report or null |
||
260 | */ |
||
261 | 3 | protected function _getClosestReport($bugReport, $index = 0) |
|
262 | { |
||
263 | 3 | if (isset($bugReport['exception_type']) |
|
264 | 3 | && $bugReport['exception_type'] == 'php' |
|
265 | ) { |
||
266 | 2 | $location = $bugReport['errors'][$index]['file']; |
|
267 | 2 | $linenumber = $bugReport['errors'][$index]['lineNum']; |
|
268 | } else { |
||
269 | list($location, $linenumber) = |
||
270 | 3 | $this->_getIdentifyingLocation($bugReport['exception']['stack']); |
|
271 | } |
||
272 | 3 | $report = TableRegistry::get('Reports')->findByLocationAndLinenumberAndPmaVersion( |
|
273 | 3 | $location, |
|
274 | $linenumber, |
||
275 | 3 | $this->getStrippedPmaVersion($bugReport['pma_version']) |
|
276 | 3 | )->all()->first(); |
|
277 | |||
278 | 3 | return $report; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * creates the report data from an incident that has no related report. |
||
283 | * |
||
284 | * @param array $bugReport the bug report the report record is being created for |
||
285 | * Integer $index: for php exception type |
||
286 | * @param int $index The report index |
||
287 | * |
||
288 | * @return array an array with the report fields can be used with Report->save |
||
289 | */ |
||
290 | 3 | protected function _getReportDetails($bugReport, $index = 0) |
|
326 | |||
327 | /** |
||
328 | * creates the incident data from the submitted bug report. |
||
329 | * |
||
330 | * @param array $bugReport the bug report the report record is being created for |
||
331 | * |
||
332 | * @return array an array of schematized incident. |
||
333 | * Can be used with Incident->save |
||
334 | */ |
||
335 | 3 | protected function _getSchematizedIncidents($bugReport) |
|
390 | |||
391 | /** |
||
392 | * Gets the identifiying location info from a stacktrace. |
||
393 | * |
||
394 | * This is used to skip stacktrace levels that are within the error reporting js |
||
395 | * files that sometimes appear in the stacktrace but are not related to the bug |
||
396 | * report |
||
397 | * |
||
398 | * returns two things in an array: |
||
399 | * - the first element is the filename/scriptname of the error |
||
400 | * - the second element is the linenumber of the error |
||
401 | * |
||
402 | * @param array $stacktrace the stacktrace being examined |
||
403 | * |
||
404 | * @return array an array with the filename/scriptname and linenumber of the |
||
405 | * error |
||
406 | */ |
||
407 | 5 | protected function _getIdentifyingLocation($stacktrace) |
|
445 | |||
446 | /** |
||
447 | * Gets a part of a version string according to the specified version Length. |
||
448 | * |
||
449 | * @param string $versionString the version string |
||
450 | * @param string $versionLength the number of version components to return. eg |
||
451 | * 1 for major version only and 2 for major and |
||
452 | * minor version |
||
453 | * |
||
454 | * @return string the major and minor version part |
||
455 | */ |
||
456 | 4 | protected function _getSimpleVersion($versionString, $versionLength) |
|
457 | { |
||
458 | 4 | $versionLength = (int) $versionLength; |
|
459 | 4 | if ($versionLength < 1) { |
|
460 | 1 | $versionLength = 1; |
|
461 | } |
||
462 | /* modify the re to accept a variable number of version components. I |
||
463 | * atleast take one component and optionally get more components if need be. |
||
464 | * previous code makes sure that the $versionLength variable is a positive |
||
465 | * int |
||
466 | */ |
||
467 | 4 | $result = preg_match( |
|
468 | 4 | "/^(\d+\.){" . ($versionLength - 1) . "}\d+/", |
|
469 | $versionString, |
||
470 | $matches |
||
471 | ); |
||
472 | 4 | if ($result) { |
|
473 | 4 | $simpleVersion = $matches[0]; |
|
474 | |||
475 | 4 | return $simpleVersion; |
|
476 | } |
||
477 | |||
478 | return $versionString; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Returns the version string stripped of |
||
483 | * 'deb', 'ubuntu' and other suffixes |
||
484 | * |
||
485 | * @param string $versionString phpMyAdmin version |
||
486 | * |
||
487 | * @return string stripped phpMyAdmin version |
||
488 | */ |
||
489 | 12 | public function getStrippedPmaVersion($versionString) |
|
504 | |||
505 | /** |
||
506 | * Gets the server name and version from the server signature. |
||
507 | * |
||
508 | * @param string $signature the server signature |
||
509 | * |
||
510 | * @return string the server name and version or UNKNOWN |
||
511 | */ |
||
512 | 4 | protected function _getServer($signature) |
|
513 | { |
||
514 | 4 | if (preg_match( |
|
515 | 4 | "/(apache\/\d+\.\d+)|(nginx\/\d+\.\d+)|(iis\/\d+\.\d+)" |
|
516 | 4 | . "|(lighttpd\/\d+\.\d+)/i", |
|
517 | $signature, |
||
518 | $matches |
||
519 | )) { |
||
520 | 4 | return $matches[0]; |
|
521 | } |
||
522 | |||
523 | 1 | return 'UNKNOWN'; |
|
524 | } |
||
525 | |||
526 | /** |
||
527 | * returns the hash pertaining to a stacktrace. |
||
528 | * |
||
529 | * @param array $stacktrace the stacktrace in question |
||
530 | * |
||
531 | * @return string the hash string of the stacktrace |
||
532 | */ |
||
533 | 4 | public function getStackHash($stacktrace) |
|
554 | |||
555 | /** |
||
556 | * Checks the length of stacktrace and full_report |
||
557 | * and logs if it is greater than what it can hold |
||
558 | * |
||
559 | * @param array $si submitted incident |
||
560 | * @param array $incident_ids incident IDs |
||
561 | * |
||
562 | * @return array $incident_ids |
||
563 | */ |
||
564 | 2 | private function _logLongIncidentSubmissions($si, &$incident_ids) |
|
590 | } |
||
591 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: