Complex classes like Link2 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 Link2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class Link2 { |
||
| 61 | |||
| 62 | protected $relationship; //relationship object this link is tied to. |
||
| 63 | protected $focus; //SugarBean this link uses as the context for its calls. |
||
| 64 | protected $def; //Field def for this link |
||
| 65 | protected $name; //Field name for this link |
||
| 66 | protected $beans; //beans on the other side of the link |
||
| 67 | protected $rows; //any additional fields on the relationship |
||
| 68 | protected $loaded; //true if this link has been loaded from the database |
||
| 69 | protected $relationship_fields = array(); |
||
| 70 | //Used to store unsaved beans on this relationship that will be combined with the ones pulled from the DB if getBeans() is called. |
||
| 71 | protected $tempBeans = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param $linkName String name of a link field in the module's vardefs |
||
| 75 | * @param $bean SugarBean focus bean for this link (one half of a relationship) |
||
| 76 | * @param $linkDef Array Optional vardef for the link in case it can't be found in the passed in bean for the global dictionary |
||
| 77 | * @return void |
||
|
|
|||
| 78 | * |
||
| 79 | */ |
||
| 80 | 126 | function __construct($linkName, $bean, $linkDef = false){ |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Returns false if no relationship was found for this link |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | 126 | public function loadedSuccesfully() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Forces the link to load the relationship rows. |
||
| 156 | * Will be called internally when the $rows property is accessed or get() is called |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 95 | public function load($params = array()) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Perform a query on this relationship. |
||
| 169 | * |
||
| 170 | * @param array $params An array that can contain the following parameters:<br/> |
||
| 171 | * <ul><li><b>where:</b> An array with 3 key/value pairs. |
||
| 172 | * lhs_field: The name of the field to check search. |
||
| 173 | * operator: The operator to use in the search. |
||
| 174 | * rhs_value: The value to search for.<br/> |
||
| 175 | * Example:<pre> |
||
| 176 | * 'where' => array( |
||
| 177 | 'lhs_field' => 'source', |
||
| 178 | 'operator' => '=', |
||
| 179 | 'rhs_value' => 'external' |
||
| 180 | )</pre> |
||
| 181 | * </li> |
||
| 182 | * <li><b>limit:</b> The maximum number of rows</li> |
||
| 183 | * <li><b>deleted:</b> If deleted is set to 1, only deleted records related to the current record will be returned.</li></ul> |
||
| 184 | */ |
||
| 185 | 95 | public function query($params){ |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @return array ids of records related through this link |
||
| 191 | */ |
||
| 192 | 59 | public function get($role = false) { |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @deprecated |
||
| 201 | * |
||
| 202 | * @return string name of table for the relationship of this link |
||
| 203 | */ |
||
| 204 | public function getRelatedTableName() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string the name of the module on the other side of this link |
||
| 210 | */ |
||
| 211 | 73 | public function getRelatedModuleName() { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return string the name of the link field used on the other side of the rel |
||
| 223 | */ |
||
| 224 | public function getRelatedModuleLinkName() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * |
||
| 236 | * @return string "many" if multiple records can be related through this link |
||
| 237 | * or "one" if at most, one record can be related. |
||
| 238 | */ |
||
| 239 | public function getType() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return SugarBean The parent Bean this link references |
||
| 255 | */ |
||
| 256 | 104 | public function getFocus() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @deprecated |
||
| 263 | * @return list of fields that exist only on the relationship |
||
| 264 | */ |
||
| 265 | public function getRelatedFields(){ |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param $name |
||
| 271 | * @return The value for the relationship field $name |
||
| 272 | */ |
||
| 273 | public function getRelatedField($name){ |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return SugarRelationship the relationship object this link references |
||
| 282 | */ |
||
| 283 | 1 | public function getRelationshipObject() { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return string "LHS" or "RHS" depending on the side of the relationship this link represents |
||
| 289 | */ |
||
| 290 | 126 | public function getSide() { |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool true if LHSModule == RHSModule |
||
| 331 | */ |
||
| 332 | protected function is_self_relationship() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return bool true if this link represents a relationship where the parent could be one of multiple modules. (ex. Activities parent) |
||
| 338 | */ |
||
| 339 | public function isParentRelationship(){ |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param $params array of parameters. Possible parameters include: |
||
| 345 | * 'join_table_link_alias': alias the relationship join table in the query (for M2M relationships), |
||
| 346 | * 'join_table_alias': alias for the final table to be joined against (usually a module main table) |
||
| 347 | * @param bool $return_array if true the query is returned as a array broken up into |
||
| 348 | * select, join, where, type, rel_key, and joined_tables |
||
| 349 | * @return string/array join query for this link |
||
| 350 | */ |
||
| 351 | 41 | function getJoin($params, $return_array =false) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $params optional parameters. Possible Values; |
||
| 358 | * 'return_as_array': returns the query broken into |
||
| 359 | * @return String/Array query to grab just ids for this relationship |
||
| 360 | */ |
||
| 361 | 5 | function getQuery($params = array()) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * This function is similair getJoin except for M2m relationships it won't join agaist the final table. |
||
| 368 | * Its used to retrieve the ID of the related beans only |
||
| 369 | * @param $params array of parameters. Possible parameters include: |
||
| 370 | * 'return_as_array': returns the query broken into |
||
| 371 | * @param bool $return_array same as passing 'return_as_array' into parameters |
||
| 372 | * @return string/array query to use when joining for subpanels |
||
| 373 | */ |
||
| 374 | public function getSubpanelQuery($params = array(), $return_array = false) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Use with caution as if you have a large list of beans in the relationship, |
||
| 383 | * it can cause the app to timeout or run out of memory. |
||
| 384 | * |
||
| 385 | * @param array $params An array that can contain the following parameters:<br/> |
||
| 386 | * <ul><li><b>where:</b> An array with 3 key/value pairs. |
||
| 387 | * lhs_field: The name of the field to check search. |
||
| 388 | * operator: The operator to use in the search. |
||
| 389 | * rhs_value: The value to search for.<br/> |
||
| 390 | * Example:<pre> |
||
| 391 | * 'where' => array( |
||
| 392 | 'lhs_field' => 'source', |
||
| 393 | 'operator' => '=', |
||
| 394 | 'rhs_value' => 'external' |
||
| 395 | )</pre> |
||
| 396 | * </li> |
||
| 397 | * <li><b>limit:</b> The maximum number of beans to load.</li> |
||
| 398 | * <li><b>deleted:</b> If deleted is set to 1, only deleted records related to the current record will be returned.</li></ul> |
||
| 399 | * @return array of SugarBeans related through this link. |
||
| 400 | */ |
||
| 401 | 45 | function getBeans($params = array()) { |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @return bool true if this link has initialized its related beans. |
||
| 457 | */ |
||
| 458 | 45 | public function beansAreLoaded() { |
|
| 461 | |||
| 462 | /** |
||
| 463 | * use this function to create link between 2 objects |
||
| 464 | * 1:1 will be treated like 1 to many. |
||
| 465 | * |
||
| 466 | * the function also allows for setting of values for additional field in the table being |
||
| 467 | * updated to save the relationship, in case of many-to-many relationships this would be the join table. |
||
| 468 | * |
||
| 469 | * @param array $rel_keys array of ids or SugarBean objects. If you have the bean in memory, pass it in. |
||
| 470 | * @param array $additional_values the values should be passed as key value pairs with column name as the key name and column value as key value. |
||
| 471 | * |
||
| 472 | * @return boolean|array Return true if all relationships were added. Return an array with the failed keys if any of them failed. |
||
| 473 | */ |
||
| 474 | 6 | function add($rel_keys,$additional_values=array()) { |
|
| 512 | |||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Marks the relationship delted for this given record pair. |
||
| 517 | * @param $id id of the Parent/Focus SugarBean |
||
| 518 | * @param string $related_id id or SugarBean to unrelate. Pass a SugarBean if you have it. |
||
| 519 | * @return boolean true if delete was successful or false if it was not |
||
| 520 | */ |
||
| 521 | 38 | function delete($id, $related_id='') { |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Returns a SugarBean with the given ID from the related module. |
||
| 544 | * @param bool $id id of related record to retrieve |
||
| 545 | * @return SugarBean |
||
| 546 | */ |
||
| 547 | 6 | protected function getRelatedBean($id = false) |
|
| 552 | |||
| 553 | 5 | public function &__get($name) |
|
| 572 | |||
| 573 | 2 | public function __set($name, $val) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Add a bean object to the list of beans currently loaded to this relationship. |
||
| 582 | * This for the most part should not need to be called except by the relatipnship implementation classes. |
||
| 583 | * @param SugarBean $bean |
||
| 584 | * @return void |
||
| 585 | */ |
||
| 586 | public function addBean($bean) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Remove a bean object from the list of beans currently loaded to this relationship. |
||
| 600 | * This for the most part should not need to be called except by the relatipnship implementation classes. |
||
| 601 | * |
||
| 602 | * @param SugarBean $bean |
||
| 603 | * @return void |
||
| 604 | */ |
||
| 605 | public function removeBean($bean) |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * Directly queries the databse for set of values. The relationship classes and not link should handle this. |
||
| 619 | * @deprecated |
||
| 620 | * @param $table_name string relationship table |
||
| 621 | * @param $join_key_values array of key=>values to identify this relationship by |
||
| 622 | * @return bool true if the given join key set exists in the relationship table |
||
| 623 | */ |
||
| 624 | 1 | public function relationship_exists($table_name, $join_key_values) { |
|
| 664 | |||
| 665 | //Below are functions not used directly and exist for backwards compatibility with customizations, will be removed in a later version |
||
| 666 | |||
| 667 | /* returns array of keys for duplicate checking, first check for an index of type alternate_key, if not found searches for |
||
| 668 | * primary key. |
||
| 669 | * |
||
| 670 | */ |
||
| 671 | 1 | public function _get_alternate_key_fields($table_name) { |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @deprecated |
||
| 688 | * Gets the vardef for the relationship of this link. |
||
| 689 | */ |
||
| 690 | public function _get_link_table_definition($table_name, $def_name) { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @deprecated |
||
| 700 | * Return the name of the role field for the passed many to many table. |
||
| 701 | * if there is no role filed : return false |
||
| 702 | * @param $table_name name of relationship table to inspect |
||
| 703 | * @return bool|string |
||
| 704 | */ |
||
| 705 | public function _get_link_table_role_field($table_name) { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @deprecated |
||
| 721 | * |
||
| 722 | * @return boolean returns true if this link is LHS |
||
| 723 | */ |
||
| 724 | public function _get_bean_position() |
||
| 728 | } |
||
| 729 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.