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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.