Complex classes like SugarRelationship 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 SugarRelationship, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | abstract class SugarRelationship |
||
| 62 | { |
||
| 63 | protected $def; |
||
| 64 | protected $lhsLink; |
||
| 65 | protected $rhsLink; |
||
| 66 | protected $ignore_role_filter = false; |
||
| 67 | protected $self_referencing = false; //A relationship is self referencing when LHS module = RHS Module |
||
| 68 | |||
| 69 | protected static $beansToResave = array(); |
||
| 70 | |||
| 71 | public abstract function add($lhs, $rhs, $additionalFields = array()); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @abstract |
||
| 75 | * @param $lhs SugarBean |
||
| 76 | * @param $rhs SugarBean |
||
| 77 | * @return boolean |
||
| 78 | */ |
||
| 79 | public abstract function remove($lhs, $rhs); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @abstract |
||
| 83 | * @param $link Link2 loads the rows for this relationship that match the given link |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public abstract function load($link, $params = array()); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets the query to load a link. |
||
| 90 | * This is currently public, but should prob be made protected later. |
||
| 91 | * See Link2->getQuery |
||
| 92 | * @abstract |
||
| 93 | * @param $link Link Object to get query for. |
||
| 94 | * @return string|array query used to load this relationship |
||
| 95 | */ |
||
| 96 | public abstract function getQuery($link, $params = array()); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @abstract |
||
| 100 | * @param Link2 $link |
||
| 101 | * @return string|array the query to join against the related modules table for the given link. |
||
| 102 | */ |
||
| 103 | public abstract function getJoin($link); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @abstract |
||
| 107 | * @param SugarBean $lhs |
||
| 108 | * @param SugarBean $rhs |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public abstract function relationship_exists($lhs, $rhs); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @abstract |
||
| 115 | * @return string name of the table for this relationship |
||
| 116 | */ |
||
| 117 | public abstract function getRelationshipTable(); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $link Link2 removes all the beans associated with this link from the relationship |
||
| 121 | * @return boolean true if all beans were successfully removed or there |
||
| 122 | * were not related beans, false otherwise |
||
| 123 | */ |
||
| 124 | 38 | public function removeAll($link) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param $rowID id of SugarBean to remove from the relationship |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function removeById($rowID){ |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string name of right hand side module. |
||
| 160 | */ |
||
| 161 | 124 | public function getRHSModule() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return string name of left hand side module. |
||
| 168 | */ |
||
| 169 | 82 | public function getLHSModule() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return String left link in relationship. |
||
| 176 | */ |
||
| 177 | 126 | public function getLHSLink() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return String right link in relationship. |
||
| 184 | */ |
||
| 185 | 107 | public function getRHSLink() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return array names of fields stored on the relationship |
||
| 192 | */ |
||
| 193 | public function getFields() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $row values to be inserted into the relationship |
||
| 200 | * @return bool|void null if new row was inserted and true if an existing row was updated |
||
| 201 | */ |
||
| 202 | protected function addRow($row) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param $id id of row to update |
||
| 228 | * @param $values values to insert into row |
||
| 229 | * @return resource result of update satatement |
||
| 230 | */ |
||
| 231 | protected function updateRow($id, $values) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Removes one or more rows from the relationship table |
||
| 250 | * @param $where array of field=>value pairs to match |
||
| 251 | * @return bool|resource |
||
| 252 | */ |
||
| 253 | 2 | protected function removeRow($where) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Checks for an existing row who's keys match the one passed in. |
||
| 274 | * @param $row |
||
| 275 | * @return array|bool returns false if now row is found, otherwise the row is returned |
||
| 276 | */ |
||
| 277 | protected function checkExisting($row) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Gets the relationship role column check for the where clause |
||
| 304 | * @param string $table |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | 61 | protected function getRoleWhere($table = "", $ignore_role_filter = false) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param SugarBean $focus base bean the hooks is triggered from |
||
| 332 | * @param SugarBean $related bean being added/removed/updated from relationship |
||
| 333 | * @param string $link_name name of link being triggerd |
||
| 334 | * @return array base arguments to pass to relationship logic hooks |
||
| 335 | */ |
||
| 336 | 9 | protected function getCustomLogicArguments($focus, $related, $link_name) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Call the before add logic hook for a given link |
||
| 352 | * @param SugarBean $focus base bean the hooks is triggered from |
||
| 353 | * @param SugarBean $related bean being added/removed/updated from relationship |
||
| 354 | * @param string $link_name name of link being triggerd |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | protected function callBeforeAdd($focus, $related, $link_name="") |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Call the after add logic hook for a given link |
||
| 365 | * @param SugarBean $focus base bean the hooks is triggered from |
||
| 366 | * @param SugarBean $related bean being added/removed/updated from relationship |
||
| 367 | * @param string $link_name name of link being triggerd |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | protected function callAfterAdd($focus, $related, $link_name="") |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param SugarBean $focus |
||
| 378 | * @param SugarBean $related |
||
| 379 | * @param string $link_name |
||
| 380 | * @return void |
||
| 381 | */ |
||
| 382 | 9 | protected function callBeforeDelete($focus, $related, $link_name="") |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param SugarBean $focus |
||
| 390 | * @param SugarBean $related |
||
| 391 | * @param string $link_name |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | 9 | protected function callAfterDelete($focus, $related, $link_name="") |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @param $optional_array clause to add to the where query when populating this relationship. It should be in the |
||
| 402 | * @param string $add_and |
||
| 403 | * @param string $prefix |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | protected function getOptionalWhereClause($optional_array) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Adds a realted Bean to the list to be resaved along with the current bean. |
||
| 418 | * @static |
||
| 419 | * @param SugarBean $bean |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | public static function addToResaveList($bean) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * |
||
| 433 | * @static |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | 71 | public static function resaveRelatedBeans() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @return bool true if the relationship is a flex / parent relationship |
||
| 468 | */ |
||
| 469 | public function isParentRelationship() |
||
| 479 | |||
| 480 | 22 | public function __get($name) |
|
| 508 | } |
||
| 509 |